Commit | Line | Data |
---|---|---|
7515f897 JB |
1 | #include <stdlib.h> |
2 | #include <stdio.h> | |
5df3071e | 3 | #include <string.h> |
7515f897 JB |
4 | #include <ncurses.h> |
5 | ||
6 | #include "display.h" | |
5df3071e | 7 | #include "coordinates.h" |
7515f897 JB |
8 | |
9 | int main() { | |
a177f80c | 10 | int row, col, errno = 0, round = 0, player = 0, key_pressed = 0; |
811d4abe JB |
11 | bool winning_condition = false; |
12 | bool loop_exit_condition = false; | |
a177f80c | 13 | const int str_max_length = 50; |
811d4abe | 14 | /* FIXME: make a strings handling library */ |
04b0afb5 JB |
15 | char* top_msg = malloc(str_max_length * sizeof(char)); |
16 | char* back_msg = malloc(str_max_length * sizeof(char)); | |
811d4abe | 17 | char* exit_msg = malloc(str_max_length * sizeof(char)); |
7515f897 JB |
18 | |
19 | initscr(); | |
98edfbe1 JB |
20 | if (has_colors() == false) { |
21 | endwin(); | |
22 | printf("Votre terminal ne supporte pas les couleurs.\n"); | |
23 | exit(EXIT_FAILURE); | |
24 | } | |
25 | start_color(); | |
5df3071e | 26 | getmaxyx(stdscr, row, col); |
7515f897 JB |
27 | noecho(); |
28 | curs_set(0); | |
29 | ||
5df3071e | 30 | /* array of the active coordinates in the entered order */ |
9c2b41f4 JB |
31 | coordinates_t coordinates_array[MAX_COORDINATES] = { |
32 | {0, 0, 0}, | |
33 | {0, 0, 0}, | |
34 | {0, 0, 0}, | |
35 | {0, 0, 0}, | |
36 | {0, 0, 0}, | |
37 | {0, 0, 0}, | |
38 | {0, 0, 0}, | |
39 | {0, 0, 0}, | |
40 | {0, 0, 0} | |
41 | }; | |
2737ed7c | 42 | coordinates_t new_coordinates = {0, 0, 0}; |
5df3071e | 43 | |
7515f897 | 44 | /* center base coordinates for the board */ |
5df3071e JB |
45 | int base_y = row/2 - 4; |
46 | int base_x = col/2 - 7; | |
7515f897 | 47 | |
2737ed7c | 48 | do { |
a177f80c | 49 | if (round % 2 != 0) { |
2737ed7c JB |
50 | player = 1; |
51 | top_msg = "Joueur 2 joue"; | |
04b0afb5 JB |
52 | } else { |
53 | player = 0; | |
54 | top_msg = "Joueur 1 joue"; | |
2737ed7c | 55 | } |
7515f897 | 56 | |
2737ed7c | 57 | mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg); |
7515f897 | 58 | |
a177f80c JB |
59 | print_board(base_y, base_x); |
60 | ||
2737ed7c JB |
61 | print_coordinates(coordinates_array, base_y, base_x); |
62 | ||
811d4abe JB |
63 | /* FIXME: group the winning case code blocks */ |
64 | if (!winning_condition) { | |
65 | /* getch() is blocking */ | |
66 | key_pressed = getch(); | |
67 | switch (key_pressed) { | |
68 | case 'a': | |
69 | new_coordinates = set_coordinates(1, 1, player); | |
70 | break; | |
71 | case 'z': | |
72 | new_coordinates = set_coordinates(1, 2, player); | |
73 | break; | |
74 | case 'e': | |
75 | new_coordinates = set_coordinates(1, 3, player); | |
76 | break; | |
77 | case 'q': | |
78 | new_coordinates = set_coordinates(2, 1, player); | |
79 | break; | |
80 | case 's': | |
81 | new_coordinates = set_coordinates(2, 2, player); | |
82 | break; | |
83 | case 'd': | |
84 | new_coordinates = set_coordinates(2, 3, player); | |
85 | break; | |
86 | case 'w': | |
87 | new_coordinates = set_coordinates(3, 1, player); | |
88 | break; | |
89 | case 'x': | |
90 | new_coordinates = set_coordinates(3, 2, player); | |
91 | break; | |
92 | case 'c': | |
93 | new_coordinates = set_coordinates(3, 3, player); | |
94 | break; | |
95 | default: | |
96 | /* set invalid coordinates */ | |
97 | new_coordinates = set_coordinates(0, 0, player); | |
98 | break; | |
99 | } | |
100 | errno = add_coordinates(new_coordinates, coordinates_array, round); | |
101 | winning_condition = chk_win_conditions(coordinates_array, round); | |
2737ed7c JB |
102 | } |
103 | ||
a177f80c JB |
104 | if (errno == 0) { |
105 | round++; | |
106 | } | |
107 | ||
811d4abe JB |
108 | if (winning_condition) { |
109 | if (player == 0) { | |
a177f80c | 110 | back_msg = "Joueur 1 gagne !"; |
811d4abe | 111 | } else { |
a177f80c | 112 | back_msg = "Joueur 2 gagne !"; |
811d4abe JB |
113 | } |
114 | } | |
115 | ||
116 | if (!winning_condition) { | |
117 | if (errno == 2) { | |
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) { | |
a177f80c | 126 | /* FIXME: properly zero the string to avoid the clear() */ |
811d4abe | 127 | back_msg = ""; |
a177f80c | 128 | clear(); |
811d4abe | 129 | } |
2737ed7c JB |
130 | } |
131 | ||
04b0afb5 JB |
132 | mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg); |
133 | ||
811d4abe JB |
134 | if (winning_condition || errno == 1) { |
135 | /* print the updated coordinates before exiting */ | |
136 | print_coordinates(coordinates_array, base_y, base_x); | |
a177f80c | 137 | exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer"; |
811d4abe JB |
138 | mvprintw(base_y + 12, (base_x + 7 - strlen(exit_msg)/2), exit_msg); |
139 | loop_exit_condition = true; | |
a177f80c JB |
140 | if (loop_exit_condition) { |
141 | int key_exit = 0; | |
142 | /* getch() is blocking */ | |
143 | key_exit = getch(); | |
144 | if (key_exit == 'r') { | |
145 | round = 0; | |
146 | player = 0; | |
147 | errno = 0; | |
9c2b41f4 | 148 | zero_coordinates(coordinates_array); |
a177f80c JB |
149 | winning_condition = false; |
150 | loop_exit_condition = false; | |
151 | clear(); | |
152 | } | |
153 | } | |
811d4abe JB |
154 | } |
155 | ||
04b0afb5 JB |
156 | refresh(); |
157 | ||
811d4abe JB |
158 | } while (!loop_exit_condition); |
159 | ||
811d4abe | 160 | endwin(); |
2737ed7c | 161 | |
04b0afb5 JB |
162 | if (!top_msg) |
163 | free(top_msg); | |
164 | if (!back_msg) | |
165 | free(back_msg); | |
811d4abe JB |
166 | if (!exit_msg) |
167 | free(exit_msg); | |
7515f897 JB |
168 | |
169 | exit(EXIT_SUCCESS); | |
170 | } |