Be more explicit in Makefiles about external libraries linking
[TD_C.git] / TP_13 / exo2 / src / main.c
index 8978ef2eee6ed33ce70c14b70a4f956e428769c7..bdc1ccd881172d9e3c206cd493e9001a6054e05d 100644 (file)
@@ -7,35 +7,46 @@
 #include "coordinates.h"
 
 int main() {
-    int row, col, errno = 0, round = 0, player = 0, key_pressed;
+    int row, col, errno = 0, round = 0, player = 0, key_pressed = 0;
     bool winning_condition = false;
     bool loop_exit_condition = false;
-    const int str_max_length = 255;
+    const int str_max_length = 50;
     /* FIXME: make a strings handling library */
     char* top_msg = malloc(str_max_length * sizeof(char));
     char* back_msg = malloc(str_max_length * sizeof(char));
     char* exit_msg = malloc(str_max_length * sizeof(char));
 
     initscr();
+    if (has_colors() == false) {
+        endwin();
+        printf("Votre terminal ne supporte pas les couleurs.\n");
+        exit(EXIT_FAILURE);
+    }
+    start_color();
     getmaxyx(stdscr, row, col);
     noecho();
     curs_set(0);
 
     /* array of the active coordinates in the entered order */
-    coordinates_t coordinates_array[MAX_COORDINATES];
-    init_coordinates(coordinates_array);
+    coordinates_t coordinates_array[MAX_COORDINATES] = {
+        {0, 0, 0},
+        {0, 0, 0},
+        {0, 0, 0},
+        {0, 0, 0},
+        {0, 0, 0},
+        {0, 0, 0},
+        {0, 0, 0},
+        {0, 0, 0},
+        {0, 0, 0}
+    };
     coordinates_t new_coordinates = {0, 0, 0};
 
     /* center base coordinates for the board */
     int base_y = row/2 - 4;
     int base_x = col/2 - 7;
 
-    print_board(base_y, base_x);
-
     do {
-        if (errno == 0) round++;
-
-        if (round % 2 == 0) {
+        if (round % 2 != 0) {
             player = 1;
             top_msg = "Joueur 2 joue";
         } else {
@@ -45,6 +56,8 @@ int main() {
 
         mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg);
 
+        print_board(base_y, base_x);
+
         print_coordinates(coordinates_array, base_y, base_x);
 
         /* FIXME: group the winning case code blocks */
@@ -88,11 +101,15 @@ int main() {
             winning_condition = chk_win_conditions(coordinates_array, round);
         }
 
+        if (errno == 0) {
+            round++;
+        }
+
         if (winning_condition) {
             if (player == 0) {
-                back_msg = "Joureur 1 gagne !";
+                back_msg = "Joueur 1 gagne !";
             } else {
-                back_msg = "Joureur 2 gagne !";
+                back_msg = "Joueur 2 gagne !";
             }
         }
 
@@ -106,8 +123,9 @@ int main() {
             } else if (errno == 4) {
                 back_msg = "Erreur inconnue";
             } else if (errno == 0) {
-                /* FIXME: properly zero the string */
+                /* FIXME: properly zero the string to avoid the clear() */
                 back_msg = "";
+                clear();
             }
         }
 
@@ -116,18 +134,29 @@ int main() {
         if (winning_condition || errno == 1) {
             /* print the updated coordinates before exiting */
             print_coordinates(coordinates_array, base_y, base_x);
-            exit_msg = "Pressez une touche pour sortir";
+            exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer";
             mvprintw(base_y + 12, (base_x + 7 - strlen(exit_msg)/2), exit_msg);
             loop_exit_condition = true;
+            if (loop_exit_condition) {
+                int key_exit = 0;
+                /* getch() is blocking */
+                key_exit = getch();
+                if (key_exit == 'r') {
+                    round = 0;
+                    player = 0;
+                    errno = 0;
+                    zero_coordinates(coordinates_array);
+                    winning_condition = false;
+                    loop_exit_condition = false;
+                    clear();
+                }
+            }
         }
 
         refresh();
 
     } while (!loop_exit_condition);
 
-    /* getch() is blocking */
-    getch();
-
     endwin();
 
     if (!top_msg)