7 #include "coordinates.h"
10 int row
, col
, errno
= 0, round
= 0, player
= 0, key_pressed
;
11 bool winning_condition
= false;
12 bool loop_exit_condition
= false;
13 const int str_max_length
= 255;
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 getmaxyx(stdscr
, row
, col
);
24 /* array of the active coordinates in the entered order */
25 coordinates_t coordinates_array
[MAX_COORDINATES
];
26 init_coordinates(coordinates_array
);
27 coordinates_t new_coordinates
= {0, 0, 0};
29 /* center base coordinates for the board */
30 int base_y
= row
/2 - 4;
31 int base_x
= col
/2 - 7;
33 print_board(base_y
, base_x
);
36 if (errno
== 0) round
++;
40 top_msg
= "Joueur 2 joue";
43 top_msg
= "Joueur 1 joue";
46 mvprintw(base_y
- 2, (base_x
+ 7 - strlen(top_msg
)/2), top_msg
);
48 print_coordinates(coordinates_array
, base_y
, base_x
);
50 /* FIXME: group the winning case code blocks */
51 if (!winning_condition
) {
52 /* getch() is blocking */
53 key_pressed
= getch();
54 switch (key_pressed
) {
56 new_coordinates
= set_coordinates(1, 1, player
);
59 new_coordinates
= set_coordinates(1, 2, player
);
62 new_coordinates
= set_coordinates(1, 3, player
);
65 new_coordinates
= set_coordinates(2, 1, player
);
68 new_coordinates
= set_coordinates(2, 2, player
);
71 new_coordinates
= set_coordinates(2, 3, player
);
74 new_coordinates
= set_coordinates(3, 1, player
);
77 new_coordinates
= set_coordinates(3, 2, player
);
80 new_coordinates
= set_coordinates(3, 3, player
);
83 /* set invalid coordinates */
84 new_coordinates
= set_coordinates(0, 0, player
);
87 errno
= add_coordinates(new_coordinates
, coordinates_array
, round
);
88 winning_condition
= chk_win_conditions(coordinates_array
, round
);
91 if (winning_condition
) {
93 back_msg
= "Joureur 1 gagne !";
95 back_msg
= "Joureur 2 gagne !";
99 if (!winning_condition
) {
101 back_msg
= "Choisir une case vide";
102 } else if (errno
== 3) {
103 back_msg
= "Coordonnees invalides";
104 } else if (errno
== 1) {
105 back_msg
= "Tableau rempli sans gagnant: egalite";
106 } else if (errno
== 4) {
107 back_msg
= "Erreur inconnue";
108 } else if (errno
== 0) {
109 /* FIXME: properly zero the string */
114 mvprintw(base_y
+ 10, (base_x
+ 7 - strlen(back_msg
)/2), back_msg
);
116 if (winning_condition
|| errno
== 1) {
117 /* print the updated coordinates before exiting */
118 print_coordinates(coordinates_array
, base_y
, base_x
);
119 exit_msg
= "Pressez une touche pour sortir";
120 mvprintw(base_y
+ 12, (base_x
+ 7 - strlen(exit_msg
)/2), exit_msg
);
121 loop_exit_condition
= true;
126 } while (!loop_exit_condition
);
128 /* getch() is blocking */