X-Git-Url: https://git.piment-noir.org/?p=Project_algorithmic_C.git;a=blobdiff_plain;f=lib%2Fothello.c;h=02eac9d82cad3c92011b35f03d4ca30e3e8262fc;hp=0cd65a4d0a266e57c6cda3e87bd263877d629ff7;hb=74e2b93b658575fa792ada51c3bf1cdc3cfde247;hpb=2e5c189444a82df0d9127c94322ec4e3b159dcce diff --git a/lib/othello.c b/lib/othello.c index 0cd65a4..02eac9d 100644 --- a/lib/othello.c +++ b/lib/othello.c @@ -16,4 +16,87 @@ * ===================================================================================== */ +#include +#include +#include "othello.h" + +unsigned int current_player(unsigned int round_count) { + + if (round_count % 2 != 0) { + return player_two; + } else { + return player_one; + } +} + +/* for consitency with ncurses, the board coordinates are in the following order: + * --x--> + * | + * y + * | + * v */ + +unsigned int get_box_value(int y, int x, unsigned int pawn_array[board_size][board_size]) { + + return pawn_array[y][x]; +} + +bool is_box_type(int y, int x, unsigned int pawn_array[board_size][board_size], unsigned int type) { + + if (type > 2) { + return NULL; + } + if (get_box_value(y, x, pawn_array) == type) { + return true; + } else { + return false; + } +} + +/* helper function to set a value != empty at the (y, x) in the pawns array */ +int** set_pawn(int y, int x, unsigned int type, unsigned int pawn_array[board_size][board_size]) { + + if (is_box_type(y, x, pawn_array, empty)) { + pawn_array[y][x] = type; + return pawn_array; + } else { + return NULL; + } +} + +static int** zero_pawns(unsigned int pawn_array[board_size][board_size]) { + for (unsigned int i = 0; i < board_size; i++) { + for (unsigned int j = 0; j < board_size; j++) { + pawn_array = set_pawn(i, j, empty, pawn_array); + } + } + return pawn_array; +} + +/* set the pawns in the start position */ +int** init_pawns(unsigned int pawn_array[board_size][board_size]) { + + pawn_array = zero_pawns(pawn_array); + pawn_array = set_pawn(5, 4, black, pawn_array); + pawn_array = set_pawn(4, 5, black, pawn_array); + pawn_array = set_pawn(4, 4, white, pawn_array); + pawn_array = set_pawn(5, 5, white, pawn_array); + return pawn_array; +} + +unsigned int count_pawn_type(unsigned int pawn_array[board_size][board_size], unsigned int type) { + unsigned int count = 0; + + if (type > 2) { + return -1; + } + for (unsigned int i = 0; i < board_size; i++) { + for (unsigned int j = 0; j < board_size; j++) { + if (pawn_array[i][j] == type) { + count++; + } + } + } + return count; +}