From: Jérôme Benoit Date: Fri, 28 Apr 2017 13:27:23 +0000 (+0200) Subject: Code cleanup and fix the logic behind the player constants number X-Git-Url: https://git.piment-noir.org/?p=Project_algorithmic_C.git;a=commitdiff_plain;h=a8b5457691fcb565dd185e7a623dd53eca96127c Code cleanup and fix the logic behind the player constants number Signed-off-by: Jérôme Benoit --- diff --git a/lib/constants.c b/lib/constants.c index 66728da..289bf04 100644 --- a/lib/constants.c +++ b/lib/constants.c @@ -23,12 +23,12 @@ //const unsigned int board_size = 8; const unsigned int empty = 0; -const unsigned int white = 1; -const unsigned int black = 2; +const unsigned int black = 1; +const unsigned int white = 2; /* FIXME: reuse the two above variables would be better */ -const unsigned int player_one = 2; /* first player is black */ -const unsigned int player_two = 1; +const unsigned int player_one = 1; /* first player is black */ +const unsigned int player_two = 2; const unsigned int hint_allowed = 3; const unsigned int hint_forbidden = 4; diff --git a/lib/constants.h b/lib/constants.h index bc15fd1..186e5db 100644 --- a/lib/constants.h +++ b/lib/constants.h @@ -23,8 +23,8 @@ //extern const unsigned int board_size; /* 8 */ extern const unsigned int empty; /* 0 */ -extern const unsigned int white; /* 1 */ -extern const unsigned int black; /* 2 */ +extern const unsigned int black; /* 1 */ +extern const unsigned int white; /* 2 */ extern const unsigned int player_one; /* black */ extern const unsigned int player_two; /* white */ diff --git a/lib/othello.c b/lib/othello.c index b576894..4bbf120 100644 --- a/lib/othello.c +++ b/lib/othello.c @@ -127,6 +127,17 @@ bool is_board_full(unsigned int pawn_array[board_size][board_size]) { return true; } +unsigned int eval_winner(unsigned int nb_white, unsigned int nb_black) { + + if (nb_white > nb_black) { + return player_two; + } else if (nb_white < nb_black) { + return player_one; + } else { + return 0; + } +} + void status_pawn(int y, int x, unsigned int pawn_array[board_size][board_size]) { } diff --git a/lib/othello.h b/lib/othello.h index 62fb04a..24bf9fb 100644 --- a/lib/othello.h +++ b/lib/othello.h @@ -28,14 +28,15 @@ struct shots_list { }; unsigned int current_player(unsigned int round_count); +unsigned int eval_winner(unsigned int nb_white, unsigned int nb_black); int** init_pawns(unsigned int pawn_array[board_size][board_size]); +unsigned int get_box_value(int y, int x, unsigned int pawn_array[board_size][board_size]); +int** set_pawn(int y, int x, unsigned int type, unsigned int pawn_array[board_size][board_size]); bool is_valid_input(int y, int x, unsigned int pawn_array[board_size][board_size]); bool is_box_type(int y, int x, unsigned int pawn_array[board_size][board_size], unsigned int type); bool is_board_full(unsigned int pawn_array[board_size][board_size]); -unsigned int get_box_value(int y, int x, unsigned int pawn_array[board_size][board_size]); -int** set_pawn(int y, int x, unsigned int type, unsigned int pawn_array[board_size][board_size]); unsigned int count_pawn_type(unsigned int pawn_array[board_size][board_size], unsigned int type); diff --git a/lib/ui.c b/lib/ui.c index c75e3b0..ae14d1d 100644 --- a/lib/ui.c +++ b/lib/ui.c @@ -56,6 +56,12 @@ void print_board(int y, int x) { mvprintw(y+25, x, " -----+----+----+----+----+----+----+-----"); } +static void set_default_colors() { + + init_pair(1, COLOR_WHITE, COLOR_BLACK); + attron(COLOR_PAIR(1)); +} + static void print_o(int y, int x, unsigned int type) { if (type == black) { @@ -69,8 +75,7 @@ static void print_o(int y, int x, unsigned int type) { mvprintw(y, x, "/\\"); mvprintw(y+1, x, "\\/"); /* reset to default */ - init_pair(1, COLOR_WHITE, COLOR_BLACK); - attron(COLOR_PAIR(1)); + set_default_colors(); } /* will be used for pawn placement hints */ @@ -86,8 +91,7 @@ static void print_x(int y, int x, unsigned int type) { mvprintw(y, x, "\\/"); mvprintw(y+1, x,"/\\"); /* reset to default */ - init_pair(1, COLOR_WHITE, COLOR_BLACK); - attron(COLOR_PAIR(1)); + set_default_colors(); } /* y = 1: y -> 0 x = 1: 1 -> 1 diff --git a/src/main.c b/src/main.c index 4f5e61d..b8c0c5e 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,7 @@ #include "debug.h" int main() { + int min_y = 26, min_x = 42; int row = 0, col = 0; unsigned int round = 0; unsigned int player = player_one; /* first player is black */ @@ -16,7 +17,12 @@ int main() { LIST_HEAD(shots_list); - char* player_msg; + char* title_msg = "Jeu Othello"; + char* score_white_msg = "Pions blancs: %d"; + char* score_black_msg = "Pions noirs: %d"; + char* player_msg = "Joueur %d joue !"; + char* winner_msg = "Joueur %d gagne !"; + char* draw_msg = "Egalite !"; unsigned int pawns[board_size][board_size] = {{}}; pawns[board_size][board_size] = init_pawns(pawns); @@ -30,7 +36,6 @@ int main() { start_color(); getmaxyx(stdscr, row, col); /* FIXME: fail if the screen size is too small */ - //noecho(); echo(); curs_set(0); @@ -45,26 +50,18 @@ int main() { print_board(board_center_y, board_center_x); print_pawns(board_center_y, board_center_x, pawns); - char* title_msg = "Jeu Othello"; mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg); + player = current_player(round); + + mvprintw(center_y - 26/2 - 2, (center_x - strlen(player_msg)/2), player_msg, player); + nb_white = count_pawn_type(pawns, white); nb_black = count_pawn_type(pawns, black); - char* score_white_msg = "Pions blancs: %d"; mvprintw(center_y, center_x - 42/2 - strlen(score_white_msg) - 2, score_white_msg, nb_white); - char* score_black_msg = "Pions noirs: %d"; mvprintw(center_y, center_x + 42/2 + 2, score_black_msg, nb_black); - player = current_player(round); - - if (player == player_one) { - player_msg = "Joueur un (noir) joue !"; - } else { - player_msg = "Joueur deux (blanc) joue !"; - } - mvprintw(center_y - 26/2 - 2, (center_x - strlen(player_msg)/2), player_msg); - int y; char x; bool input_ok = false; @@ -86,8 +83,16 @@ int main() { refresh(); - if (is_board_full(pawns) || round == 60) { - /* print the winnner and the restart possibility */ + /* here are all the end of the game conditions */ + //if (is_board_full(pawns) || round == 60) { + if (is_board_full(pawns)) { + int winner = eval_winner(nb_white, nb_black); + if (winner != 0) { + mvprintw(center_y - 26/2 - 2, (center_x - strlen(winner_msg)/2), winner_msg, winner); + } else { + mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg); + } + /* print and implement restart possibility */ exit_condition = true; }