-
Notifications
You must be signed in to change notification settings - Fork 1
/
TTT.asm
3491 lines (2736 loc) · 61.8 KB
/
TTT.asm
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Jeremy Greenburg and Cody Robertson
; Tic Tac Toe
; Plays ye old guessing game with the user
; nasm -f elf32 TTT.asm && gcc -m32 TTT.o -o TTT
; DEBUG:
; nasm -f elf32 -l TTT.lst TTT.asm && gcc -m32 -o TTT TTT.o
%define SYS_EXIT 1
%define SYS_READ 3
%define SYS_WRITE 4
%define STDIN 0
%define STDOUT 1
%define STDERR 2
%define EMPTY_VAL 0
%define X_VAL 1
%define O_VAL 2
%define PVAI 1
%define PVP 2
%define AIVAI 3
SECTION .bss
input: resd 1 ; User inputed space
playSymb: resd 1 ; The Player's symbol
compSymb: resd 1 ; The Computer's symbol
winner: resd 1 ; 1 If player won, 0 if Comp won
mode: resd 1 ; Game mode (PVAI, PVP, or AIVAI)
SECTION .data
m_badBounds: db "ERROR: Invalid space", 10, 0 ;user chose a space that is not 1-9
board: times 9 dd 0 ;legit board
f_int: db "%d", 0 ; Integer format
f_strn: db "%s", 10, 0 ; String format w newline
f_str: db "%s", 0 ; String format
f_char: db "%c", 0 ; Character format
white: db 27, "[47m", 0 ; White background
red: db 27, "[31m", 0 ; Red foreground
blue: db 27, "[34m", 0 ; Blue foreground
black: db 27, "[30m", 0 ; Black foreground
alt: db 27, "(0", 0 ; Alternate character set
norm: db 27, "(B", 0 ; Normal Character Set
reset: db 27, "(B", 27, "[40m", 0 ; Normal character set and background
clearScreen: db 27, "[H", 27, "[2J", 0
X: db "X", 0 ; X
O: db "O", 0 ; Y
E: db " ", 0 ; Space
a: db "a", 0 ; a
pName: db "Dave", 0 ; Player/Player1/Computer1 Name
times 100-$+pName db 0 ; Reserve enough room in pName for 100 characters total
cName: db "Hal", 0 ; Computer/Player2/Computer2 Name
times 100-$+cName db 0 ; Reserve enough room in cName for 100 characters total
m_badSpot: db "Space already occupied", 10, 0 ; User chose occupied spot in board
newline: db 10, 0 ; newline character
m_tieWin: db "Tie game!", 10, 0 ; Tie Game Message
m_winner: db " is the winner!", 10, 0 ; Is the winner message
m_anyKey: db "Press enter to continue. ", 0 ; Press enter to continue
m_currWins: db " won ", 0 ; x Won y games
m_currTies: db "Games tied: ", 0 ; Games tied message
m_totGames: db " out of ", 0 ; y games out of total games
m_games: db " games", 10, 0 ; Games
m_playerMove: db ", please make a move. ", 0 ; The player's turn
m_compMove: db " is making a move. ", 0 ; The computer's turn
m_characterIs: db " is ", 0 ; is
m_promptName: db "Please enter your name: ", 0 ; Prompt the only player for his name
m_prompt1stName:db "Please enter player 1's name: ", 0 ; Prompt player 1 for his name
m_prompt2ndName:db "Please enter player 2's name: ", 0 ; Prompt player 2 for his name
m_promptMode: db "Please select a game mode by entering the corresponing number: ", 10
db "1) Play against an AI", 10
db "2) Play against another player", 10
db "3) Watch two AI duke it out", 10
db ": ", 0
m_playAgain: db "Would you like to play again? ", 10
db "1 - Yes", 10
db "2 - No", 10, 0
currentSymb: dd X_VAL ; Current playing symbol
wins: dd 0 ; Player wins
loss: dd 0 ; Player loss
ties: dd 0 ; Ties
debug: db "Not Found", 10, 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CELL ONE ESCAPES
cell1Start: db 27, "[3;2H", 0
cell1_2: db 27, "[4;2H", 0
cell1_3: db 27, "[5;2H", 0
cell1_4: db 27, "[6;2H", 0
cell1_5: db 27, "[7;2H", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CELL TWO ESCAPES
cell2Start: db 27, "[3;7H", 0
cell2_2: db 27, "[4;7H", 0
cell2_3: db 27, "[5;7H", 0
cell2_4: db 27, "[6;7H", 0
cell2_5: db 27, "[7;7H", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CELL THREE ESCAPES
cell3Start: db 27, "[3;12H", 0
cell3_2: db 27, "[4;12H", 0
cell3_3: db 27, "[5;12H", 0
cell3_4: db 27, "[6;12H", 0
cell3_5: db 27, "[7;12H", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CELL FOUR ESCAPES
cell4Start: db 27, "[8;2H", 0
cell4_2: db 27, "[9;2H", 0
cell4_3: db 27, "[10;2H", 0
cell4_4: db 27, "[11;2H", 0
cell4_5: db 27, "[12;2H", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CELL FIVE ESCAPES
cell5Start: db 27, "[8;7H", 0
cell5_2: db 27, "[9;7H", 0
cell5_3: db 27, "[10;7H", 0
cell5_4: db 27, "[11;7H", 0
cell5_5: db 27, "[12;7H", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CELL SIX ESCAPES
cell6Start: db 27, "[8;12H", 0
cell6_2: db 27, "[9;12H", 0
cell6_3: db 27, "[10;12H", 0
cell6_4: db 27, "[11;12H", 0
cell6_5: db 27, "[12;12H", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CELL SEVEN ESCAPES
cell7Start: db 27, "[13;2H", 0
cell7_2: db 27, "[14;2H", 0
cell7_3: db 27, "[15;2H", 0
cell7_4: db 27, "[16;2H", 0
cell7_5: db 27, "[17;2H", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CELL EIGHT ESCAPES
cell8Start: db 27, "[13;7H", 0
cell8_2: db 27, "[14;7H", 0
cell8_3: db 27, "[15;7H", 0
cell8_4: db 27, "[16;7H", 0
cell8_5: db 27, "[17;7H", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CELL NINE ESCAPES
cell9Start: db 27, "[13;12H", 0
cell9_2: db 27, "[14;12H", 0
cell9_3: db 27, "[15;12H", 0
cell9_4: db 27, "[16;12H", 0
cell9_5: db 27, "[17;12H", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; LEFT WALL ESCAPES
lft1: db 27, "[3;1H", 0
lft2: db 27, "[4;1H", 0
lft3: db 27, "[5;1H", 0
lft4: db 27, "[6;1H", 0
lft5: db 27, "[7;1H", 0
lft6: db 27, "[8;1H", 0
lft7: db 27, "[9;1H", 0
lft8: db 27, "[10;1H", 0
lft9: db 27, "[11;1H", 0
lft10: db 27, "[12;1H", 0
lft11: db 27, "[13;1H", 0
lft12: db 27, "[14;1H", 0
lft13: db 27, "[15;1H", 0
lft14: db 27, "[16;1H", 0
lft15: db 27, "[17;1H", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END CELL ESCAPES
solidRow: db "aaaaa", 0 ; A solid row
xRow1: db "\ /a", 0 ; First x row
xRow2: db " \/ a", 0 ; Second x row
xRow3: db " /\ a", 0 ; Third x row
xRow4: db "/ \a", 0 ; Fourth x row
oRow1: db " ff a", 0 ; First circle row
oRow2: db "f fa", 0 ; Second circle row
oRow3: db "f fa", 0 ; Third circle row
oRow4: db " ff a", 0 ; Fourth circle row
ePrint: db " a", 0 ; Printing for empty cell
enemySymb: dd O_VAL ; Not playing symbol
firstTurn: dd 0 ; 0 if first turn, 1 if not
SECTION .text
global main
extern scanf
extern printf
extern time
extern rand
extern srand
main:;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; BEGIN main
push white ; Set the foreground to white
call printf
add ESP, 4
push black ; Set text to black
call printf
add ESP, 4
push norm ; Normal Character set (just in case)
call printf
add ESP, 4
call choosePlayers ; choose which player goes first (also sets random seed for program)
call getMode ; Prompts the player for game type
mov EAX, [mode] ; Move the answer into EAX
cmp EAX, PVAI ; See if they chose pvai
je pvaiInit ; Start pvai game
cmp EAX, PVP ; See if they cose pvp
je pvpInit ; Start pvp game
cmp EAX, AIVAI ; See if they chose aivai game
je aivailoop ; Start aivai game
;jmp to get mode again
pvaiInit: ; Initialize pvai cycle
call getName ; Get the player's anme
pvailoop: ; Player versus AI loop
call gameInfo ; Print who is who
call prettyPrint ; print original empty board
.gameloop: ; Start of the game loop
mov EAX, [currentSymb] ;put current symbol in EAX
mov EBX, [playSymb] ;put player's symbol in EBX
cmp EAX, EBX ;check if player's turn or computer's turn
je .player
jmp .computer
.player: ; player's turn
call getInput ; get user input for move placement
jmp .bottom
.computer:
call computer ; get user input for move placement
jmp .bottom
.bottom:
call switchTurn ; switch who's turn it is
call prettyPrint ; Print board again
call calcWin ; returns 0 if game is still going; 1,2,or 3 if ended
cmp EAX, 0 ; if game is still going,
je .gameloop ; continue game loop
call endgame ; something to update scores, reset board, etc.
cmp EAX, 0 ; See if player chose not to continue
je exit ; If so, exit
call switchPlayers ; switch players' symbols
jmp pvailoop ; start new game
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END PVAI LOOP
aivailoop: ; AI versus AI game
call prettyPrint ; print original empty board
.gameloop:
mov EAX, [currentSymb] ;put current symbol in EAX
mov EBX, [playSymb] ;put player's symbol in EBX
cmp EAX, EBX ;check if player's turn or computer's turn
je .player
jmp .computer
.player:
call computer ; get user input for move placement
jmp .bottom
.computer:
call computer ; get user input for move placement
jmp .bottom
.bottom:
call switchTurn ; Switch who's turn it is
call gameInfo ; Print who is who
call prettyPrint ; Print the board
call calcWin ; returns 0 if game is still going; 1,2,or 3 if ended
cmp EAX, 0 ; if game is still going,
je .gameloop ; continue game loop
call endgame ; something to update scores, reset board, etc.
cmp EAX, 0
je exit
call switchPlayers ; switch players' symbols
jmp aivailoop ; start new game
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END AIVAI LOOP
pvpInit:
call getName ; Get player 1's name
call getOtherName ; Get player 2's name
pvploop: ; Player versus player game
call prettyPrint ; print original empty board
.gameloop:
mov EAX, [currentSymb] ;put current symbol in EAX
mov EBX, [playSymb] ;put player's symbol in EBX
cmp EAX, EBX ;check if player's turn or computer's turn
je .player
jmp .computer
.player:
call getInput ; get user input for move placement
jmp .bottom
.computer:
call getInput ; get user input for move placement
jmp .bottom
.bottom:
call switchTurn ; Switch who's turn it is
call prettyPrint
call calcWin ; returns 0 if game is still going; 1,2,or 3 if ended
cmp EAX, 0 ; if game is still going,
je .gameloop ; continue game loop
call endgame ; something to update scores, reset board, etc.
cmp EAX, 0
je exit
call switchPlayers ; switch players' symbols
jmp pvploop ; start new game
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END main
getInput:;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; BEGIN getInput
call turnInfo
push input ; push variable to store input to stack
push f_int ; push format string for getting integer input
call scanf ; get user's inputted move
add ESP, 8 ; adjust stack pointer
mov EAX, [input] ; move user's inputted number into EAX
cmp EAX, 1 ; make sure user's input is greater or equal to 1
jl .fail ; if not, print m_badBounds
cmp EAX, 9 ; make sure user's input is less than or equal to 9
jg .fail ; if not, print m_badBounds
call setSpot ; if input is within bounds, attempt to set space
jmp .end ; return
.fail:
call prettyPrint
push red ; Make foreground red
call printf
add ESP, 4
push m_badBounds ; push out of bounds warning string to stack
call printf ; print warning to user
add ESP, 4 ; adjust stack pointer
push black ; Make foreground black
call printf
add ESP, 4
jmp getInput ; Go to top of function
.end: ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END getInput
;;Takes EAX as parameter;;
setSpot:;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; BEGIN setSpot
mov [input], EAX
dec EAX ; adjust input for array indexing
xor EDX, EDX ; set EDX to 0 before multiplication
imul EAX, 4 ; multiply EAX by 4 due to 4-byte blocks of integers in array
mov EBX, [board + EAX] ; retrieve current value at position in board
cmp EBX, 0 ; if board position is not empty, print warning
jne .bad
mov EBX, [currentSymb] ; mov the player's symbol into EBX
mov [board + EAX], EBX ; set board spot to player's symbol
jmp .end ; ret
.bad:
call prettyPrint
push red ; Make foreground red
call printf
add ESP, 4
push m_badSpot ; move warning string that space is occupied to stack
call printf ; print warning to user
add ESP, 4 ; adjust stack pointer
push black ; Make foreground black
call printf
add ESP, 4
call getInput ; Get input again
.end: ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END setSpot
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; BEGIN computer
computer:
call turnInfo
xor ECX, ECX
.wait:
NOP
inc ECX
cmp ECX, 1000000000
jne .wait
.checkFirst:
mov EAX, [currentSymb]
cmp EAX, O_VAL
je .checkWins
mov EAX, [firstTurn] ;check if first turn
cmp EAX, 0 ;if equal, first turn
je .first ;calculate random spot to move and go there
;if not the first move, check if winning moves exist for computer or player
.checkWins:
mov EBX, [currentSymb]
call calcWinMove ;check if computer has winning move
cmp EAX, 0 ;if 0, no move was found
jne .makeMove ;if not 0, make the move found
mov EBX, [enemySymb]
call calcWinMove ;check if player has winning move
cmp EAX, 0 ;if 0, no move was found
jne .makeMove ;if not 0, make the move found
;if no winning moves, continue to main algorithm to
.findMove:
;main algorithm should go here
call defend ;protects against adjacent sides
cmp EAX, 0
jne .makeMove
call cornerSide ;protects against corner and opposite sides
cmp EAX, 0
jne .makeMove
mov EAX, [input]
cmp EAX, 1
je .counterCorner
cmp EAX, 3
je .counterCorner
cmp EAX, 7
je .counterCorner
cmp EAX, 9
je .counterCorner
cmp EAX, 5
je .counterCenter
jmp .counterSide
.counterCorner:
call tryCenter
cmp EAX, 0
jne .makeMove
call tryCorner
cmp EAX, 0
jne .makeMove
call trySide
jmp .makeMove
.counterCenter:
call tryCorner
cmp EAX, 0
jne .makeMove
call trySide
jmp .makeMove
.counterSide:
mov EAX, [firstTurn]
cmp EAX, 1
je .specialSide
call trySide
cmp EAX, 0
jne .makeMove
call tryCenter
cmp EAX, 0
jne .makeMove
call tryCorner
jmp .makeMove
.specialSide:
call tryCenter
cmp EAX, 0
jne .makeMove
call trySide
cmp EAX, 0
jne .makeMove
call tryCorner
jmp .makeMove
.makeMove:
call setSpot ;set spot (takes EAX as parameter)
jmp .exit
.first:
call rand ;store random value in EAX
xor EDX, EDX ;set EDX to 0 to prepare for division
mov EBX, 9 ;willl divide by 9
div EBX
mov EAX, EDX ;copy EDX remainder into EAX
inc EAX ;format EAX for setSpot function
call setSpot ;sets spot (takes EAX as parameter)
.exit:
ret ;exit function
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END computer
;;Takes EBX as input. EBX holds current symbol being checked;;
;;Returns location 1-9 for placement or 0 if no spot found in EDX;;
calcWinMove:;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; BEGIN checkWin
;;;;;;;;;;;;;;;;;BEGIN AREA CHECK;;;;;;;;;;;;;;;;;;
.topRow:
xor ECX, ECX ; Set spaces counter to 0
.tr1:
mov EAX, [board] ; Top left cell
cmp EAX, EBX ; If equal to selected symbol,
je .tr2 ; move to next
cmp EAX, 0 ; if not 0 either, must be other symbol
jne .centRow ; begin next area check
mov EDX, 1 ; store location of empty space
inc ECX ; increment space counter
.tr2:
mov EAX, [board + 4] ; Top center cell
cmp EAX, EBX ; If equal to selected symbol,
je .tr3 ; move to next
cmp EAX, 0 ; if not 0 either, must be other symbol
jne .centRow ; begin next area check
cmp ECX, 0 ; find value of space counter
jg .centRow ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 2 ; store location of empty space
inc ECX ; increment space counter
.tr3:
mov EAX, [board + 8] ; Top right cell
cmp EAX, EBX ; If equal to selected symbol,
je .found ; space has been found previously
cmp EAX, 0 ; if not 0 either, must be other symbol
jne .centRow ; begin next area check
cmp ECX, 0 ; find value of space counter
jg .centRow ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 3 ; store location of empty space
inc ECX ; increment space counter
jmp .found ; space has been found
;;;;;;;;;;;;;;;;;;BEGIN NEXT AREA CHECK;;;;;;;;;;;;;;;;;;;;
.centRow:
xor ECX, ECX ; Set space counter to 0
.cr1:
mov EAX, [board + 12] ; Middle left cell
cmp EAX, EBX ; If equal to selected symbol,
je .cr2 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .botRow ; begin next area check
mov EDX, 4 ; store location of empty space
inc ECX ; increment space counter
.cr2:
mov EAX, [board + 16] ; Middle center cell
cmp EAX, EBX ; If equal to selected symbol
je .cr3 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .botRow ; begin next area check
cmp ECX, 0 ; find value of space counter
jg .botRow ; if greater than 0, move to next cehck (multiple spaces found)
mov EDX, 5 ; store location of empty space
inc ECX ; increment space counter
.cr3:
mov EAX, [board + 20] ; Middle right cell
cmp EAX, EBX ; If equal to selected symbol,
je .found ; move to area check
cmp EAX, 0 ; if not 0 either, must be other symbol
jne .botRow ; begin next area check
cmp ECX, 0 ; find value of space counter
jg .botRow ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 6 ; store location of empty space
inc ECX ; increment space counter
jmp .found
;;;;;;;;;;;;;;;BEGIN NEXT AREA CHECK;;;;;;;;;;;;;;;;;
.botRow:
xor ECX, ECX ; set space counter back to 0
.br1:
mov EAX, [board + 24] ; Bottom left cell
cmp EAX, EBX ; If equal to selected symbol,
je .br2 ; move to next
cmp EAX, 0 ; if not 0 either, must be other symbol
jne .leftCol ; begin next area check
mov EDX, 7 ; store location of empty space
inc ECX ; increment space counter
.br2:
mov EAX, [board + 28] ; Bottom center cell
cmp EAX, EBX ; If equal to selected symbol,
je .br3 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .leftCol ; begin next area check
cmp ECX, 0 ; find value of space counter
jg .leftCol ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 8 ; store location of empty space
inc ECX ; increment space counter
.br3:
mov EAX, [board + 32] ; Bottom right cell
cmp EAX, EBX ; If equal to selected symbol,
je .found ; move to area check
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .leftCol ; begin next area check
cmp ECX, 0 ; Find value of space counter
jg .leftCol ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 9 ; store location of empty space
inc ECX ; increment space counter
jmp .found
;;;;;;;;;;;;;;;;BEGIN NEXT AREA CHECK;;;;;;;;;;;;;;;
.leftCol:
xor ECX, ECX ; set space counter back to 0
.lc1:
mov EAX, [board] ; Top left cell
cmp EAX, EBX ; If equal to selected symbol,
je .lc2 ; move to next
cmp EAX, 0 ; if not 0 either, must be other symbol
jne .centCol ; begin next area check
mov EDX, 1 ; store location of empty space
inc ECX ; increment space counter
.lc2:
mov EAX, [board + 12] ; Middle left cell
cmp EAX, EBX ; If equal to selected symbol,
je .lc3 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .centCol ; begin next area check
cmp ECX, 0 ; find value of space counter
jg .centCol ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 4 ; store location of empty space
inc ECX ; increment space counter
.lc3:
mov EAX, [board + 24] ; Bottom left cell
cmp EAX, EBX ; If equal to selected symbol
je .found ; free space has been found
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .centCol ; begin next area check
cmp ECX, 0 ; Find value of space counter
jg .centCol ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 7 ; store location of empty space
inc ECX ; increment space counter
jmp .found
;;;;;;;;;;;;;;;BEGIN NEXT AREA CHECK;;;;;;;;;;;;;;;;;;
.centCol:
xor ECX, ECX ; reset space counter
.cc1:
mov EAX, [board + 4] ; Top Middle cell
cmp EAX, EBX ; If equal to selected symbol,
je .cc2 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .rightCol ; begin next area check
mov EDX, 2 ; store location of empty space
inc ECX
.cc2:
mov EAX, [board + 16] ; Middle Center cell
cmp EAX, EBX ; If equal to selected symbol,
je .cc3 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .rightCol ; begin next area check
cmp ECX, 0 ; Find value of space counter
jg .rightCol ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 5 ; store location of empty space
inc ECX ; increment space counter
.cc3:
mov EAX, [board + 28] ; Bottom Middle cell
cmp EAX, EBX ; If equal to selected symbol,
je .found ; free space has been found previously
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .rightCol ; begin next area check
cmp ECX, 0 ; Find value of space counter
jg .rightCol ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 8 ; store location of empty space
inc ECX ; increment space counter
jmp .found ; return location found
;;;;;;;;;;;;;;;;BEGIN NEXT AREA CHECK;;;;;;;;;;;;;;;;;
.rightCol:
xor ECX, ECX ; reset space counter
.rc1:
mov EAX, [board + 8] ; Top Right cell
cmp EAX, EBX ; If equal to selected symbol,
je .rc2 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .lrDi ; begin next area check
mov EDX, 3 ; store location of empty space
inc ECX ; increment space counter
.rc2:
mov EAX, [board + 20] ; Middle Right cell
cmp EAX, EBX ; If equal to selected symbol,
je .rc3 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .lrDi ; begin next area check
cmp ECX, 0 ; Find value of space counter
jg .lrDi ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 6 ; store location of empty space
inc ECX ; increment space counter
.rc3:
mov EAX, [board + 32] ; Bottom right cell
cmp EAX, EBX ; If equal to selected symbol,
je .found ; space has been found previously
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .lrDi ; begin next area check
cmp ECX, 0 ; find value of space counter
jg .lrDi ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 9 ; store location of empty space
inc ECX ; increment space counter
jmp .found ; return location found
;;;;;;;;;;;;;;;;BEGIN NEXT AREA CHECK;;;;;;;;;;;;;;;;;
.lrDi:
xor ECX, ECX ; reset space counter
.lr1:
mov EAX, [board] ; Top left cell
cmp EAX, EBX ; If equal to selected symbol,
je .lr2 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .rlDi ; begin next area check
mov EDX, 1 ; store location of empty space
inc ECX ; increment space counter
.lr2:
mov EAX, [board + 16] ; Middle center cell
cmp EAX, EBX ; If equal to selected symbol,
je .lr3 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .rlDi ; begin next area check
cmp ECX, 0 ; Find value of space counter
jg .rlDi ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 5 ; store location of empty space
inc ECX ; increment space counter
.lr3:
mov EAX, [board + 32] ; Bottom right cell
cmp EAX, EBX ; If equal to selected symbol,
je .found ; space has been found previously
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .rlDi ; begin next area check
cmp ECX, 0 ; find value of space counter
jg .rlDi ; if greater than 0, move to next check (multiple spaces found)
mov EDX, 9 ; store location of empty space
inc ECX ; increment space counter
jmp .found ; return location found
;;;;;;;;;;;;;;;;;BEGIN NEXT AREA CHECK;;;;;;;;;;;;;;;;
.rlDi:
xor ECX, ECX ; reset space counter
.rl1:
mov EAX, [board + 8] ; Top right cell
cmp EAX, EBX ; If equal to selected symbol
je .rl2 ; move to next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .notFound ; no winning move found
mov EDX, 3 ; store location of empty space
inc ECX ; increment space counter
.rl2:
mov EAX, [board + 16] ; Middle center cell
cmp EAX, EBX ; if equal to selected symbol
je .rl3 ; move ot next
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .notFound ; no winning move found
cmp ECX, 0 ; Find value of space counter
jg .notFound ; If greater than 0, move to next check (multiple spaces found
mov EDX, 5 ; store location of empty space
inc ECX ; increment space counter
.rl3:
mov EAX, [board + 24] ; Bottom left cell
cmp EAX, EBX ; If equal to selected symbol
je .found ; free space found previously
cmp EAX, 0 ; If not 0 either, must be other symbol
jne .notFound ; no winning move found
cmp ECX, 0 ; Find value of space counter
jg .notFound ; no winning move found (multiple spaces found)
mov EDX, 7 ; store location of empty space
inc ECX ; increment space counter
jmp .found ; return free space
;;;;;;;;;;;;;;;;;END CONDITIONS;;;;;;;;;;;;;;;;
.notFound:
xor EAX, EAX ;return 0 in EAX
jmp .end
.found:
mov EAX, EDX ;put return value in EAX
jmp .end
.end: ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END checkWin
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; EAX = 0 if no win, 1 if computer win, 2 if player win, 3 if tie
calcWin:;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Begin calcWin
mov EBX, X_VAL
call checkWin
cmp EAX, 1 ; If X won, EAX will be 1
je .XWin
mov EBX, O_VAL
call checkWin
cmp EAX, 1 ; If O won, EAX will be 1
je .OWin
call checkTie
cmp EAX, 1 ; If there are no empty space, EAX will be 1
je .tie
jmp .noWin ; Else there's no endgame yet
.XWin: ; If X won
mov EAX, [playSymb] ; Put the player's symbol in EAX
cmp EAX, X_VAL ; See if the player won
je .playWin ; If so, go to player win
jmp .compWin ; Else go to computer win
.OWin: ; If O won
mov EAX, [playSymb] ; Put the player's symbol in EAX
cmp EAX, O_VAL ; See if the player won
je .playWin ; if so, go to player win
jmp .compWin ; Else go to computer win
.compWin: ; if the computer won
mov [winner], DWORD 1
mov EAX, 1 ; Return 1
jmp .end
.playWin: ; if the player won
mov [winner], DWORD 2
mov EAX, 2 ; Return 2
jmp .end
.tie:
mov [winner], DWORD 3
mov EAX, 3 ; Return 3
jmp .end
.noWin:
xor EAX, EAX ; Return 0
jmp .end
.end:
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END calcWin
checkWin:;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; BEGIN checkWin
.topRow:
mov EAX, [board] ; Top left cell
cmp EAX, EBX
jne .centRow
mov EAX, [board + 4] ; Top center cell
cmp EAX, EBX
jne .centRow
mov EAX, [board + 8] ; Top right cell
cmp EAX, EBX
jne .centRow
jmp .win
.centRow:
mov EAX, [board + 12] ; Middle left cell
cmp EAX, EBX
jne .botRow
mov EAX, [board + 16] ; Middle center cell
cmp EAX, EBX
jne .botRow
mov EAX, [board + 20] ; Middle right cell
cmp EAX, EBX
jne .botRow
jmp .win
.botRow:
mov EAX, [board + 24] ; Middle left cell
cmp EAX, EBX
jne .leftCol
mov EAX, [board + 28] ; Middle center cell
cmp EAX, EBX
jne .leftCol
mov EAX, [board + 32] ; Middle right cell
cmp EAX, EBX
jne .leftCol
jmp .win
.leftCol:
mov EAX, [board] ; Middle left cell
cmp EAX, EBX
jne .centCol
mov EAX, [board + 12] ; Middle center cell
cmp EAX, EBX
jne .centCol
mov EAX, [board + 24] ; Middle right cell
cmp EAX, EBX
jne .centCol
jmp .win
.centCol:
mov EAX, [board + 4] ; Middle left cell
cmp EAX, EBX
jne .rightCol
mov EAX, [board + 16] ; Middle center cell
cmp EAX, EBX
jne .rightCol
mov EAX, [board + 28] ; Middle right cell
cmp EAX, EBX
jne .rightCol
jmp .win
.rightCol:
mov EAX, [board + 8] ; Middle left cell
cmp EAX, EBX
jne .lrDi
mov EAX, [board + 20] ; Middle center cell
cmp EAX, EBX
jne .lrDi
mov EAX, [board + 32] ; Middle right cell