-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttt.para
503 lines (437 loc) · 14.3 KB
/
ttt.para
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
let ttt = Type.extend({
name = "TicTacToe"
EMPTY = 0
O_SPOT = 1
X_SPOT = 2
instance = {
moves = [0, 0, 0, 0, 0, 0, 0, 0, 0]
func to_str(self) {
return "TicToeToe moves: " + self.moves.to_str();
}
}
func __construct__(self){
}
play = func (self){}
board_gen = func (self){}
func board_draw_top(self){
}
func get(self, loc) {
let strn = loc+1;
let ch:str = strn.to_str();
let move = self.moves[loc].to_int();
if move == self.O_SPOT {
ch = 'O';
}
elif move == self.X_SPOT {
ch = 'X';
}
return ch;
}
func clear_board(self) {
self.moves = [0, 0, 0, 0, 0, 0, 0, 0, 0];
}
func ai_find_next_spot(self) {
let about_to_win = self.check_almost_winner(self.O_SPOT);
let opponent_about_to_win = self.check_almost_winner(self.X_SPOT);
let available_spaces = [];
let index = 0;
for move in self.moves {
if move == 0 {
available_spaces += index;
}
index += 1;
}
if !self.check_winner(self.X_SPOT) && !self.check_winner(self.O_SPOT) && available_spaces.len() == 0 {
self.board_draw();
print("\n=== TIED! ===\n");
self.clear_board();
self.ai_find_next_spot();
return 0;
}
let spot;
let done = false;
if about_to_win {
spot = self.check_missing_spot(self.O_SPOT);
if self.moves[spot] == 0 {
self.moves[spot] = self.O_SPOT;
done = true;
}
else {
if opponent_about_to_win {
spot = self.check_missing_spot(self.X_SPOT);
if self.moves[spot] == 0 {
self.moves[spot] = self.O_SPOT;
done = true;
}
else {
import "std/time.para";
import "std/math/random.para";
spot = random.range(available_spaces.len()-1, 0, Time.now().to_int());
self.moves[available_spaces[spot]] = self.O_SPOT;
done = true;
}
}
else {
import "std/time.para";
import "std/math/random.para";
spot = random.range(available_spaces.len()-1, 0, Time.now().to_int());
self.moves[available_spaces[spot]] = self.O_SPOT;
done = true;
}
}
}
if opponent_about_to_win && !done {
spot = self.check_missing_spot(self.X_SPOT);
if self.moves[spot] == 0 {
self.moves[spot] = self.O_SPOT;
done = true;
}
else {
import "std/time.para";
import "std/math/random.para";
spot = random.range(available_spaces.len()-1, 0, Time.now().to_int());
self.moves[available_spaces[spot]] = self.O_SPOT;
done = true;
}
}
if !done {
import "std/time.para";
import "std/math/random.para";
spot = random.range(available_spaces.len()-1, 0, Time.now().to_int());
self.moves[available_spaces[spot]] = self.O_SPOT;
done = true;
}
available_spaces = [];
for move in self.moves {
if move == 0 {
available_spaces += index;
}
index += 1;
}
if available_spaces.len() == 0 {
self.board_draw();
print("\n=== TIED! ===\n");
self.clear_board();
self.ai_find_next_spot();
return 0;
}
}
func ai_random_find_next_spot(self) {
import "std/time.para";
import "std/math/random.para";
let available_spaces = [];
let index = 0;
for move in self.moves {
if move == 0 {
available_spaces += index;
}
index += 1;
}
if !self.check_winner(self.X_SPOT) && !self.check_winner(self.O_SPOT) && available_spaces.len() == 0 {
self.board_draw();
print("\n=== TIED! ===\n");
self.clear_board();
self.ai_random_find_next_spot();
return 0;
}
let rand = random.range(available_spaces.len()-1, 0, Time.now().to_int());
self.moves[available_spaces[rand]] = self.O_SPOT;
available_spaces = [];
for move in self.moves {
if move == 0 {
available_spaces += index;
}
index += 1;
}
if !self.check_winner(self.X_SPOT) && !self.check_winner(self.O_SPOT) && available_spaces.len() == 0 {
self.board_draw();
print("\n=== TIED! ===\n");
self.clear_board();
self.ai_random_find_next_spot();
return 0;
}
}
func ai_move(self) {
return self.ai_find_next_spot();
}
func check_index(self, player, center_index, offset, check_offset) {
let index = 0;
while index != 3 {
if self.moves[center_index-check_offset] == player {
if self.moves[center_index+check_offset] == player {
if self.moves[center_index] == player {
return player;
}
}
}
center_index += offset;
index += 1;
}
return self.EMPTY;
}
func check_almost_index(self, player, center_index, offset, check_offset) {
let index = 0;
while index != 3 {
if self.moves[center_index-check_offset] == player {
if self.moves[center_index] == player {
return player;
}
}
if self.moves[center_index+check_offset] == player {
if self.moves[center_index] == player {
return player;
}
}
if self.moves[center_index+check_offset] == player {
if self.moves[center_index-check_offset] == player {
return player;
}
}
center_index += offset;
index += 1;
}
return self.EMPTY;
}
func check_missing_index(self, player, center_index, offset, check_offset) {
let index = 0;
while index != 3 {
if self.moves[center_index-check_offset] == player {
if self.moves[center_index] == player {
return center_index+check_offset;
}
}
if self.moves[center_index+check_offset] == player {
if self.moves[center_index] == player {
return center_index-check_offset;
}
}
if self.moves[center_index+check_offset] == player {
if self.moves[center_index-check_offset] == player {
return center_index;
}
}
center_index += offset;
index += 1;
}
return self.EMPTY;
}
func check_diag_corner(self, player, top, bottom) {
if self.moves[top] == player {
if self.moves[bottom] == player {
if self.moves[4] == player {
return player;
}
}
}
return self.EMPTY;
}
func check_almost_diag_corner(self, player, top, bottom) {
if self.moves[top] == player {
if self.moves[4] == player {
return player;
}
}
if self.moves[bottom] == player {
if self.moves[4] == player {
return player;
}
}
if self.moves[top] == player {
if self.moves[bottom] == player {
return player;
}
}
return self.EMPTY;
}
func check_missing_diag_corner(self, player, top, bottom) {
if self.moves[top] == player {
if self.moves[4] == player {
return bottom;
}
}
if self.moves[bottom] == player {
if self.moves[4] == player {
return top;
}
}
if self.moves[top] == player {
if self.moves[bottom] == player {
return 4;
}
}
return self.EMPTY;
}
func check_diagonal(self, player) {
let tl = self.check_diag_corner(player, 0, 8);
let bl = self.check_diag_corner(player, 6, 2);
if tl {
return tl;
}
if bl {
return bl;
}
return self.EMPTY;
}
func check_almost_diagonal(self, player) {
let tl = self.check_almost_diag_corner(player, 0, 8);
let bl = self.check_almost_diag_corner(player, 6, 2);
if tl {
return tl;
}
if bl {
return bl;
}
return self.EMPTY;
}
func check_missing_diagonal(self, player) {
let tl = self.check_almost_diag_corner(player, 0, 8);
let bl = self.check_almost_diag_corner(player, 6, 2);
if tl {
let missing = self.check_missing_diag_corner(player, 0, 8);
return missing;
}
if bl {
let missing = self.check_missing_diag_corner(player, 6, 2);
return missing;
}
return self.EMPTY;
}
func print_winner(self, player) {
if player == self.O_SPOT {
self.board_draw();
print("\n=== YOU LOSE! ===\n");
}
elif player == self.X_SPOT {
self.board_draw();
print("\n=== YOU WIN! ===\n");
}
}
func check_winner(self, player) {
// start at left side in middle of board, checking above and below, and move by 1 spot.
let colcheck = self.check_index(player, 3, 1, 3);
// start at top in middle of board, checking left and right and moving down by board width
let rowcheck = self.check_index(player, 1, 3, 1);
let diagcheck = self.check_diagonal(player);
// check if win in columns
if colcheck {
self.print_winner(player);
self.clear_board();
return true;
}
// check if win in rows
elif rowcheck {
self.print_winner(player);
self.clear_board();
return true;
}
// check if win in diagonal spaces
elif diagcheck {
self.print_winner(player);
self.clear_board();
return true;
}
return false;
}
func check_almost_winner(self, player) {
// start at left side in middle of board, checking above and below, and move by 1 spot.
let colcheck = self.check_almost_index(player, 3, 1, 3);
// start at top in middle of board, checking left and right and moving down by board width
let rowcheck = self.check_almost_index(player, 1, 3, 1);
let diagcheck = self.check_almost_diagonal(player);
// check if almost win in columns
if colcheck {
return true;
}
// check if almost win in rows
elif rowcheck {
return true;
}
// check if almost win in diagonal spaces
elif diagcheck {
return true;
}
return false;
}
func check_missing_spot(self, player) {
// start at left side in middle of board, checking above and below, and move by 1 spot.
let colcheck = self.check_almost_index(player, 3, 1, 3);
// start at top in middle of board, checking left and right and moving down by board width
let rowcheck = self.check_almost_index(player, 1, 3, 1);
let diagcheck = self.check_almost_diagonal(player);
// check if almost win in columns
if colcheck {
let missing = self.check_missing_index(player, 3, 1, 3);
return missing;
}
// check if almost win in rows
elif rowcheck {
let missing = self.check_missing_index(player, 1, 3, 1);
return missing;
}
// check if almost win in diagonal spaces
elif diagcheck {
let missing = self.check_missing_diagonal(player);
return missing;
}
return false;
}
func player_write(self, player_char) {
if player_char == 'O' {
io.write_color(Console.RED, player_char);
}
elif player_char == 'X' {
io.write_color(Console.YELLOW, player_char);
}
else {
io.write(player_char);
}
}
func board_draw_row(self, lc) {
let v0 = self.get(lc);
let v1 = self.get(lc + 1);
let v2 = self.get(lc + 2);
// print column 1
io.write("| "); self.player_write(v0);
// column 2
io.write(" | "); self.player_write(v1);
// column 3
io.write(" | "); self.player_write(v2);
// print end
io.write(" |\n");
print("+---+---+---+");
}
func board_draw(self) {
let index = 0;
print("+---+---+---+");
while index != 9 {
self.board_draw_row(index);
index += 3;
}
}
});
ttt.play = func (self) {
let command = "";
print("=== TICTACTOE ===");
print("Input 'q' to exit");
print("Input a number play a spot");
self.board_draw();
let int_cmd = 0;
while command != "q" {
command = Console.read();
if command == 'q' {
return 0;
}
int_cmd = command.to_int()-1;
if self.moves[int_cmd] == self.EMPTY {
self.moves[int_cmd] = self.X_SPOT;
self.check_winner(self.X_SPOT);
self.ai_move();
self.check_winner(self.O_SPOT);
self.board_draw();
}
else {
print("Space already taken!");
self.board_draw();
}
}
};