所以这是我的tic tac toe python游戏,但结果是在有人获胜后,你必须在结束之前选择另一个方格,当它结束时,它永远不会显示'玩家一赢!'或者'玩家二赢了!'我不确定为什么会这样。有人可以帮忙吗?
# Tic Tac Toe Game
import time
import sys
who = 'p1'
finished = 'no'
board = [0,1,2,
3,4,5,
6,7,8]
def show():
print (board[0], '|', board [1], '|', board[2])
print ('---------')
print (board[3], '|', board [4], '|', board[5])
print ('---------')
print (board[6], '|', board [7], '|', board[8])
def checkWin():
if board[0] == 'x' and board[3] == 'x' and board[6] == 'x':
finished = 'yes'
return True
elif board[0] == 'y' and board[3] == 'y' and board[6] == 'y':
finished = 'yes'
return True
elif board[1] == 'x' and board[4] == 'x' and board[7] == 'x':
finished = 'yes'
return True
elif board[1] == 'y' and board[4] == 'y' and board[7] == 'y':
finished = 'yes'
return True
elif board[2] == 'y' and board[5] == 'y' and board[8] == 'y':
finished = 'yes'
return True
elif board[2] == 'x' and board[5] == 'x' and board[8] == 'x':
finished = 'yes'
return True
elif board[0] == 'y' and board[1] == 'y' and board[2] == 'y':
finished = 'yes'
return True
elif board[0] == 'x' and board[1] == 'x' and board[2] == 'x':
finished = 'yes'
return True
elif board[3] == 'x' and board[4] == 'x' and board[5] == 'x':
finished = 'yes'
return True
elif board[3] == 'y' and board[4] == 'y' and board[5] == 'y':
finished = 'yes'
return True
elif board[6] == 'y' and board[7] == 'y' and board[8] == 'y':
finished = 'yes'
return True
elif board[6] == 'x' and board[7] == 'x' and board[8] == 'x':
finished = 'yes'
return True
elif board[0] == 'x' and board[4] == 'x' and board[8] == 'x':
finished = 'yes'
return True
elif board[0] == 'y' and board[4] == 'y' and board[8] == 'y':
finished = 'yes'
return True
elif board[2] == 'y' and board[4] == 'y' and board[6] == 'y':
finished = 'yes'
return True
elif board[2] == 'x' and board[4] == 'x' and board[6] == 'x':
finished = 'yes'
return True
else:
return False
print ('Welcome to a two-player Tic.Tac.Toe Game!')
time.sleep(2)
print ("Just pick a spot to put your mark!")
time.sleep(1.5)
print ('Now, start!')
time.sleep(0.2)
print (show())
while checkWin() == False:
while who == 'p1':
try:
p1 = int(input('Player 1, select a spot: '))
if board[p1] != 'x' and board[p1] != 'o':
board[p1] = 'x'
who = 'p2'
break
else:
print ('This spot is taken.')
pass
except (ValueError, IndexError):
print ('That is not available. Please try again.')
print (show())
while who == 'p2':
try:
p2 = int(input('Player 2, select a spot: '))
if board[p2] != 'x' and board[p2] != 'o':
board[p2] = 'o'
who = 'p1'
break
else:
print ('This spot is taken.')
pass
except (ValueError, IndexError):
print ('That is not available. Please try again.')
print (show())
while checkWin == True:
# x
if board[0] == 'x' and board[3] == 'x' and board[6] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[1] == 'x' and board[4] == 'x' and board[7] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[2] == 'x' and board[5] == 'x' and board[8] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[0] == 'x' and board[1] == 'x' and board[2] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[3] == 'x' and board[4] == 'x' and board[5] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[6] == 'x' and board[7] == 'x' and board[8] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[0] == 'x' and board[4] == 'x' and board[8] == 'x':
print ('Player 1 won!')
sys.exit()
elif board[2] == 'x' and board[4] == 'x' and board[6] == 'x':
print ('Player 1 won!')
sys.exit()
# y
else:
print ('Player 2 won!')
sys.exit()