<commit_before>#include <stdlib.h>

#include "game.h"

game_t *game_new() {
    game_t *new_game = malloc(sizeof(game_t));

    game_reset(new_game);
    return new_game;
}


void game_free(game_t *g) {
    free(g);
}


void game_reset(game_t *g) {
    g->num_goats_to_put = 20;
    g->num_eaten_goats  = 0;

    // Goats begin the game
    g->turn = GOAT_TURN;

    // Clear the board
    position_t pos;
    for (pos.r = 0; pos.r < 5; pos.r++) {
        for (pos.c = 0; pos.c < 5; pos.c++) {
            board_set_cell(&(g->board), pos, EMPTY_CELL);
        }
    }

    // Put tigers in the corners
    board_set_cell(&g->board, (position_t){0, 0 }, TIGER_CELL);
    board_set_cell(&g->board, (position_t){0, 4 }, TIGER_CELL);
    board_set_cell(&g->board, (position_t){4, 0 }, TIGER_CELL);
    board_set_cell(&g->board, (position_t){4, 4 }, TIGER_CELL);
}


// test_possible_position tests from a given position if a move can be done.
// If possible_dest is set, then if the token can be moved to the given postion
// then the destination is maked as possible.
//
// If test_diagonals is set to true, we only care about the diagonals. Meaning
// top-left, top-right, bottom_left and bottom-right.
// Otherwhise, we only look at the top, bottom, left and right positions.
//
// Returns true a movement is possible.
static bool test_possible_position(board_t              *board,
                                   position_t           pos,
                                   bool                 test_diagonals,
                                   possible_positions_t *possible_dest) {
    int mvt_possible = false;


    /*   not test_diagonals     |      test_diagonals
     *     .    +    .          |        +    .    +
     *          |               |          \     /
     *     + -- O -- +          |        .    O    .
     *          |               |          /     \
     *     .    +   .           |        +         +
     *
     *  . not tested
     *  + tested
     */

    int        diag_offset = test_diagonals ? 1 : 0;
    position_t to_check[]  = {
        { pos.c + 1,           pos.r + diag_offset },
        { pos.c - 1,           pos.r - diag_offset },
        { pos.c + diag_offset, pos.r + 1           },
        { pos.c - diag_offset, pos.r - 1           }
    };


    for (int i = 0; i < sizeof(to_check) / sizeof(to_check[0]); i++) {
        if (position_is_valid(to_check[i]) &&
            (board_get_cell(board, to_check[i]) == EMPTY_CELL)) {
            if (possible_dest == NULL) {
                return true;
            } else {
                set_possible_position(possible_dest, to_check[i], true);
                mvt_possible = true;
            }
        }
    }

    if (board_get_cell(board, pos) == TIGER_CELL) {
        // We have a tiger, so we can jump and eat other goats.

        int        diag_offset     = test_diagonals ? 2 : 0;
        position_t jump_to_check[] = {
            { pos.c + 2,           pos.r + diag_offset },
            { pos.c - 2,           pos.r - diag_offset },
            { pos.c + diag_offset, pos.r + 2           },
            { pos.c - diag_offset, pos.r - 2           }
        };


        for (int i = 0; i < sizeof(to_check) / sizeof(to_check[0]); i++) {<commit_msg>Fix game bug checking twice two diagonal positions<commit_after>#include <stdlib.h>

#include "game.h"

game_t *game_new() {
    game_t *new_game = malloc(sizeof(game_t));

    game_reset(new_game);
    return new_game;
}


void game_free(game_t *g) {
    free(g);
}


void game_reset(game_t *g) {
    g->num_goats_to_put = 20;
    g->num_eaten_goats  = 0;

    // Goats begin the game
    g->turn = GOAT_TURN;

    // Clear the board
    position_t pos;
    for (pos.r = 0; pos.r < 5; pos.r++) {
        for (pos.c = 0; pos.c < 5; pos.c++) {
            board_set_cell(&(g->board), pos, EMPTY_CELL);
        }
    }

    // Put tigers in the corners
    board_set_cell(&g->board, (position_t){0, 0 }, TIGER_CELL);
    board_set_cell(&g->board, (position_t){0, 4 }, TIGER_CELL);
    board_set_cell(&g->board, (position_t){4, 0 }, TIGER_CELL);
    board_set_cell(&g->board, (position_t){4, 4 }, TIGER_CELL);
}


// test_possible_position tests from a given position if a move can be done.
// If possible_dest is set, then if the token can be moved to the given postion
// then the destination is maked as possible.
//
// If test_diagonals is set to true, we only care about the diagonals. Meaning
// top-left, top-right, bottom_left and bottom-right.
// Otherwhise, we only look at the top, bottom, left and right positions.
//
// Returns true a movement is possible.
static bool test_possible_position(board_t              *board,
                                   position_t           pos,
                                   bool                 test_diagonals,
                                   possible_positions_t *possible_dest) {
    int mvt_possible = false;


    /*   not test_diagonals     |      test_diagonals
     *     .    +    .          |        +    .    +
     *          |               |          \     /
     *     + -- O -- +          |        .    O    .
     *          |               |          /     \
     *     .    +   .           |        +         +
     *
     *  . not tested
     *  + tested
     */

    int        diag_offset = test_diagonals ? 1 : 0;
    position_t to_check[]  = {
        { pos.c + 1,           pos.r + diag_offset },
        { pos.c - 1,           pos.r - diag_offset },
        { pos.c - diag_offset, pos.r + 1           },
        { pos.c + diag_offset, pos.r - 1           }
    };


    for (int i = 0; i < sizeof(to_check) / sizeof(to_check[0]); i++) {
        if (position_is_valid(to_check[i]) &&
            (board_get_cell(board, to_check[i]) == EMPTY_CELL)) {
            if (possible_dest == NULL) {
                return true;
            } else {
                set_possible_position(possible_dest, to_check[i], true);
                mvt_possible = true;
            }
        }
    }

    if (board_get_cell(board, pos) == TIGER_CELL) {
        // We have a tiger, so we can jump and eat other goats.

        int        diag_offset     = test_diagonals ? 2 : 0;
        position_t jump_to_check[] = {
            { pos.c + 2,           pos.r + diag_offset },
            { pos.c - 2,           pos.r - diag_offset },
            { pos.c - diag_offset, pos.r + 2           },
            { pos.c + diag_offset, pos.r - 2           }
        };


        for (int i = 0; i < sizeof(to_check) / sizeof(to_check[0]); i++) {
{'fraction_non_alphanumeric': 0.1220070154033857, 'fraction_numerical': 0.009455543693762392, 'mean_word_length': 1.4397321428571428, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5339404', 'n_tokens_mistral': 2276, 'n_tokens_neox': 2149, 'n_words': 517}