我正在制作井字游戏,并正在执行检查玩家是否获胜的功能。出于测试目的,我让程序在A玩家获胜时退出。刚开始时,我使程序仅检查第一行:
0,0,0 ==>此行0、0、00,0,0
当我选择程序检查的位置之一时(每次选择一个位置时,每次将A位置更改为X或O时,程序都会存在,例外在第一行)。该函数的代码在末尾
我不想包含整个代码,因为我不仅将其打印到屏幕上,还以图形模式绘制它(ah = 13h int 10h)我将仅向您展示主循环和仅过滤重要内容的checkIfWon函数。这是代码:董事会的定义:
.data
board db 0,0,0
db 0,0,0
db 0,0,0
db '$'
CheckIfWon函数:
checkIfWon proc
; check rows
; Equal to: if (board[0] == board[1] && board[1] == board[2])
movsx eax, byte ptr [board]
movsx ecx, byte ptr [board+1]
cmp eax, ecx
jne cont
movsx eax, byte ptr [board]
movsx ecx, byte ptr [board+2]
cmp eax, ecx
jne cont
mov ax, 4c00h
int 21h
cont:
ret
ret
endp checkIfWon
.386
; mainloop
mainloop:
; check for exit button (ESC)...
; get mouse button and cursor position...
;;;;;;;;;;;;;;;;;;;;;;; Check if mouse is on one of the positions
box0:
; check if it got clicked...
drawXplayer0:
; draw X
jmp boxHitten
drawOplayer0:
; draw o
jmp boxHitten
box1:
; check if it got clicked...
drawXplayer1:
; draw X
jmp boxHitten
drawOplayer1:
; draw o
jmp boxHitten
box2:
; check if it got clicked...
drawXplayer2:
; draw X
jmp boxHitten
drawOplayer2:
; draw o
jmp boxHitten
box3:
; check if it got clicked...
drawXplayer3:
; draw X
jmp boxHitten
drawOplayer3:
; draw o
jmp boxHitten
box4:
; check if it got clicked...
drawXplayer4:
; draw X
jmp boxHitten
drawOplayer4:
; draw o
jmp boxHitten
box5:
; check if it got clicked...
drawXplayer5:
; draw X
jmp boxHitten
drawOplayer5:
; draw o
jmp boxHitten
box6:
; check if it got clicked...
drawXplayer6:
; draw X
jmp boxHitten
drawOplayer6:
; draw o
jmp boxHitten
box7:
; check if it got clicked...
drawXplayer7:
; draw X
jmp boxHitten
drawOplayer7:
; draw o
jmp boxHitten
box8:
; check if it got clicked...
drawXplayer8:
; draw X
jmp boxHitten
drawOplayer8:
; draw o
jmp boxHitten
boxHitten:
; Fix overdrawn borders
call drawBoard
; set cursor position
mov dl, 1
mov dh, 1
mov bh, 0 ;Display page
mov ah, 02h ;SetCursorPosition
int 10h
;
; change player
; if player='x': player='O' else player=x'
; here im playing a sound that tells the user he clicked
; delay
push 500 ; 0.5 secs
call delay
call checkIfWon
jmp mainloop
takenError:
push 14000 ; frequency for bad answer
push 200 ; duration milliseconds
call play
; delay
push 1000
call delay
jmp mainloop
程序没有退出(如我想要的那样:存在(不是我想要的):
程序跳过它的原因可能是什么?
编辑:我想我知道原因,正在调查...
我想出了解决问题的方法:事情是,因为我将板定义为
board db 0,0,0分贝0,0,0分贝0,0,0
每次检查都是简单的,因为它们都是0。当我单击该位置的东西时,面板变为[]
板X,0,00,0,00,0,0
然后它没有通过检查。解决方案代码:
checkIfWon proc
; check rows
; Equal to: if (board[0] != 0 && board[0] == board[1] && board[1] == board[2])
movsx eax, byte ptr [board]
movsx ecx, byte ptr [board+1]
cmp eax, 0
je cont
cmp eax, ecx
jne cont
movsx eax, byte ptr [board]
movsx ecx, byte ptr [board+2]
cmp eax, ecx
jne cont
mov ax, 4c00h
int 21h
cont:
ret
ret
endp checkIfWon