From cca9463e74e618b99f79d6b2c29b9b3f4f5a1511 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 5 May 2017 01:12:12 +0200 Subject: [PATCH] Polish the UI: MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * clear the screen when neeed; * be more informational about wrong shot; * permit to restart the game once finished. Signed-off-by: Jérôme Benoit --- src/main.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 08c6216..ce7c8a6 100644 --- a/src/main.c +++ b/src/main.c @@ -17,9 +17,11 @@ int main() { char* title_msg = "Jeu Othello"; char* score_msg = "Pions %s: %d"; + char* invalid_move_msg = "Coup invalide"; char* player_msg = "Joueur %d (%s) joue !"; char* winner_msg = "Joueur %d (%s) gagne !"; char* draw_msg = "Egalite !"; + char* exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer"; /* linked list of the history shots */ //struct shots_history_list_s shots_history; @@ -81,19 +83,24 @@ int main() { 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); - display_array(1, 1, pawns); + //display_array(1, 1, pawns); int y; char x_char; bool input_ok = false; + unsigned int nb_pawns_reversed = 0; do { y = 0; 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_char); + int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, (center_x - strlen(prompt_msg)/2), prompt_msg, &y, &x_char); int x = map_col_letter_to_index(x_char); - if (valid_shot(y, x, player, pawns) > 0 && prmt_rt == 1) { + nb_pawns_reversed = valid_shot(y, x, player, pawns); + if (nb_pawns_reversed > 0 && prmt_rt == 1) { input_ok = true; + clear(); + } else { + mvprintw(center_y + 26/2 + 4, (center_x - strlen(invalid_move_msg)/2), invalid_move_msg); } } while (!input_ok); @@ -101,18 +108,32 @@ int main() { /* here are all the end of the game conditions */ if (is_board_full(pawns)) { + print_board(board_center_y, board_center_x); + /* print the updated pawns before exiting */ + print_pawns(board_center_y, board_center_x, pawns); unsigned int winner = eval_winner(nb_white, nb_black); if (winner != 0) { if (winner == player_one) { - mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "noir"), winner_msg, winner, "noir"); + mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "noir")/2, winner_msg, winner, "noir"); } else { - mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "blanc"), winner_msg, winner, "blanc"); + mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "blanc")/2, winner_msg, winner, "blanc"); } } else { mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg); } - /* print and implement restart possibility */ + mvprintw(center_y + 26/2 + 1, (center_x - strlen(exit_msg)/2), exit_msg); exit_condition = true; + /* getch() is blocking */ + int key_exit = getch(); + if (key_exit == 'r') { + round = 0; + player = player_one; + nb_white = nb_black = 0; + /* FIXME: do not seem properly reset the pawns 2D array */ + init_pawns(pawns); + exit_condition = false; + clear(); + } } refresh(); -- 2.34.1