add draw condition and fix not-so good but also not terrible issue with rbx register not being reserved in makeAIMove
This commit is contained in:
32
game.s
32
game.s
@@ -1,11 +1,9 @@
|
||||
; TODO out of moves check
|
||||
|
||||
global _start
|
||||
|
||||
section .text
|
||||
|
||||
checkVictory:
|
||||
mov rax, 0
|
||||
mov eax, 0
|
||||
|
||||
mov cl, [fieldBuffer + 12]
|
||||
and cl, [fieldBuffer + 14]
|
||||
@@ -64,25 +62,26 @@ checkVictory:
|
||||
|
||||
makeAIMove:
|
||||
rdtsc
|
||||
mov rbx, rax
|
||||
mov r8, rax
|
||||
.loop:
|
||||
; RAND from wiki https://en.wikipedia.org/wiki/Linear_congruential_generator. Numerical Recipes ranqd1, Chapter 7.1, §An Even Quicker Generator, Eq. 7.1.6 parameters from Knuth and H. W. Lewis
|
||||
mov rdx, 1664525
|
||||
mul rdx
|
||||
add rax, 1013904223
|
||||
mov rbx, rax
|
||||
mov r8, rax
|
||||
|
||||
; clamping to 1-3 range and moving to rdi
|
||||
mov rsi, 3
|
||||
mov rax, 0
|
||||
mov ah, bh
|
||||
mov al, r8b
|
||||
mov dx, 0
|
||||
div si ;TODO for some reason div sil causes division by zero
|
||||
inc dx
|
||||
mov di, dx
|
||||
shl di, 8
|
||||
mov rax, 0
|
||||
mov ah, bl
|
||||
mov ax, r8w
|
||||
shr ax, 8
|
||||
mov dx, 0
|
||||
div si
|
||||
inc dx
|
||||
@@ -91,7 +90,7 @@ makeAIMove:
|
||||
call placeChar
|
||||
cmp rax, 0
|
||||
je .return
|
||||
mov rax, rbx
|
||||
mov rax, r8
|
||||
jmp .loop
|
||||
|
||||
.return:
|
||||
@@ -175,6 +174,7 @@ _start:
|
||||
mov rdx, readInputsMessageLen ; size
|
||||
syscall ;
|
||||
|
||||
mov rbp, 0 ; number of moves done
|
||||
.loop:
|
||||
;reading input
|
||||
call readXY
|
||||
@@ -196,12 +196,17 @@ _start:
|
||||
; failed to place a char
|
||||
jne .printOccupiedError
|
||||
|
||||
inc rbp
|
||||
|
||||
;check win condition
|
||||
mov rdi, 'x'
|
||||
call checkVictory
|
||||
cmp rax, 0
|
||||
jne .printYouWon
|
||||
|
||||
cmp rbp, 5 ; player did 5 moves, time to go
|
||||
je .printDraw
|
||||
|
||||
;drawing field again
|
||||
call drawField
|
||||
|
||||
@@ -274,6 +279,15 @@ _start:
|
||||
syscall ;
|
||||
jmp .exit
|
||||
|
||||
.printDraw:
|
||||
call drawField
|
||||
mov rax, 1 ; sys_write
|
||||
mov rdi, 1 ; out
|
||||
mov rsi, drawMessage ; str
|
||||
mov rdx, drawMessageLen ; size
|
||||
syscall ;
|
||||
jmp .exit
|
||||
|
||||
.printYouLost:
|
||||
call drawField
|
||||
mov rax, 1 ; sys_write
|
||||
@@ -301,6 +315,8 @@ section .rodata
|
||||
youWonMessageLen: equ $ - youWonMessage
|
||||
youLostMessage: db "you lost T_T", 0xA
|
||||
youLostMessageLen: equ $ - youLostMessage
|
||||
drawMessage: db "it's a draw", 0xA
|
||||
drawMessageLen: equ $ - drawMessage
|
||||
aiThinkingMessage: db "AI thinking"
|
||||
aiThinkingMessageLen: equ $ - aiThinkingMessage
|
||||
aiThinkingDotMessage: db "."
|
||||
|
||||
Reference in New Issue
Block a user