Code cleanup and fix the logic behind the player constants number
[Project_algorithmic_C.git] / src / main.c
index 4f5e61da73ac243a7b194b937f9ccb08ec3d4e5d..b8c0c5e4c9bd1b05ac2ab4d83e5ab63f85f1def5 100644 (file)
@@ -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;
         }