-
Notifications
You must be signed in to change notification settings - Fork 131
/
TicTacToe.c
228 lines (186 loc) · 5.32 KB
/
TicTacToe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32) || defined(_WIN64)
#define WINDOWS
#include <conio.h>
#elif defined(unix)
#define UNIX
char getch(void);
#else
#error "Platform is not supported"
#endif
void clear (void) {
#ifdef WINDOWS
printf("\033[2J");
printf("\033[0;0f");
#else
system("clear");
#endif
}
#ifdef UNIX
char getch (void) {
char ch;
system("stty raw");
ch = getchar();
system("stty cooked");
return ch;
}
#endif
enum {Y = 7, X = 13};
typedef enum {false, true} bool;
enum State { ZERO = 'O', CROSS = 'X' };
static enum State now = CROSS;
static char map[Y][X] = {
"=============",
"| 7 | 8 | 9 |",
"=============",
"| 4 | 5 | 6 |",
"=============",
"| 1 | 2 | 3 |",
"============="
};
#define NUM1 map[5][2]
#define NUM2 map[5][6]
#define NUM3 map[5][10]
#define NUM4 map[3][2]
#define NUM5 map[3][6]
#define NUM6 map[3][10]
#define NUM7 map[1][2]
#define NUM8 map[1][6]
#define NUM9 map[1][10]
#define CHANGE(x) (x = now)
#define GOTO(x) if(x == ZERO || x == CROSS) goto again; else CHANGE(x);
static void menu (void);
static void game (void);
static void printMap (void);
static bool checkWin (void);
static void restart (void);
int main (void) {
menu();
return 0;
}
static void menu (void) {
auto char symbol;
start:
clear();
printf("\v\tPRESS '1' TO START GAME\n");
printf("\tPRESS '0' TO STOP GAME\n");
printf("\t> ");
restart:
symbol = getch();
switch(symbol) {
case '1': game(); break;
case '0': printf("\n\n"); return;
default: goto start;
}
restart();
printf("\v\tPRESS '1' TO PLAY AGAIN\n");
printf("\tPRESS '0' TO STOP GAME\n");
printf("\t> ");
goto restart;
}
static void restart (void) {
now = CROSS;
NUM7 = '7'; NUM8 = '8'; NUM9 = '9';
NUM4 = '4'; NUM5 = '5'; NUM6 = '6';
NUM1 = '1'; NUM2 = '2'; NUM3 = '3';
}
static void game (void) {
auto char symbol;
while(true) {
again:
printMap();
printf("\v\t Number > ");
symbol = getch();
switch(symbol) {
case '0': printf("\n\n"); return;
case '1': GOTO(NUM1); break;
case '2': GOTO(NUM2); break;
case '3': GOTO(NUM3); break;
case '4': GOTO(NUM4); break;
case '5': GOTO(NUM5); break;
case '6': GOTO(NUM6); break;
case '7': GOTO(NUM7); break;
case '8': GOTO(NUM8); break;
case '9': GOTO(NUM9); break;
default: goto again;
}
if (checkWin()) return;
if (now == CROSS) now = ZERO;
else now = CROSS;
}
}
static void printMap (void) {
auto unsigned char x, y;
clear(); printf("\n\v");
for (y = 0; y < 7; y++) {
printf("\t");
for (x = 0; x < 13; x++)
printf("%c", map[y][x]);
printf("\n");
}
}
static bool checkWin (void) {
auto bool win = false;
auto char temp;
/* HORIZONTAL */
if ((NUM7 == ZERO && NUM8 == ZERO && NUM9 == ZERO)||
(NUM7 == CROSS && NUM8 == CROSS && NUM9 == CROSS)
) {
temp = NUM7; NUM7 = NUM8 = NUM9 = '-'; printMap();
printf("\n\t WIN: %s\n\n",(temp == ZERO)?" ZERO":" CROSS");
win = true;
}
if ((NUM4 == ZERO && NUM5 == ZERO && NUM6 == ZERO)||
(NUM4 == CROSS && NUM5 == CROSS && NUM6 == CROSS)
) {
temp = NUM4; NUM4 = NUM5 = NUM6 = '-'; printMap();
printf("\n\t WIN: %s\n\n",(temp == ZERO)?" ZERO":" CROSS");
win = true;
}
if ((NUM1 == ZERO && NUM2 == ZERO && NUM3 == ZERO)||
(NUM1 == CROSS && NUM2 == CROSS && NUM3 == CROSS)
) {
temp = NUM1; NUM1 = NUM2 = NUM3 = '-'; printMap();
printf("\n\t WIN: %s\n\n",(temp == ZERO)?" ZERO":" CROSS");
win = true;
}
/* VERTICAL */
if ((NUM7 == ZERO && NUM4 == ZERO && NUM1 == ZERO)||
(NUM7 == CROSS && NUM4 == CROSS && NUM1 == CROSS)
) {
temp = NUM7; NUM7 = NUM4 = NUM1 = '|'; printMap();
printf("\n\t WIN: %s\n\n",(temp == ZERO)?" ZERO":" CROSS");
win = true;
}
if ((NUM8 == ZERO && NUM5 == ZERO && NUM2 == ZERO)||
(NUM8 == CROSS && NUM5 == CROSS && NUM2 == CROSS)
) {
temp = NUM8; NUM8 = NUM5 = NUM2 = '|'; printMap();
printf("\n\t WIN: %s\n\n",(temp == ZERO)?" ZERO":" CROSS");
win = true;
}
if ((NUM9 == ZERO && NUM6 == ZERO && NUM3 == ZERO)||
(NUM9 == CROSS && NUM6 == CROSS && NUM3 == CROSS)
) {
temp = NUM9; NUM9 = NUM6 = NUM3 = '|'; printMap();
printf("\n\t WIN: %s\n\n",(temp == ZERO)?" ZERO":" CROSS");
win = true;
}
/* DIAGONAL */
if ((NUM7 == ZERO && NUM5 == ZERO && NUM3 == ZERO)||
(NUM7 == CROSS && NUM5 == CROSS && NUM3 == CROSS)
) {
temp = NUM7; NUM7 = NUM5 = NUM3 = '\\'; printMap();
printf("\n\t WIN: %s\n\n",(temp == ZERO)?" ZERO":" CROSS");
win = true;
}
if ((NUM9 == ZERO && NUM5 == ZERO && NUM1 == ZERO)||
(NUM9 == CROSS && NUM5 == CROSS && NUM1 == CROSS)
) {
temp = NUM9; NUM9 = NUM5 = NUM1 = '/'; printMap();
printf("\n\t WIN: %s\n\n",(temp == ZERO)?" ZERO":" CROSS");
win = true;
}
return win;
}