Implement :
[Project_algorithmic_C.git] / src / main.c
index ead55ef8004ee9c018a5ace3fbb72c82cb1d5cb5..6a56eed094f9393d6c8a598af3873e370a6b7f52 100644 (file)
 #include <ncurses.h>
 
 #include "ui.h"
+#include "othello.h"
+#include "debug.h"
 
 int main() {
+    int min_y = 26 + 6, min_x = 42 + 14 + 15 + 4;
     int row = 0, col = 0;
-    int key_pressed = 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;
+
+    char* title_msg = "Jeu Othello";
+    char* score_msg = "Pions %s: %d";
+    char* player_msg = "Joueur %d (%s) joue !";
+    char* winner_msg = "Joueur %d (%s) gagne !";
+    char* draw_msg = "Egalite !";
+
+    unsigned int pawns[board_size][board_size] = {{}};
+    pawns[board_size][board_size] = 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);
-    noecho();
+    if (row < min_y || col < min_x) {
+        endwin();
+        printf("Votre terminal est trop petit pour afficher le jeu.\n");
+        printf("Merci d'agrandir la fenetre de votre terminal.\n");
+        exit(EXIT_FAILURE);
+    }
+    /* FIXME: fail if the screen size is too small */
+    echo();
     curs_set(0);
 
-    /* center the board */
-    int center_board_y = row/2 - 23/2;
-    int center_board_x = col/2 - 40/2;
+    /* center */
+    int center_y = row/2;
+    int center_x = col/2;
+    /* base coordinates to center the board */
+    int board_center_y = center_y - 26/2;
+    int board_center_x = center_x - 42/2;
 
     do {
-        print_board(center_board_y, center_board_x);
+        print_board(board_center_y, board_center_x);
+        print_pawns(board_center_y, board_center_x, pawns);
+
+        mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg);
 
-        key_pressed = getch();
-        if (key_pressed == 'q') {
+        player = current_player(round);
+
+        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_pawn_type(pawns, white);
+        nb_black = count_pawn_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);
+
+        int y;
+        char x;
+        bool input_ok = false;
+        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);
+            if (is_valid_input(y, map_col_letter_to_index(x), pawns) && prmt_rt == 1) {
+                input_ok = true;
+            }
+        } while (!input_ok);
+        pawns[board_size][board_size] = set_pawn(y, map_col_letter_to_index(x), player, pawns);
+        struct shots_history_list_s shots_history;
+        INIT_LIST_HEAD(&shots_history.list);
+        struct shots_history_list_s shots_elmt; 
+        shots_elmt.pawn_array_member = &pawns[y-1][x-1];
+        list_add(&shots_elmt.list, &shots_history.list);
+
+        round++; /* increment the round count */
+
+        refresh();
+
+        /* 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) {
+                if (winner == player_one) {
+                    mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "noir"), winner_msg, winner, "noir");
+                } else {
+                    mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "blanc"), winner_msg, winner, "blanc");
+                }
+            } else {
+                mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg);
+            }
+            /* print and implement restart possibility */
             exit_condition = true;
         }
-    } while (!exit_condition);
 
+    } while (!exit_condition);
+    
     endwin();
 
     exit(EXIT_SUCCESS);