TP_13 exo2: Finish the implementation of a basic tic-tac-toe game.
[TD_C.git] / TP_13 / exo2 / src / main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <ncurses.h>
5
6 #include "display.h"
7 #include "coordinates.h"
8
9 int main() {
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));
18
19 initscr();
20 getmaxyx(stdscr, row, col);
21 noecho();
22 curs_set(0);
23
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};
28
29 /* center base coordinates for the board */
30 int base_y = row/2 - 4;
31 int base_x = col/2 - 7;
32
33 print_board(base_y, base_x);
34
35 do {
36 if (errno == 0) round++;
37
38 if (round % 2 == 0) {
39 player = 1;
40 top_msg = "Joueur 2 joue";
41 } else {
42 player = 0;
43 top_msg = "Joueur 1 joue";
44 }
45
46 mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg);
47
48 print_coordinates(coordinates_array, base_y, base_x);
49
50 /* FIXME: group the winning case code blocks */
51 if (!winning_condition) {
52 /* getch() is blocking */
53 key_pressed = getch();
54 switch (key_pressed) {
55 case 'a':
56 new_coordinates = set_coordinates(1, 1, player);
57 break;
58 case 'z':
59 new_coordinates = set_coordinates(1, 2, player);
60 break;
61 case 'e':
62 new_coordinates = set_coordinates(1, 3, player);
63 break;
64 case 'q':
65 new_coordinates = set_coordinates(2, 1, player);
66 break;
67 case 's':
68 new_coordinates = set_coordinates(2, 2, player);
69 break;
70 case 'd':
71 new_coordinates = set_coordinates(2, 3, player);
72 break;
73 case 'w':
74 new_coordinates = set_coordinates(3, 1, player);
75 break;
76 case 'x':
77 new_coordinates = set_coordinates(3, 2, player);
78 break;
79 case 'c':
80 new_coordinates = set_coordinates(3, 3, player);
81 break;
82 default:
83 /* set invalid coordinates */
84 new_coordinates = set_coordinates(0, 0, player);
85 break;
86 }
87 errno = add_coordinates(new_coordinates, coordinates_array, round);
88 winning_condition = chk_win_conditions(coordinates_array, round);
89 }
90
91 if (winning_condition) {
92 if (player == 0) {
93 back_msg = "Joureur 1 gagne !";
94 } else {
95 back_msg = "Joureur 2 gagne !";
96 }
97 }
98
99 if (!winning_condition) {
100 if (errno == 2) {
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 */
110 back_msg = "";
111 }
112 }
113
114 mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg);
115
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;
122 }
123
124 refresh();
125
126 } while (!loop_exit_condition);
127
128 /* getch() is blocking */
129 getch();
130
131 endwin();
132
133 if (!top_msg)
134 free(top_msg);
135 if (!back_msg)
136 free(back_msg);
137 if (!exit_msg)
138 free(exit_msg);
139
140 exit(EXIT_SUCCESS);
141 }