-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTic-Tac-Toe.bat
More file actions
482 lines (438 loc) · 12.5 KB
/
Tic-Tac-Toe.bat
File metadata and controls
482 lines (438 loc) · 12.5 KB
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
prompt $s
@echo off
title Tic Tac Toe
setlocal EnableDelayedExpansion
set dataAddress=BatchScript_Data\TicTacToe
if not exist "%dataAddress%" md "%dataAddress%"
set "logName=AI_Weakness.log"
set "moveLogsAddress=%dataAddress%\%logName%"
set "gameMode=2P"
set "p1Code=X"
set "gameDebug=1"
:setup
set "turnCode=X"
set gameTurnNum=1
for %%a in (A B C) do (
for /l %%n in (1,1,3) do (
set "%%a%%n= "
)
)
for /l %%n in (1,1,9) do (
set "move%%n="
set "moveType%%n="
)
:inMode
cls
echo 1. Single Player
echo 2. 2 Players
echo 3. Previous setting
echo 4. Test AI vs. AI
echo 5. Moves Simulator
echo=
echo 0. Exit
echo=
choice /c 012345 /n /m "Choose your mode: "
set /a userInput=%errorlevel%-1
if "%userInput%" == "0" exit
if "%userInput%" == "3" (
if "%gameMode%"=="AIT" goto testAI
if "%p1Code%"=="O" if "%gameMode%"=="AI" goto gameAI%AILvl%
goto viewBoard
)
if "%userInput%" == "4" goto inAILvl
set "gameMode=2P"
set "p1Code=X"
if "%userInput%" == "2" goto viewBoard
if "%userInput%" == "5" goto gameMovesIn
set "gameMode=AI"
cls
echo 1. Very Easy
echo 2. Medium
echo 3. Very Hard
echo=
choice /c 123 /n /m "Choose your difficulty level: "
set "AILvl=%errorlevel%"
goto inTurnCode
:inAILvl
set "gameMode=AIT"
cls
echo 1. Very Easy
echo 2. Medium
echo 3. Very Hard
echo=
choice /c 123 /n /m "Choose AI_1 difficulty level: "
set "AIXLvl=%errorlevel%"
cls
echo 1. Very Easy
echo 2. Medium
echo 3. Very Hard
echo=
choice /c 123 /n /m "Choose AI_2 difficulty level: "
set "AIOLvl=%errorlevel%"
:testAI
cls
call :displayBoard
echo=
echo %turnCode%'s Turn
echo=
echo Press any key to make the AI move...
if "%gameDebug%" == "0" pause > nul
if "%turnCode%"=="X" set "checkRoute=OO XX"
if "%turnCode%"=="O" set "checkRoute=XX OO"
goto gameAI!AI%turnCode%Lvl!
:inTurnCode
cls
set "p1Code=X"
set "turnCode=X"
set "checkRoute=XX OO"
echo X starts first
echo=
choice /c XO /m "What would player 1 play as?"
if %errorlevel% == 2 set "p1Code=O"
if "%p1Code%"=="O" (
set "checkRoute=OO XX"
if "%gameMode%"=="AI" goto gameAI%AILvl%
)
:viewBoard
cls
call :displayBoard
echo=
echo %turnCode%'s Turn
echo=
echo Choose the box by typing in the number and the letter respectively. (eg.A1)
set /p "cellCode="
call :String_UPPERCASE cellCode
if "%cellCode%"=="X" goto setup
if "!%cellCode%!"==" " goto markCell
echo=
echo Invalid cell code
pause
goto viewBoard
:gameMovesIn
set "gameMode=S"
cls
echo Input game moves:
echo=
set /p "gameMoves="
set "gameTurnNum=1"
:gameSimulate
set /a tempVar1=(%gameTurnNum%-1) * 3
set "cellCode=!gameMoves:~%tempVar1%,2!"
cls
call :displayBoard
echo=
echo [ Move %gameTurnNum%] [%turnCode% Turn] %cellCode%
echo=
pause
if "%gameTurnNum%" == "9" goto inMode
:markCell
rem Mark the cell
set "%cellCode%=%turnCode%"
set "move%gameTurnNum%=%cellCode%"
if "%cellCode%" == "A1" set "moveType%gameTurnNum%=C"
if "%cellCode%" == "B1" set "moveType%gameTurnNum%=E"
if "%cellCode%" == "C1" set "moveType%gameTurnNum%=C"
if "%cellCode%" == "A2" set "moveType%gameTurnNum%=E"
if "%cellCode%" == "B2" set "moveType%gameTurnNum%=O"
if "%cellCode%" == "C2" set "moveType%gameTurnNum%=E"
if "%cellCode%" == "A3" set "moveType%gameTurnNum%=C"
if "%cellCode%" == "B3" set "moveType%gameTurnNum%=E"
if "%cellCode%" == "C3" set "moveType%gameTurnNum%=C"
set /a gameTurnNum+=1
:gameCheck
rem Check the game
set "gameStatus=Draw"
rem Check for draw
for %%a in (A B C) do (
for /l %%n in (1,1,3) do (
if "!%%a%%n!" == " " set "gameStatus=Unknown"
)
)
rem Check for win
for %%c in (XXX OOO) do (
for %%a in (A B C) do if "!%%a1!!%%a2!!%%a3!" == "%%c" set "gameStatus=%turnCode% Win"
for /l %%n in (1,1,3) do if "!A%%n!!B%%n!!C%%n!" == "%%c" set "gameStatus=%turnCode% Win"
if "%A1%%B2%%C3%" == "%%c" set "gameStatus=%turnCode% Win"
if "%A3%%B2%%C1%" == "%%c" set "gameStatus=%turnCode% Win"
)
if not "%gameStatus%" == "Unknown" goto announceGame
:changePlayer
if "%turnCode%" == "X" set "turnCode=T"
if "%turnCode%" == "O" set "turnCode=X"
if "%turnCode%" == "T" set "turnCode=O"
if "%gameMode%" == "2P" goto viewBoard
if "%gameMode%" == "AIT" goto testAI
if "%gameMode%" == "S" goto gameSimulate
if "%p1Code%" == "%turnCode%" goto viewBoard
goto gameAI%AILvl%
:gameAI1
call :genRandomCell
call :genOrBlockWin
if "!%cellCode%!"==" " goto markCell
goto gameAI1
:gameAI2
call :genRandomCell
call :genWinPos
call :genOrBlockWin
if "!%cellCode%!"==" " goto markCell
goto gameAI2
:gameAI3
call :genRandomCell
if %gameTurnNum% == 2 (
if "%moveType1%" == "O" call :genCornerRandom
if "%moveType1%" == "C" set "cellCode=B2"
if "%moveType1%" == "E" set "cellCode=B2"
)
if %gameTurnNum% == 3 (
if "%moveType2%" == "O" (
if "%moveType1%" == "E" call :genEquAdjRandom %move1%
if "%moveType1%" == "C" call :genOpposite %move1%
)
if "%moveType2%" == "C" (
if "%moveType1%" == "O" call :genOpposite %move2%
if "%moveType1%" == "E" set "cellCode=B2"
if "%move1%%move2%" == "C2C1" call :genEquAdjACW %move1%
if "%move1%%move2%" == "C2C3" call :genEquAdjCW %move1%
if "%move1%%move2%" == "B3C3" call :genEquAdjACW %move1%
if "%move1%%move2%" == "B3A3" call :genEquAdjCW %move1%
if "%move1%%move2%" == "A2A3" call :genEquAdjACW %move1%
if "%move1%%move2%" == "A2A1" call :genEquAdjCW %move1%
if "%move1%%move2%" == "B1A1" call :genEquAdjACW %move1%
if "%move1%%move2%" == "B1A3" call :genEquAdjCW %move1%
)
if "%moveType2%" == "E" (
if "%moveType1%" == "O" call :genNeqAdjRandom %move2%
if "%moveType1%" == "C" call :genEquAdjRandom %move1%
if "%moveType1%" == "E" call :genCornerRandom
)
)
if %gameTurnNum% == 4 (
if "%moveType3%" == "O" (
rem Checks here...
)
if "%moveType3%" == "C" (
rem OCC Prevention
if "%B2%" == "%checkRoute:~1,1%" call :genCornerRandom
if not "%B2%" == "%checkRoute:~1,1%" call :genEdgeRandom
)
if "%moveType3%" == "E" (
call :genCornerRandom
if "%A1: =%%A2%%A3: =%%B1%%C1: =%" == "XX" if "%cellCode%"=="C3" goto gameAI3
if "%C1: =%%A1: =%%B1%%C2%%C3: =%" == "XX" if "%cellCode%"=="A3" goto gameAI3
if "%C3: =%%C1: =%%C2%%A3: =%%B3%" == "XX" if "%cellCode%"=="A1" goto gameAI3
if "%A3: =%%A1: =%%A2%%B3%%C3: =%" == "XX" if "%cellCode%"=="C1" goto gameAI3
if "%B2%" == " " set "cellCode=B2"
)
)
if %gameTurnNum% == 5 (
if "%B2%" == " " set "cellCode=B2"
)
call :genOrBlockWin
rem Unlisted Strategies
rem
rem Unlisted Preventions
rem B1 B3 C2 A2 B2 A3 C3 A1
rem B3 B2 C2 A1 C3 A3 C1
rem C1 B2 B3 A1 C3 A3 C2
rem A3 B2 C2 A1 C3 B3 C1
rem A3 B2 B1 C3 A1 C1 A2
rem
if "!%cellCode%!"==" " goto markCell
goto gameAI3
goto gameCheck
:announceGame
set "gameMoves=Moves:"
for /l %%n in (1,1,9) do set "gameMoves=!gameMoves! !move%%n!"
if "%gameMode%%AILvl%%p1Code%" == "AI3%gameStatus:~0,1%" goto writeMoves
cls
call :displayBoard
echo=
echo %gameStatus%
echo=
pause
echo=
set "errorlevel=0"
if "%gameDebug%" == "0" choice /c YND /m "Do you want to play again?"
if "%errorlevel%" == "1" goto setup
if "%errorlevel%" == "2" exit
cls
call :displayBoard
echo=
echo %gameMoves%
echo=
pause
if "%gameDebug%" == "0" goto setup
set "turnCode=X"
set gameTurnNum=1
for %%a in (A B C) do (
for /l %%n in (1,1,3) do (
set "%%a%%n= "
)
)
for /l %%n in (1,1,9) do (
set "move%%n="
set "moveType%%n="
)
goto testAI
:writeMoves
@echo Game moves: >> %moveLogsAddress%
@echo [%p1Code%] %gameMoves% >> %moveLogsAddress%
@echo ================================================== >> %moveLogsAddress%
@echo= >> %moveLogsAddress%
cls
call :displayBoard
echo=
echo Excellent ^^! Congratulations ^^!
echo You found the weakness of the hardest AI made in this script.
echo=
echo Game moves:
echo [%p1Code%] %gameMoves%
echo=
pause
echo=
choice /c YND /m "Do you want to play again?"
if %errorlevel% == 1 goto setup
rem Functions
:genRandomCell
set /a cellCode=%random% %% 3
if %cellCode% == 0 set "cellCode=A"
if %cellCode% == 1 set "cellCode=B"
if %cellCode% == 2 set "cellCode=C"
set /a cellCode=(%random% %% 3)+1 & set "cellCode=%cellCode%!cellCode!"
rem Random Cell [%cellCode%]
goto :EOF
:genCornerRandom
set /a cellCode=%random% %% 4
if %cellCode% == 0 set "cellCode=A1"
if %cellCode% == 1 set "cellCode=A3"
if %cellCode% == 2 set "cellCode=C1"
if %cellCode% == 3 set "cellCode=C3"
rem Corner random [%cellCode%]
goto :EOF
:genEdgeRandom
set /a cellCode=%random% %% 4
if %cellCode% == 0 set "cellCode=A2"
if %cellCode% == 1 set "cellCode=B1"
if %cellCode% == 2 set "cellCode=B3"
if %cellCode% == 3 set "cellCode=C2"
rem Edge Random [%cellCode%]
goto :EOF
:genOpposite
if "%1" == "A1" set "cellCode=C3"
if "%1" == "B1" set "cellCode=B3"
if "%1" == "C1" set "cellCode=A3"
if "%1" == "A2" set "cellCode=C2"
if "%1" == "B2" set "cellCode=B2"
if "%1" == "C2" set "cellCode=A2"
if "%1" == "A3" set "cellCode=C1"
if "%1" == "B3" set "cellCode=B1"
if "%1" == "C3" set "cellCode=A1"
goto :EOF
:genEquAdjCW
if "%1" == "A1" set "cellCode=C1"
if "%1" == "B1" set "cellCode=C2"
if "%1" == "C1" set "cellCode=C3"
if "%1" == "A2" set "cellCode=B1"
if "%1" == "B2" set "cellCode=B2"
if "%1" == "C2" set "cellCode=B3"
if "%1" == "A3" set "cellCode=A1"
if "%1" == "B3" set "cellCode=A2"
if "%1" == "C3" set "cellCode=A3"
goto :EOF
:genEquAdjACW
if "%1" == "A1" set "cellCode=A3"
if "%1" == "B1" set "cellCode=A2"
if "%1" == "C1" set "cellCode=A1"
if "%1" == "A2" set "cellCode=B3"
if "%1" == "B2" set "cellCode=B2"
if "%1" == "C2" set "cellCode=B1"
if "%1" == "A3" set "cellCode=C3"
if "%1" == "B3" set "cellCode=C2"
if "%1" == "C3" set "cellCode=C1"
goto :EOF
:genEquAdjRandom
set /a tempVar1=%random% %% 2
if %tempVar1% == 0 call :genEquAdjCW %1
if %tempVar1% == 1 call :genEquAdjACW %1
goto :EOF
:genNeqAdjCW
if "%1" == "A1" set "cellCode=B1"
if "%1" == "B1" set "cellCode=C1"
if "%1" == "C1" set "cellCode=C2"
if "%1" == "A2" set "cellCode=A1"
if "%1" == "B2" set "cellCode=B2"
if "%1" == "C2" set "cellCode=C3"
if "%1" == "A3" set "cellCode=A2"
if "%1" == "B3" set "cellCode=A3"
if "%1" == "C3" set "cellCode=B3"
goto :EOF
:genNeqAdjACW
if "%1" == "A1" set "cellCode=A2"
if "%1" == "B1" set "cellCode=A1"
if "%1" == "C1" set "cellCode=B1"
if "%1" == "A2" set "cellCode=A3"
if "%1" == "B2" set "cellCode=B2"
if "%1" == "C2" set "cellCode=C1"
if "%1" == "A3" set "cellCode=B3"
if "%1" == "B3" set "cellCode=C3"
if "%1" == "C3" set "cellCode=C2"
goto :EOF
:genNeqAdjRandom
set /a tempVar1=%random% %% 2
if %tempVar1% == 0 call :genNeqAdjCW %1
if %tempVar1% == 1 call :genNeqAdjACW %1
goto :EOF
:genOrBlockWin
rem Set AI winning position or block opponent winning position
for %%c in (%checkRoute%) do (
for %%a in (A B C) do if "!%%a1: =!!%%a2: =!!%%a3: =!" == "%%c" (
for %%n in (1 2 3) do if "!%%a%%n!" == " " set "cellCode=%%a%%n"
)
for %%n in (1 2 3) do if "!A%%n: =!!B%%n: =!!C%%n: =!" == "%%c" (
for %%a in (A B C) do if "!%%a%%n!" == " " set "cellCode=%%a%%n"
)
if "%A1: =%%B2: =%%C3: =%" == "%%c" (
for %%b in (A1 B2 C3) do if "!%%b!" == " " set "cellCode=%%b"
)
if "%A3: =%%B2: =%%C1: =%" == "%%c" (
for %%b in (A3 B2 C1) do if "!%%b!" == " " set "cellCode=%%b"
)
)
goto :EOF
:genWinPos
rem Set AI possible winning positions
for %%c in (%checkRoute:~-1,1%) do (
for %%a in (A B C) do if "!%%a1: =!!%%a2: =!!%%a3: =!" == "%%c" (
for %%n in (1 2 3) do if "!%%a%%n!" == " " set "cellCode=%%a%%n"
)
for %%n in (1 2 3) do if "!A%%n: =!!B%%n: =!!C%%n: =!" == "%%c" (
for %%a in (A B C) do if "!%%a%%n!" == " " set "cellCode=%%a%%n"
)
if "%A1: =%%B2: =%%C3: =%" == "%%c" (
for %%b in (A1 B2 C3) do if "!%%b!" == " " set "cellCode=%%b"
)
if "%A3: =%%B2: =%%C1: =%" == "%%c" (
for %%b in (A3 B2 C1) do if "!%%b!" == " " set "cellCode=%%b"
)
)
goto :EOF
:displayBoard
echo A B C
echo ÉÍÍÍÍÍÍÍÍÍÍÍ»
echo 1 º %A1% ³ %B1% ³ %C1% º
echo ºÄÄÄoÄÄÄoÄÄĺ
echo 2 º %A2% ³ %B2% ³ %C2% º
echo ºÄÄÄoÄÄÄoÄÄĺ
echo 3 º %A3% ³ %B3% ³ %C3% º
echo ÈÍÍÍÍÍÍÍÍÍÍͼ
goto :EOF
:String_UPPERCASE
rem %1 is the string name
rem TempVar:
for %%a in (
A B C D E F G
H I J K L M N
O P Q R S T U
V W X Y Z
) do set "%1=!%1:%%a=%%a!"
goto :EOF