-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.c
265 lines (228 loc) · 4.49 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// made by John Corbett 2011
#define USE_TI89
#include <tigcclib.h>
#define px 1
#define po 2
#define EMPTY(x) ((x) == 0)
#define WIN0 ((board[0] == board[1] & board[1] == board[2]) & (board[0] != 0))
#define WIN1 ((board[3] == board[4] & board[4] == board[5]) & (board[3] != 0))
#define WIN2 ((board[6] == board[7] & board[7] == board[8]) & (board[6] != 0))
#define WIN3 ((board[0] == board[3] & board[3] == board[6]) & (board[0] != 0))
#define WIN4 ((board[1] == board[4] & board[4] == board[7]) & (board[1] != 0))
#define WIN5 ((board[2] == board[5] & board[5] == board[8]) & (board[2] != 0))
#define WIN6 ((board[0] == board[4] & board[4] == board[8]) & (board[0] != 0))
#define WIN7 ((board[2] == board[4] & board[4] == board[6]) & (board[2] != 0))
#define WIN (WIN0 | WIN1 | WIN2 | WIN3 | WIN4 | WIN5 | WIN6 | WIN7)
typedef enum {
upperLeft = '7',
upperMiddle,
upperRight,
middleLeft = '4',
middleMiddle,
middleRight,
lowerLeft = '1',
lowerMiddle,
lowerRight
} Position;
int ptoi(Position pos) {
switch (pos) {
case upperLeft:
return 0;
case upperMiddle:
return 1;
case upperRight:
return 2;
case middleLeft:
return 3;
case middleMiddle:
return 4;
case middleRight:
return 5;
case lowerLeft:
return 6;
case lowerMiddle:
return 7;
case lowerRight:
return 8;
break;
default:
break;
}
return 0;
}
const unsigned long X[] = {
0xA0000003,
0x3000000A,
0x0A000030,
0x030000A0,
0x00A00300,
0x00300A00,
0x000A3000,
0x0003A000,
0x0003A000,
0x000A3000,
0x00300A00,
0x00A00300,
0x030000A0,
0x0A000030,
0x3000000A,
0xA0000003
};
const unsigned long O[] = {
0x00008000,
0x00036000,
0x00063000,
0x000C1800,
0x00180C00,
0x00300600,
0x00600300,
0x00C00180,
0x00C00180,
0x00600300,
0x00300600,
0x00180C00,
0x000C1800,
0x00063000,
0x00036000,
0x00008000
};
void drawBoard() {
DrawLine(53, 0, 53, 100, A_NORMAL);
DrawLine(54, 0, 54, 100, A_NORMAL);
DrawLine(52, 0, 52, 100, A_NORMAL);
DrawLine(106, 0, 106, 100, A_NORMAL);
DrawLine(107, 0, 107, 100, A_NORMAL);
DrawLine(105, 0, 105, 100, A_NORMAL);
DrawLine(0, 33, 160, 33, A_NORMAL);
DrawLine(0, 32, 160, 32, A_NORMAL);
DrawLine(0, 31, 160, 31, A_NORMAL);
DrawLine(0, 66, 160, 66, A_NORMAL);
DrawLine(0, 65, 160, 65, A_NORMAL);
DrawLine(0, 64, 160, 64, A_NORMAL);
}
void draw(Position pos, char sprite) {
int x, y;
if ((pos >= upperLeft) & (pos <= upperRight))
y = 8;
else if ((pos >= middleLeft) & (pos <= middleRight))
y = 41;
else if ((pos >= lowerLeft) & (pos <= lowerRight))
y = 74;
switch (pos) {
case upperLeft:
case middleLeft:
case lowerLeft:
x = 10;
break;
case upperMiddle:
case middleMiddle:
case lowerMiddle:
x = 63;
break;
case upperRight:
case middleRight:
case lowerRight:
x = 116;
break;
default:
break;
}
if (sprite == 'x')
Sprite32(x, y, 16, X, LCD_MEM, SPRT_XOR);
else
Sprite32(x, y, 16, O, LCD_MEM, SPRT_XOR);
}
void welcomeScreen() {
puts("Tic-Tac-Toe v1.0
by John Corbett\n
(press any key)");
ngetchx();
clrscr();
}
void _main(void) {
clrscr();
welcomeScreen();
unsigned short retry;
do {
clrscr();
unsigned short board[9];
short player = px;
int i = 0;
for (i = 0; i < 9; i++)
board[i] = 0;
drawBoard();
i = 0;
while (i < 9) {
char playerz;
if (player == px)
playerz = 'x';
else
playerz = 'o';
Position pos;
pos = (Position) ngetchx();
if (EMPTY(board[ptoi(pos)]) & pos >= '1' & pos <= '9') {
draw(pos, playerz);
board[ptoi(pos)] = player;
if (player == px)
player = po;
else
player = px;
i++;
}
unsigned short x1, y1, x2, y2;
if (WIN0) {
x1 = 26;
x2 = 134;
y1 = y2 = 16;
}
else if (WIN1) {
x1 = 26;
x2 = 134;
y1 = y2 = 50;
}
else if (WIN2) {
x1 = 26;
x2 = 134;
y1 = y2 = 84;
}
else if (WIN3) {
x1 = x2 = 26;
y1 = 16;
y2 = 84;
}
else if (WIN4) {
x1 = x2 = 80;
y1 = 16;
y2 = 84;
}
else if (WIN5) {
x1 = x2 = 114;
y1 = 16;
y2 = 84;
}
else if (WIN6) {
x1 = 26;
x2 = 134;
y1 = 16;
y2 = 84;
}
else if (WIN7) {
x1 = 134;
x2 = 26;
y1 = 16;
y2 = 84;
}
if (WIN) {
DrawLine(x1, y1, x2, y2, A_NORMAL);
DrawLine(x1+1, y1+1, x2+1, y2+1, A_NORMAL);
DrawLine(x1-1, y1-1, x2-1, y2-1, A_NORMAL);
break;
}
}
ngetchx();
clrscr();
puts("Play Again?\n(1 = YES / 2 = NO): ");
retry = ngetchx();
} while (retry == '1');
clrscr();
}