Code cleanup on the shot validation path
[Project_algorithmic_C.git] / src / main.c
index b8c0c5e4c9bd1b05ac2ab4d83e5ab63f85f1def5..3ed7991b2229685cdc40c975a1937b38f3709f32 100644 (file)
@@ -8,34 +8,45 @@
 #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);
+    const char* title_msg = "Jeu Othello";
+    const char* score_msg = "Pions %s: %d";
+    const char* invalid_move_msg = "Coup invalide";
+    const char* player_msg = "Joueur %d (%s) joue !";
+    const char* winner_msg = "Joueur %d (%s) gagne !";
+    const char* draw_msg = "Egalite !";
+    const char* exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer";
 
-    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 !";
+    /* linked list of playable shots */
+    struct shots_list_s playable_shots;
+    INIT_LIST_HEAD(&playable_shots.list);
 
-    unsigned int pawns[board_size][board_size] = {{}};
-    pawns[board_size][board_size] = init_pawns(pawns);
+    unsigned int pawns[board_size][board_size] = {
+        {0, 0},
+        {0, 0}
+    };
+    init_pawns(pawns);
 
     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);
-    /* FIXME: fail if the screen size is too small */
+    if (row < min_y || col < min_x) {
+        endwin();
+        printf("Votre terminal est trop petit pour afficher ce jeu.\n");
+        printf("Merci d'agrandir la fenetre de votre terminal.\n");
+        exit(EXIT_FAILURE);
+    }
     echo();
     curs_set(0);
 
@@ -54,50 +65,81 @@ 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_pawns_type(pawns, white);
+        nb_black = count_pawns_type(pawns, 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);
 
-        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);
+        build_playable_shots_list(player, &playable_shots, pawns);
+        print_shots_list(board_center_y, board_center_x, &playable_shots);
+
+        display_array(1, 1, pawns);
 
         int y;
-        char x;
+        char x_char;
         bool input_ok = false;
+        unsigned int nb_pawns_reversed = 0;
         do {
             y = 0;
-            x = "";
-            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) {
+            x_char = (char)"";
+            const 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 x = map_col_letter_to_index(x_char);
+            /* TODO: a comparaison to the linked list of playable shots is better */
+            if (is_legal_shot(y, x, player, pawns) && prmt_rt == 1) {
+                nb_pawns_reversed = valid_shot(y, x, player, pawns);
                 input_ok = true;
+                clear();
+            } else {
+                mvprintw(center_y + 26/2 + 4, (center_x - strlen(invalid_move_msg)/2), invalid_move_msg);
             }
         } 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);
 
-        round++; /* increment the round count */
+        free_shots_list(&playable_shots);
 
-        refresh();
+        round++; /* increment the round count */
 
         /* 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);
+            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) {
-                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")/2, winner_msg, winner, "noir");
+                } else {
+                    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();
+
     } while (!exit_condition);
-    
+
     endwin();
 
     exit(EXIT_SUCCESS);