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(); | |
5df3071e | 20 | getmaxyx(stdscr, row, col); |
7515f897 JB |
21 | noecho(); |
22 | curs_set(0); | |
23 | ||
5df3071e | 24 | /* array of the active coordinates in the entered order */ |
9c2b41f4 JB |
25 | coordinates_t coordinates_array[MAX_COORDINATES] = { |
26 | {0, 0, 0}, | |
27 | {0, 0, 0}, | |
28 | {0, 0, 0}, | |
29 | {0, 0, 0}, | |
30 | {0, 0, 0}, | |
31 | {0, 0, 0}, | |
32 | {0, 0, 0}, | |
33 | {0, 0, 0}, | |
34 | {0, 0, 0} | |
35 | }; | |
2737ed7c | 36 | coordinates_t new_coordinates = {0, 0, 0}; |
5df3071e | 37 | |
7515f897 | 38 | /* center base coordinates for the board */ |
5df3071e JB |
39 | int base_y = row/2 - 4; |
40 | int base_x = col/2 - 7; | |
7515f897 | 41 | |
2737ed7c | 42 | do { |
a177f80c | 43 | if (round % 2 != 0) { |
2737ed7c JB |
44 | player = 1; |
45 | top_msg = "Joueur 2 joue"; | |
04b0afb5 JB |
46 | } else { |
47 | player = 0; | |
48 | top_msg = "Joueur 1 joue"; | |
2737ed7c | 49 | } |
7515f897 | 50 | |
2737ed7c | 51 | mvprintw(base_y - 2, (base_x + 7 - strlen(top_msg)/2), top_msg); |
7515f897 | 52 | |
a177f80c JB |
53 | print_board(base_y, base_x); |
54 | ||
2737ed7c JB |
55 | print_coordinates(coordinates_array, base_y, base_x); |
56 | ||
811d4abe JB |
57 | /* FIXME: group the winning case code blocks */ |
58 | if (!winning_condition) { | |
59 | /* getch() is blocking */ | |
60 | key_pressed = getch(); | |
61 | switch (key_pressed) { | |
62 | case 'a': | |
63 | new_coordinates = set_coordinates(1, 1, player); | |
64 | break; | |
65 | case 'z': | |
66 | new_coordinates = set_coordinates(1, 2, player); | |
67 | break; | |
68 | case 'e': | |
69 | new_coordinates = set_coordinates(1, 3, player); | |
70 | break; | |
71 | case 'q': | |
72 | new_coordinates = set_coordinates(2, 1, player); | |
73 | break; | |
74 | case 's': | |
75 | new_coordinates = set_coordinates(2, 2, player); | |
76 | break; | |
77 | case 'd': | |
78 | new_coordinates = set_coordinates(2, 3, player); | |
79 | break; | |
80 | case 'w': | |
81 | new_coordinates = set_coordinates(3, 1, player); | |
82 | break; | |
83 | case 'x': | |
84 | new_coordinates = set_coordinates(3, 2, player); | |
85 | break; | |
86 | case 'c': | |
87 | new_coordinates = set_coordinates(3, 3, player); | |
88 | break; | |
89 | default: | |
90 | /* set invalid coordinates */ | |
91 | new_coordinates = set_coordinates(0, 0, player); | |
92 | break; | |
93 | } | |
94 | errno = add_coordinates(new_coordinates, coordinates_array, round); | |
95 | winning_condition = chk_win_conditions(coordinates_array, round); | |
2737ed7c JB |
96 | } |
97 | ||
a177f80c JB |
98 | if (errno == 0) { |
99 | round++; | |
100 | } | |
101 | ||
811d4abe JB |
102 | if (winning_condition) { |
103 | if (player == 0) { | |
a177f80c | 104 | back_msg = "Joueur 1 gagne !"; |
811d4abe | 105 | } else { |
a177f80c | 106 | back_msg = "Joueur 2 gagne !"; |
811d4abe JB |
107 | } |
108 | } | |
109 | ||
110 | if (!winning_condition) { | |
111 | if (errno == 2) { | |
112 | back_msg = "Choisir une case vide"; | |
113 | } else if (errno == 3) { | |
114 | back_msg = "Coordonnees invalides"; | |
115 | } else if (errno == 1) { | |
116 | back_msg = "Tableau rempli sans gagnant: egalite"; | |
117 | } else if (errno == 4) { | |
118 | back_msg = "Erreur inconnue"; | |
119 | } else if (errno == 0) { | |
a177f80c | 120 | /* FIXME: properly zero the string to avoid the clear() */ |
811d4abe | 121 | back_msg = ""; |
a177f80c | 122 | clear(); |
811d4abe | 123 | } |
2737ed7c JB |
124 | } |
125 | ||
04b0afb5 JB |
126 | mvprintw(base_y + 10, (base_x + 7 - strlen(back_msg)/2), back_msg); |
127 | ||
811d4abe JB |
128 | if (winning_condition || errno == 1) { |
129 | /* print the updated coordinates before exiting */ | |
130 | print_coordinates(coordinates_array, base_y, base_x); | |
a177f80c | 131 | exit_msg = "Pressez une touche pour sortir ou \'r\' pour rejouer"; |
811d4abe JB |
132 | mvprintw(base_y + 12, (base_x + 7 - strlen(exit_msg)/2), exit_msg); |
133 | loop_exit_condition = true; | |
a177f80c JB |
134 | if (loop_exit_condition) { |
135 | int key_exit = 0; | |
136 | /* getch() is blocking */ | |
137 | key_exit = getch(); | |
138 | if (key_exit == 'r') { | |
139 | round = 0; | |
140 | player = 0; | |
141 | errno = 0; | |
9c2b41f4 | 142 | zero_coordinates(coordinates_array); |
a177f80c JB |
143 | winning_condition = false; |
144 | loop_exit_condition = false; | |
145 | clear(); | |
146 | } | |
147 | } | |
811d4abe JB |
148 | } |
149 | ||
04b0afb5 JB |
150 | refresh(); |
151 | ||
811d4abe JB |
152 | } while (!loop_exit_condition); |
153 | ||
811d4abe | 154 | endwin(); |
2737ed7c | 155 | |
04b0afb5 JB |
156 | if (!top_msg) |
157 | free(top_msg); | |
158 | if (!back_msg) | |
159 | free(back_msg); | |
811d4abe JB |
160 | if (!exit_msg) |
161 | free(exit_msg); | |
7515f897 JB |
162 | |
163 | exit(EXIT_SUCCESS); | |
164 | } |