Properly implement the validation of a shot and reverse or flip the
[Project_algorithmic_C.git] / src / main.c
CommitLineData
4ddf6f1a
JB
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <ncurses.h>
5
6#include "ui.h"
2e5c1894 7#include "othello.h"
54f1c58c 8#include "debug.h"
4ddf6f1a
JB
9
10int main() {
45ce2fe3 11 int min_y = 26 + 6, min_x = 42 + 14 + 15 + 4;
4ddf6f1a 12 int row = 0, col = 0;
74e2b93b
JB
13 unsigned int round = 0;
14 unsigned int player = player_one; /* first player is black */
4ddf6f1a 15 bool exit_condition = false;
74e2b93b
JB
16 unsigned int nb_white = 0, nb_black = 0;
17
a8b54576 18 char* title_msg = "Jeu Othello";
45ce2fe3
JB
19 char* score_msg = "Pions %s: %d";
20 char* player_msg = "Joueur %d (%s) joue !";
21 char* winner_msg = "Joueur %d (%s) gagne !";
a8b54576 22 char* draw_msg = "Egalite !";
a80646b7
JB
23
24 /* linked list of the history shots */
25 //struct shots_history_list_s shots_history;
26 //INIT_LIST_HEAD(&shots_history.list);
27 /* linked list of black playable shots */
28 struct shots_list_s black_playable_shots;
29 INIT_LIST_HEAD(&black_playable_shots.list);
30 /* linked list of white playable shots */
31 struct shots_list_s white_playable_shots;
32 INIT_LIST_HEAD(&white_playable_shots.list);
33
34 unsigned int pawns[board_size][board_size] = {
35 {0, 0},
36 {0, 0}
37 };
38 init_pawns(pawns);
4ddf6f1a
JB
39
40 initscr();
41 if (has_colors() == false) {
42 endwin();
45ce2fe3 43 printf("Votre terminal ne supporte pas les couleurs.\n");
4ddf6f1a
JB
44 exit(EXIT_FAILURE);
45 }
46 start_color();
47 getmaxyx(stdscr, row, col);
45ce2fe3
JB
48 if (row < min_y || col < min_x) {
49 endwin();
a80646b7 50 printf("Votre terminal est trop petit pour afficher ce jeu.\n");
45ce2fe3
JB
51 printf("Merci d'agrandir la fenetre de votre terminal.\n");
52 exit(EXIT_FAILURE);
53 }
74e2b93b 54 echo();
4ddf6f1a
JB
55 curs_set(0);
56
74e2b93b
JB
57 /* center */
58 int center_y = row/2;
59 int center_x = col/2;
60 /* base coordinates to center the board */
61 int board_center_y = center_y - 26/2;
62 int board_center_x = center_x - 42/2;
4ddf6f1a
JB
63
64 do {
74e2b93b
JB
65 print_board(board_center_y, board_center_x);
66 print_pawns(board_center_y, board_center_x, pawns);
67
74e2b93b 68 mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg);
4ddf6f1a 69
a8b54576
JB
70 player = current_player(round);
71
45ce2fe3
JB
72 if (player == player_one) {
73 mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, player_msg, player, "noir")/2, player_msg, player, "noir");
74 } else {
75 mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, player_msg, player, "blanc")/2, player_msg, player, "blanc");
76 }
a8b54576 77
a80646b7
JB
78 nb_white = count_pawns_type(pawns, white);
79 nb_black = count_pawns_type(pawns, black);
74e2b93b 80
45ce2fe3
JB
81 mvprintw(center_y, center_x - 42/2 - snprintf(NULL, 0, score_msg, "noirs", nb_black) - 2, score_msg, "noirs", nb_black);
82 mvprintw(center_y, center_x + 42/2 + 2, score_msg, "blancs", nb_white);
a80646b7
JB
83
84 display_array(1, 1, pawns);
74e2b93b 85
74e2b93b 86 int y;
a80646b7 87 char x_char;
74e2b93b
JB
88 bool input_ok = false;
89 do {
90 y = 0;
a80646b7 91 x_char = "";
54f1c58c 92 char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):";
a80646b7
JB
93 int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, center_x - strlen(prompt_msg)/2, prompt_msg, &y, &x_char);
94 int x = map_col_letter_to_index(x_char);
95 if (valid_shot(y, x, player, pawns) > 0 && prmt_rt == 1) {
74e2b93b
JB
96 input_ok = true;
97 }
98 } while (!input_ok);
74e2b93b
JB
99
100 round++; /* increment the round count */
101
a8b54576 102 /* here are all the end of the game conditions */
a8b54576 103 if (is_board_full(pawns)) {
a80646b7 104 unsigned int winner = eval_winner(nb_white, nb_black);
a8b54576 105 if (winner != 0) {
45ce2fe3
JB
106 if (winner == player_one) {
107 mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "noir"), winner_msg, winner, "noir");
108 } else {
109 mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "blanc"), winner_msg, winner, "blanc");
110 }
a8b54576
JB
111 } else {
112 mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg);
113 }
114 /* print and implement restart possibility */
54f1c58c
JB
115 exit_condition = true;
116 }
a80646b7
JB
117
118 refresh();
54f1c58c 119
74e2b93b
JB
120 } while (!exit_condition);
121
4ddf6f1a
JB
122 endwin();
123
124 exit(EXIT_SUCCESS);
125}