Finalize the implementation of the playable shots list displaying
[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
21b9239c
JB
18 const char* title_msg = "Jeu Othello";
19 const char* score_msg = "Pions %s: %d";
20 const char* invalid_move_msg = "Coup invalide";
21 const char* player_msg = "Joueur %d (%s) joue !";
22 const char* winner_msg = "Joueur %d (%s) gagne !";
23 const char* draw_msg = "Egalite !";
24 const char* exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer";
25
7aab9e03
JB
26 /* linked list of playable shots */
27 struct shots_list_s playable_shots;
28 INIT_LIST_HEAD(&playable_shots.list);
a80646b7
JB
29
30 unsigned int pawns[board_size][board_size] = {
31 {0, 0},
32 {0, 0}
33 };
34 init_pawns(pawns);
4ddf6f1a
JB
35
36 initscr();
37 if (has_colors() == false) {
38 endwin();
45ce2fe3 39 printf("Votre terminal ne supporte pas les couleurs.\n");
4ddf6f1a
JB
40 exit(EXIT_FAILURE);
41 }
42 start_color();
43 getmaxyx(stdscr, row, col);
45ce2fe3
JB
44 if (row < min_y || col < min_x) {
45 endwin();
a80646b7 46 printf("Votre terminal est trop petit pour afficher ce jeu.\n");
45ce2fe3
JB
47 printf("Merci d'agrandir la fenetre de votre terminal.\n");
48 exit(EXIT_FAILURE);
49 }
74e2b93b 50 echo();
4ddf6f1a
JB
51 curs_set(0);
52
74e2b93b
JB
53 /* center */
54 int center_y = row/2;
55 int center_x = col/2;
56 /* base coordinates to center the board */
57 int board_center_y = center_y - 26/2;
58 int board_center_x = center_x - 42/2;
4ddf6f1a
JB
59
60 do {
74e2b93b
JB
61 print_board(board_center_y, board_center_x);
62 print_pawns(board_center_y, board_center_x, pawns);
63
74e2b93b 64 mvprintw(center_y - 26/2 - 4, (center_x - strlen(title_msg)/2), title_msg);
4ddf6f1a 65
a8b54576
JB
66 player = current_player(round);
67
45ce2fe3
JB
68 if (player == player_one) {
69 mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, player_msg, player, "noir")/2, player_msg, player, "noir");
70 } else {
71 mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, player_msg, player, "blanc")/2, player_msg, player, "blanc");
72 }
a8b54576 73
a80646b7
JB
74 nb_white = count_pawns_type(pawns, white);
75 nb_black = count_pawns_type(pawns, black);
21b9239c 76
45ce2fe3
JB
77 mvprintw(center_y, center_x - 42/2 - snprintf(NULL, 0, score_msg, "noirs", nb_black) - 2, score_msg, "noirs", nb_black);
78 mvprintw(center_y, center_x + 42/2 + 2, score_msg, "blancs", nb_white);
21b9239c 79
9240af1a
JB
80 build_playable_shots_list(player, &playable_shots, pawns);
81 print_shots_list(board_center_y, board_center_x, &playable_shots);
82 free_shots_list(&playable_shots);
83
c8c562de 84 display_array(1, 1, pawns);
74e2b93b 85
74e2b93b 86 int y;
a80646b7 87 char x_char;
74e2b93b 88 bool input_ok = false;
cca9463e 89 unsigned int nb_pawns_reversed = 0;
74e2b93b
JB
90 do {
91 y = 0;
c8c562de 92 x_char = (char)"";
21b9239c 93 const char* prompt_msg = "Prochain pion ? (ligne colonne - chiffre lettre):";
cca9463e 94 int prmt_rt = prompt_values(stdscr, center_y + 26/2 + 1, (center_x - strlen(prompt_msg)/2), prompt_msg, &y, &x_char);
a80646b7 95 int x = map_col_letter_to_index(x_char);
cca9463e
JB
96 nb_pawns_reversed = valid_shot(y, x, player, pawns);
97 if (nb_pawns_reversed > 0 && prmt_rt == 1) {
74e2b93b 98 input_ok = true;
cca9463e
JB
99 clear();
100 } else {
101 mvprintw(center_y + 26/2 + 4, (center_x - strlen(invalid_move_msg)/2), invalid_move_msg);
74e2b93b
JB
102 }
103 } while (!input_ok);
74e2b93b
JB
104
105 round++; /* increment the round count */
106
a8b54576 107 /* here are all the end of the game conditions */
a8b54576 108 if (is_board_full(pawns)) {
cca9463e
JB
109 print_board(board_center_y, board_center_x);
110 /* print the updated pawns before exiting */
111 print_pawns(board_center_y, board_center_x, pawns);
a80646b7 112 unsigned int winner = eval_winner(nb_white, nb_black);
a8b54576 113 if (winner != 0) {
45ce2fe3 114 if (winner == player_one) {
cca9463e 115 mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "noir")/2, winner_msg, winner, "noir");
45ce2fe3 116 } else {
cca9463e 117 mvprintw(center_y - 26/2 - 2, center_x - snprintf(NULL, 0, winner_msg, winner, "blanc")/2, winner_msg, winner, "blanc");
45ce2fe3 118 }
a8b54576
JB
119 } else {
120 mvprintw(center_y - 26/2 - 2, (center_x - strlen(draw_msg)/2), draw_msg);
121 }
cca9463e 122 mvprintw(center_y + 26/2 + 1, (center_x - strlen(exit_msg)/2), exit_msg);
54f1c58c 123 exit_condition = true;
cca9463e
JB
124 /* getch() is blocking */
125 int key_exit = getch();
126 if (key_exit == 'r') {
127 round = 0;
128 player = player_one;
129 nb_white = nb_black = 0;
130 /* FIXME: do not seem properly reset the pawns 2D array */
131 init_pawns(pawns);
132 exit_condition = false;
133 clear();
134 }
54f1c58c 135 }
21b9239c 136
a80646b7 137 refresh();
54f1c58c 138
74e2b93b 139 } while (!exit_condition);
21b9239c 140
4ddf6f1a
JB
141 endwin();
142
143 exit(EXIT_SUCCESS);
144}