Skip to content
Snippets Groups Projects
rugby-scoring.c 1.98 KiB
#include "rugby-scoring.h"

static inline void
possibility_free (gpointer possibility)
{
    g_slice_free (RugbyPossibility, possibility);
}

/**
 * rugby_scoring_get_possibilities:
 * @score: the score of a team
 *
 * Gets the possible ways of scoring @score points.
 *
 * Returns: (transfer-full): the possibilities
 */
GPtrArray *
rugby_scoring_get_possibilities (gint score)
{
    GPtrArray *array;

    g_return_val_if_fail (score >= 0, NULL);

    array = g_ptr_array_new_with_free_func (possibility_free);

    gint possibilities = 0;
    gint max_tries = score / TRY_POINTS;
    gint max_utries = score / UTRY_POINTS;

    for (gint tries = 0; tries <= max_tries; tries++)
    {
        for (gint utries = 0; utries <= max_utries; utries++)
        {
            gint left = score - (tries * TRY_POINTS) - (utries * UTRY_POINTS);

            if (left < 0)
                continue;

            if (left % KICK_POINTS == 0)
            {
                gint kicks = left / KICK_POINTS;

                RugbyPossibility *possibility = g_slice_new (RugbyPossibility);
                possibility->tries = tries;
                possibility->utries = utries;
                possibility->kicks = kicks;

                g_ptr_array_add (array, possibility);
                possibilities++;
            }
        }
    }

    return array;
}

/**
 * rugby_scoring_get_max_tries:
 * @score: the score of a team
 *
 * Gets the maximum number of tries possible for a given score. Not related to
 * Max Evans.
 *
 * Returns: the maximum number of tries possible
 */
gint
rugby_scoring_get_max_tries (gint score)
{
	return (score % 5 == 1) ? score / UTRY_POINTS - 1 : score / UTRY_POINTS;
}

/**
 * rugby_scoring_get_max_kicks:
 * @score: the score of a team
 *
 * Gets the maximum number of kicks (penalties and drop goals) for a given score.
 *
 * Returns: the maximum number of kicks possible
 */
gint
rugby_scoring_get_max_kicks (gint score)
{
	return (score % 3 == 1) ? score / KICK_POINTS : score / KICK_POINTS - 1;
}