我正在尝试用汇编语言制作Tic Tac Toe如何切换播放器? 这是我的代码。
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.data
tic BYTE "TIC TAC TOE GAME"
player BYTE "PLAYER 1 (X) : PLAYER 2 (O)"
myarray1 BYTE '1' , ' ' , '|' , ' ' , '2' , ' ' , '|' ,' ' ,'3'
myarray2 BYTE '4' , ' ' , '|' , ' ' , '5' , ' ' , '|' ,' ' ,'6'
myarray3 BYTE '7', ' ' , '|' , ' ' , '8' , ' ' , '|' ,' ' ,'9'
choise BYTE 'X','O'
player1 BYTE "Player 1 select your position ",0
player2 BYTE "Player 2 select your position ",0
info byte ?
turn BYTE 2
.code
main PROC
mov bl,1
mov ecx,9
L13:
push ecx
call BOARD
call GAME
pop ecx
call clrscr
loop L13
BOARD proc
;TO DISPLAY TIC TAC TOE GAME
mov ecx, LENGTHOF tic
mov esi,Offset tic
L0:
mov al,[esi]
call writechar
inc esi
loop l0
call crlf
call crlf
;TO DISPLAY PLAYER INFO
mov ecx,LENGTHOF player
mov esi,OFFSET player
l6:
mov al,[esi]
call writechar
inc esi
loop L6
call crlf
call crlf
;LOOP TO DISPLAY FIRST ARRAY
mov ecx,LENGTHOF myarray1
mov esi,OFFSET myarray1
L1:
mov al,[esi]
call writechar
inc esi
loop L1
call crlf
;LOOP TO DISPLAY SECOND ARRAY
mov ecx,LENGTHOF myarray2
mov esi,OFFSET myarray2
L2:
mov al,[esi]
call writechar
inc esi
loop L2
call crlf
;LOOP TO DISPLAY THIRD ARRAY
mov ecx,LENGTHOF myarray3
mov esi,OFFSET myarray3
L3:
mov al,[esi]
call writechar
inc esi
loop L3
call crlf
call crlf
ret
BOARD endp
GAME proc
mov ecx,LENGTHOF player1
mov esi,OFFSET player1
L4:
mov al,[esi]
call writechar
inc esi
loop L4
call crlf
call readint
mov info,al
cmp al,1
je one
cmp al,2
je two
cmp al,3
je three
cmp al,4
je four
cmp al,5
je five
cmp al,6
je six
cmp al,7
je seven
cmp al,8
je eight
cmp al,9
je nine
one:
mov al,choise
xchg al,[myarray1]
ret
two:
mov al,choise
xchg al,[myarray1+4]
ret
three:
mov al,choise
xchg al,[myarray1+8]
ret
four:
mov al,choise
xchg al,[myarray2]
ret
five:
mov al,choise
xchg al,[myarray2+4]
ret
six:
mov al,choise
xchg al,[myarray2+8]
ret
seven:
mov al,choise
xchg al,[myarray3]
ret
eight:
mov al,choise
xchg al,[myarray3+4]
ret
nine:
mov al,choise
xchg al,[myarray3+8]
ret
GAME endp
exit
main endp
end main
答案 0 :(得分:0)
.code main PROC mov bl,1 mov ecx,9 L13: push ecx call BOARD call GAME pop ecx call clrscr loop L13
主要代码中已经提供了解决播放器切换问题的提示!请注意mov bl, 1
指令,目前在您的程序中完全没有必要。您可以使用BX
寄存器来表示当前播放器。最简单的是玩家1的BX=0
和玩家2的BX=1
。
在 main 循环的每次迭代中,您必须使用BX
轻松切换xor bx, 1
中的值。
xor bx, bx ;Sets BX=0, so player 1 starts
mov ecx, 9
L13:
push ecx
call BOARD
call GAME
pop ecx
call clrscr
xor bx, 1 ;This switches players, 0 becomes 1 and 1 becomes 0
loop L13
在 GAME 程序中,您需要
显示个性化提示。您不再需要 Player2 消息。
GAME proc
mov ecx, LENGTHOF player1
mov esi, OFFSET player1
lea ax, ["1" + bx]
mov [esi + 7], al ;Modifies message to show "1" or "2"
L4:
mov al, [esi]
call writechar
inc esi
loop L4
call crlf
在9个不同场合选择正确的角色(" X"对于玩家1或" O"对于玩家2)。
您撰写mov al, choise
的任何地方,现在都需要mov al, [choise + bx]
。