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