From 45ce2fe369cead248855111baa5fe0c0495acf69 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 29 Apr 2017 00:03:41 +0200 Subject: [PATCH] Implement : MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * Already played shots linked list; * Terminal min size detection; * Some helper functions and declare helpful data structures. Fix also a bunch of compiler warnings. Signed-off-by: Jérôme Benoit --- analyse_descendante | 9 ++++++++ lib/constants.c | 9 ++++++++ lib/constants.h | 9 ++++++++ lib/debug.c | 2 +- lib/othello.c | 44 ++++++++++++++++++++++++++------------ lib/othello.h | 19 +++++++++++++++-- lib/ui.c | 26 ++++++++++++++++++++++- lib/ui.h | 3 ++- src/main.c | 51 ++++++++++++++++++++++++++++----------------- 9 files changed, 135 insertions(+), 37 deletions(-) diff --git a/analyse_descendante b/analyse_descendante index 2baa9bc..b3f559e 100644 --- a/analyse_descendante +++ b/analyse_descendante @@ -53,3 +53,12 @@ Sinon si case contient un pion adverse alors continuer l'exploration dans la même direction; Sinon si case contient pion du joueur alors les pions adverses vus précédement doivent être retournés + +explorer(i, j, dir) +Si Vide ou Dehors + Rien à faire +Sinon + Si PionAdverse + explorer CaseSuivante + Sinon + RetournerPionsRencontrés diff --git a/lib/constants.c b/lib/constants.c index 289bf04..7248975 100644 --- a/lib/constants.c +++ b/lib/constants.c @@ -32,3 +32,12 @@ const unsigned int player_two = 2; const unsigned int hint_allowed = 3; const unsigned int hint_forbidden = 4; + +const unsigned int north = 5; +const unsigned int north_east = 6; +const unsigned int east = 7; +const unsigned int south_east = 8; +const unsigned int south = 9; +const unsigned int south_west = 10; +const unsigned int west = 11; +const unsigned int north_west = 12; diff --git a/lib/constants.h b/lib/constants.h index 186e5db..976b299 100644 --- a/lib/constants.h +++ b/lib/constants.h @@ -32,5 +32,14 @@ extern const unsigned int player_two; /* white */ extern const unsigned int hint_allowed; /* legal place for a pawn */ extern const unsigned int hint_forbidden; /* illegal place for a pawn */ +extern const unsigned int north; +extern const unsigned int north_east; +extern const unsigned int east; +extern const unsigned int south_east; +extern const unsigned int south; +extern const unsigned int south_west; +extern const unsigned int west; +extern const unsigned int north_west; + #endif /* CONSTANTS_H */ diff --git a/lib/debug.c b/lib/debug.c index e322990..a2bf9ee 100644 --- a/lib/debug.c +++ b/lib/debug.c @@ -22,5 +22,5 @@ void dbg_mvprintw(int base_y, int base_x, const char* fmt, va_list varglist) { - mvprintw(base_y, base_x - strlen(fmt)/2, fmt, varglist); + mvprintw(base_y, base_x - snprintf(NULL, 0, fmt, varglist)/2, fmt, varglist); } diff --git a/lib/othello.c b/lib/othello.c index 4bbf120..1115e92 100644 --- a/lib/othello.c +++ b/lib/othello.c @@ -31,7 +31,7 @@ unsigned int current_player(unsigned int round_count) { } /* for consitency with ncurses, the board coordinates are in the following order: - * O-x--> + * O--x--> * | * y * | @@ -55,12 +55,12 @@ bool is_box_type(int y, int x, unsigned int pawn_array[board_size][board_size], } } -/* helper function to set a value != empty at the (y, x) in the pawns array */ +/* helper function to set a value != empty at the (y, x) coordinates 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-1][x-1] = type; - return pawn_array; + return (int**)pawn_array; } else { return NULL; } @@ -73,7 +73,7 @@ static int** zero_pawns(unsigned int pawn_array[board_size][board_size]) { pawn_array = set_pawn(i, j, empty, pawn_array); } } - return pawn_array; + return (int**)pawn_array; } /* set the pawns in the start position */ @@ -84,7 +84,7 @@ int** init_pawns(unsigned int pawn_array[board_size][board_size]) { 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; + return (int**)pawn_array; } unsigned int count_pawn_type(unsigned int pawn_array[board_size][board_size], unsigned int type) { @@ -95,7 +95,7 @@ unsigned int count_pawn_type(unsigned int pawn_array[board_size][board_size], un } for (unsigned int i = 1; i <= board_size; i++) { for (unsigned int j = 1; j <= board_size; j++) { - if (get_box_value(i, j, pawn_array) == type) { + if (is_box_type(i, j, pawn_array, type)) { count++; } } @@ -117,6 +117,7 @@ bool is_valid_input(int y, int x, unsigned int pawn_array[board_size][board_size bool is_board_full(unsigned int pawn_array[board_size][board_size]) { + /* an alternate method is to test the round count vs. 60 */ for (unsigned int i = 1; i <= board_size; i++) { for (unsigned int j = 1; j <= board_size; j++) { if (is_box_type(i, j, pawn_array , empty)) { @@ -138,14 +139,31 @@ unsigned int eval_winner(unsigned int nb_white, unsigned int nb_black) { } } -void status_pawn(int y, int x, unsigned int pawn_array[board_size][board_size]) { - -} - -bool is_legal_box(int y, int x, int player, unsigned int pawn_array[board_size][board_size]) { - +void direction_to_coordinates(unsigned int direction, int* start_y, int* start_x) { + + if (direction == north) { + *start_y = *start_y - 1; + } else if (direction == north_east) { + *start_y = *start_y - 1; + *start_x = *start_x + 1; + } else if (direction == east) { + *start_x = *start_x + 1; + } else if (direction == south_east) { + *start_y = *start_y + 1; + *start_x = *start_x + 1; + } else if (direction == south) { + *start_y = *start_y + 1; + } else if (direction == south_west) { + *start_y = *start_y + 1; + *start_x = *start_x - 1; + } else if (direction == west) { + *start_x = *start_x - 1; + } else if (direction == north_west) { + *start_y = *start_y - 1; + *start_x = *start_x - 1; + } } -bool reverse_box(unsigned int pawn_array[board_size][board_size]) { +bool explore(int y, int x, int direction) { } diff --git a/lib/othello.h b/lib/othello.h index 24bf9fb..a591405 100644 --- a/lib/othello.h +++ b/lib/othello.h @@ -22,9 +22,24 @@ #include "constants.h" #include "list.h" -struct shots_list { +/* TODO: must be used to replace the explicit pawns 2D array */ +struct pawns_s { + unsigned int pawns[board_size][board_size]; +}; + +/* linked list of played shots */ +struct shots_history_list_s { + struct list_head list; + unsigned int*** pawn_array_member; /* pointer to a cell of the pawns 2D array */ + /* struct* pawns_s pawn */ +}; + +/* linked list of can play shots */ +struct shots_exploration_s { struct list_head list; - unsigned int*** pawn_array_member; + int y; + int x; + unsigned int type; }; unsigned int current_player(unsigned int round_count); diff --git a/lib/ui.c b/lib/ui.c index ae14d1d..be739aa 100644 --- a/lib/ui.c +++ b/lib/ui.c @@ -129,7 +129,7 @@ void print_pawns(int base_y, int base_x, unsigned int pawn_array[board_size][boa } } -int map_col_letter_to_int(char c) { +int map_col_letter_to_index(char c) { if (c == 'a' || c == 'A') { return 1; @@ -152,6 +152,30 @@ int map_col_letter_to_int(char c) { } } +/* return capital letters */ +char map_col_index_to_letter(int index) { + + if (index == 1) { + return 'A'; + } else if (index == 2) { + return 'B'; + } else if (index == 3) { + return 'C'; + } else if (index == 4) { + return 'D'; + } else if (index == 5) { + return 'E'; + } else if (index == 6) { + return 'F'; + } else if (index == 7) { + return 'G'; + } else if (index == 9) { + return 'H'; + } else { + return -1; + } +} + int prompt_values(WINDOW* windows, int base_y, int base_x, const char* msg, int* y, char* x) { mvwprintw(windows, base_y, base_x, "%s\n", msg); int retVal = mvwscanw(windows, base_y + 1, base_x + strlen(msg)/2, "%d%c", y, x); diff --git a/lib/ui.h b/lib/ui.h index b24b674..c93a091 100644 --- a/lib/ui.h +++ b/lib/ui.h @@ -25,7 +25,8 @@ int prompt_values(WINDOW* windows, int base_y, int base_x, const char* msg, int* y, char* x); -int map_col_letter_to_int(char c); +int map_col_letter_to_index(char c); +char map_col_index_to_letter(int index); /* ncurses printing */ void print_board(int y, int x); diff --git a/src/main.c b/src/main.c index b8c0c5e..6a56eed 100644 --- a/src/main.c +++ b/src/main.c @@ -8,20 +8,17 @@ #include "debug.h" int main() { - int min_y = 26, min_x = 42; + int min_y = 26 + 6, min_x = 42 + 14 + 15 + 4; int row = 0, col = 0; unsigned int round = 0; unsigned int player = player_one; /* first player is black */ bool exit_condition = false; unsigned int nb_white = 0, nb_black = 0; - LIST_HEAD(shots_list); - 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* score_msg = "Pions %s: %d"; + char* player_msg = "Joueur %d (%s) joue !"; + char* winner_msg = "Joueur %d (%s) gagne !"; char* draw_msg = "Egalite !"; unsigned int pawns[board_size][board_size] = {{}}; @@ -30,11 +27,17 @@ int main() { initscr(); if (has_colors() == false) { endwin(); - printf("Votre terminal ne supporte pas les couleurs\n"); + printf("Votre terminal ne supporte pas les couleurs.\n"); exit(EXIT_FAILURE); } start_color(); getmaxyx(stdscr, row, col); + if (row < min_y || col < min_x) { + endwin(); + printf("Votre terminal est trop petit pour afficher le jeu.\n"); + printf("Merci d'agrandir la fenetre de votre terminal.\n"); + exit(EXIT_FAILURE); + } /* FIXME: fail if the screen size is too small */ echo(); curs_set(0); @@ -54,41 +57,51 @@ int main() { player = current_player(round); - mvprintw(center_y - 26/2 - 2, (center_x - strlen(player_msg)/2), player_msg, player); + if (player == player_one) { + mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, player_msg, player, "noir")/2, player_msg, player, "noir"); + } else { + mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, player_msg, player, "blanc")/2, player_msg, player, "blanc"); + } nb_white = count_pawn_type(pawns, white); nb_black = count_pawn_type(pawns, black); - mvprintw(center_y, center_x - 42/2 - strlen(score_white_msg) - 2, score_white_msg, nb_white); - mvprintw(center_y, center_x + 42/2 + 2, score_black_msg, nb_black); + mvprintw(center_y, center_x - 42/2 - snprintf(NULL, 0, score_msg, "noirs", nb_black) - 2, score_msg, "noirs", nb_black); + mvprintw(center_y, center_x + 42/2 + 2, score_msg, "blancs", nb_white); int y; char x; bool input_ok = false; do { y = 0; - x = ""; + x = (char)""; char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):"; int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, center_x - strlen(prompt_msg)/2, prompt_msg, &y, &x); - if (is_valid_input(y, map_col_letter_to_int(x), pawns) && prmt_rt == 1) { + if (is_valid_input(y, map_col_letter_to_index(x), pawns) && prmt_rt == 1) { input_ok = true; } } while (!input_ok); - pawns[board_size][board_size] = set_pawn(y, map_col_letter_to_int(x), player, pawns); - struct shots_list shot_current; - shot_current.pawn_array_member = &pawns[y-1][x-1]; - //list_add(shot_current.list, shots_list.list); + pawns[board_size][board_size] = set_pawn(y, map_col_letter_to_index(x), player, pawns); + struct shots_history_list_s shots_history; + INIT_LIST_HEAD(&shots_history.list); + struct shots_history_list_s shots_elmt; + shots_elmt.pawn_array_member = &pawns[y-1][x-1]; + list_add(&shots_elmt.list, &shots_history.list); round++; /* increment the round count */ refresh(); /* here are all the end of the game conditions */ - //if (is_board_full(pawns) || round == 60) { + //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); + if (winner == player_one) { + mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "noir"), winner_msg, winner, "noir"); + } else { + mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "blanc"), winner_msg, winner, "blanc"); + } } else { mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg); } -- 2.34.1