7 #include "coordinates.h"
10 int row
, col
, errno
= 0, round
= 0, player
= 0, key_pressed
= 0;
11 bool winning_condition
= false;
12 bool loop_exit_condition
= false;
13 const int str_max_length
= 50;
14 /* FIXME: make a strings handling library */
15 char* top_msg
= malloc(str_max_length
* sizeof(char));
16 char* back_msg
= malloc(str_max_length
* sizeof(char));
17 char* exit_msg
= malloc(str_max_length
* sizeof(char));
20 if (has_colors() == false) {
22 printf("Votre terminal ne supporte pas les couleurs.\n");
26 getmaxyx(stdscr
, row
, col
);
30 /* array of the active coordinates in the entered order */
31 coordinates_t coordinates_array
[MAX_COORDINATES
] = {
42 coordinates_t new_coordinates
= {0, 0, 0};
44 /* center base coordinates for the board */
45 int base_y
= row
/2 - 4;
46 int base_x
= col
/2 - 7;
51 top_msg
= "Joueur 2 joue";
54 top_msg
= "Joueur 1 joue";
57 mvprintw(base_y
- 2, (base_x
+ 7 - strlen(top_msg
)/2), top_msg
);
59 print_board(base_y
, base_x
);
61 print_coordinates(coordinates_array
, base_y
, base_x
);
63 /* FIXME: group the winning case code blocks */
64 if (!winning_condition
) {
65 /* getch() is blocking */
66 key_pressed
= getch();
67 switch (key_pressed
) {
69 new_coordinates
= set_coordinates(1, 1, player
);
72 new_coordinates
= set_coordinates(1, 2, player
);
75 new_coordinates
= set_coordinates(1, 3, player
);
78 new_coordinates
= set_coordinates(2, 1, player
);
81 new_coordinates
= set_coordinates(2, 2, player
);
84 new_coordinates
= set_coordinates(2, 3, player
);
87 new_coordinates
= set_coordinates(3, 1, player
);
90 new_coordinates
= set_coordinates(3, 2, player
);
93 new_coordinates
= set_coordinates(3, 3, player
);
96 /* set invalid coordinates */
97 new_coordinates
= set_coordinates(0, 0, player
);
100 errno
= add_coordinates(new_coordinates
, coordinates_array
, round
);
101 winning_condition
= chk_win_conditions(coordinates_array
, round
);
108 if (winning_condition
) {
110 back_msg
= "Joueur 1 gagne !";
112 back_msg
= "Joueur 2 gagne !";
116 if (!winning_condition
) {
118 back_msg
= "Choisir une case vide";
119 } else if (errno
== 3) {
120 back_msg
= "Coordonnees invalides";
121 } else if (errno
== 1) {
122 back_msg
= "Tableau rempli sans gagnant: egalite";
123 } else if (errno
== 4) {
124 back_msg
= "Erreur inconnue";
125 } else if (errno
== 0) {
126 /* FIXME: properly zero the string to avoid the clear() */
132 mvprintw(base_y
+ 10, (base_x
+ 7 - strlen(back_msg
)/2), back_msg
);
134 if (winning_condition
|| errno
== 1) {
135 /* print the updated coordinates before exiting */
136 print_coordinates(coordinates_array
, base_y
, base_x
);
137 exit_msg
= "Pressez une touche pour sortir ou \'r\' pour rejouer";
138 mvprintw(base_y
+ 12, (base_x
+ 7 - strlen(exit_msg
)/2), exit_msg
);
139 loop_exit_condition
= true;
140 if (loop_exit_condition
) {
142 /* getch() is blocking */
144 if (key_exit
== 'r') {
148 zero_coordinates(coordinates_array
);
149 winning_condition
= false;
150 loop_exit_condition
= false;
158 } while (!loop_exit_condition
);