Be more explicit in Makefiles about external libraries linking
[TD_C.git] / TP_13 / exo2 / src / main.c
index 390bc40005122a294846808868852871354b1235..bdc1ccd881172d9e3c206cd493e9001a6054e05d 100644 (file)
@@ -7,31 +7,46 @@
 #include "coordinates.h"
 
 int main() {
-    int row, col, errno = 0, round = 0, player = 0, key_pressed;
-    const int str_max_length = 255;
+    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 = 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 {
@@ -41,71 +56,115 @@ 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);
 
-        /* getch() is blocking */
-        key_pressed = getch();
-        switch (key_pressed) {
-            case 'a':
-                new_coordinates = set_coordinates(1, 1, player);
-                break;
-            case 'z':
-                new_coordinates = set_coordinates(1, 2, player);
-                break;
-            case 'e':
-                new_coordinates = set_coordinates(1, 3, player);
-                break;
-            case 'q':
-                new_coordinates = set_coordinates(2, 1, player);
-                break;
-            case 's':
-                new_coordinates = set_coordinates(2, 2, player);
-                break;
-            case 'd':
-                new_coordinates = set_coordinates(2, 3, player);
-                break;
-            case 'w':
-                new_coordinates = set_coordinates(3, 1, player);
-                break;
-            case 'x':
-                new_coordinates = set_coordinates(3, 2, player);
-                break;
-            case 'c':
-                new_coordinates = set_coordinates(3, 3, player);
-                break;
-            default:
-                /* set invalid coordinates */
-                new_coordinates = set_coordinates(0, 0, player);
-                break;
+        /* FIXME: group the winning case code blocks */
+        if (!winning_condition) {
+            /* getch() is blocking */
+            key_pressed = getch();
+            switch (key_pressed) {
+                case 'a':
+                    new_coordinates = set_coordinates(1, 1, player);
+                    break;
+                case 'z':
+                    new_coordinates = set_coordinates(1, 2, player);
+                    break;
+                case 'e':
+                    new_coordinates = set_coordinates(1, 3, player);
+                    break;
+                case 'q':
+                    new_coordinates = set_coordinates(2, 1, player);
+                    break;
+                case 's':
+                    new_coordinates = set_coordinates(2, 2, player);
+                    break;
+                case 'd':
+                    new_coordinates = set_coordinates(2, 3, player);
+                    break;
+                case 'w':
+                    new_coordinates = set_coordinates(3, 1, player);
+                    break;
+                case 'x':
+                    new_coordinates = set_coordinates(3, 2, player);
+                    break;
+                case 'c':
+                    new_coordinates = set_coordinates(3, 3, player);
+                    break;
+                default:
+                    /* set invalid coordinates */
+                    new_coordinates = set_coordinates(0, 0, player);
+                    break;
+            }
+            errno = add_coordinates(new_coordinates, coordinates_array, round);
+            winning_condition = chk_win_conditions(coordinates_array, round);
+        }
+
+        if (errno == 0) {
+            round++;
+        }
+
+        if (winning_condition) {
+            if (player == 0) {
+                back_msg = "Joueur 1 gagne !";
+            } else {
+                back_msg = "Joueur 2 gagne !";
+            }
         }
 
-        errno = add_coordinates(new_coordinates, coordinates_array, round);
-
-        if (errno == 2) {
-            back_msg = "Choisir une case vide";
-        } else if (errno == 3) {
-            back_msg = "Coordonnees invalides";
-        } else if (errno == 1) {
-            back_msg = "Tableau rempli sans gagnant: egalite";
-        } else if (errno == 4) {
-            back_msg = "Erreur inconnue";
-        } else if (errno == 0) {
-            /* FIXME: properly zero the string */
-            back_msg = "";
+        if (!winning_condition) {
+            if (errno == 2) {
+                back_msg = "Choisir une case vide";
+            } else if (errno == 3) {
+                back_msg = "Coordonnees invalides";
+            } else if (errno == 1) {
+                back_msg = "Tableau rempli sans gagnant: egalite";
+            } else if (errno == 4) {
+                back_msg = "Erreur inconnue";
+            } else if (errno == 0) {
+                /* FIXME: properly zero the string to avoid the clear() */
+                back_msg = "";
+                clear();
+            }
         }
 
         mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg);
 
+        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 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 (errno != 1);
+    } while (!loop_exit_condition);
+
+    endwin();
 
     if (!top_msg)
         free(top_msg);
     if (!back_msg)
         free(back_msg);
-
-    endwin();
+    if (!exit_msg)
+        free(exit_msg);
 
     exit(EXIT_SUCCESS);
 }