def getCSpot():
global board
global cspot
spotchosen = False
while spotchosen == False:
spotchosen = False
cspot = random.randint(0, 8)
if board[cspot] == 'X' or board[cspot] == 'O':
cspot = random.randint(0, 8)
else:
spotchosen = True
board[cspot] = 'O'
我真的不明白为什么这不起作用。它根本不会将 O 放入字符串中。我有另一部分代码确定当连续有 3 个 O 或 X 时是否有赢家,这也不起作用,这是该代码。任何帮助深表感谢。
def didwin(player):
global gameOver
if (board[0] == player and board[1] == player and board[2] == player or
board[3] == player and board[4] == player and board[5] == player or
board[6] == player and board[7] == player and board[8] == player or
board[0] == player and board[3] == player and board[6] == player or
board[1] == player and board[4] == player and board[7] == player or
board[2] == player and board[5] == player and board[8] == player or
board[0] == player and board[4] == player and board[8] == player or
board[2] == player and board[4] == player and board[6] == player):
gameOver = True
if player == 'X':
print 'congratulations! You won!!'
endGame()
else:
print 'Better luck next time, you lost!'
endGame()
出于参考目的,这里是 endGame 函数。
def endGame():
global board
displayBoard()
answer = ' '
while answer == ' ':
print 'Would you like to play another game?'
answer = raw_input('Y/N')
if answer == 'Y' or answer == 'y' or answer == 'Yes' or answer == 'yes':
board = [0, 1, 2, 3, 4, 5, 6, 7, 8]
game1()
game()
elif answer == 'N' or answer == 'n' or answer == 'No' or answer == 'no':
exit()
编辑:以下是我的整个代码,从头到尾没有编辑。
board = [0, 1, 2,
3, 4, 5,
6, 7, 8]
import random
def displayBoard():
global board
print board[0], '|', board[1], '|', board[2]
print '----------'
print board[3], '|', board[4], '|', board[5]
print '----------'
print board[6], '|', board[7], '|', board[8]
def getspot():
global board
spotchosen = False
while spotchosen == False:
spotchosen = False
playerSpot = int(raw_input('Where would you like to go? '))
if board[playerSpot] != 'X':
board[playerSpot] = 'X'
if board[playerSpot] != 'O':
board[playerSpot] = 'X'
if playerSpot == 'X':
playerSpot = raw_input('You have already chosen that spot. Please choose another. ')
if playerSpot == 'O':
playerSpot = raw_input('The computer chose that spot already. Please choose another. ')
else:
spotchosen = True
def getCSpot():
global board
global cspot
spotchosen = False
while spotchosen == False:
spotchosen = False
cspot = random.randint(0, 8)
if board[cspot] == 'X' or board[cspot] == 'O':
cspot = random.randint(0, 8)
else:
spotchosen = True
board[cspot] = 'O'
def endGame():
global board
displayBoard()
answer = ' '
while answer == ' ':
print 'Would you like to play another game?'
answer = raw_input('Y/N')
if answer == 'Y' or answer == 'y' or answer == 'Yes' or answer == 'yes':
board = [0, 1, 2, 3, 4, 5, 6, 7, 8]
game1()
game()
elif answer == 'N' or answer == 'n' or answer == 'No' or answer == 'no':
exit()
def didwin(player):
global gameOver
if (board[0] == player and board[1] == player and board[2] == player or
board[3] == player and board[4] == player and board[5] == player or
board[6] == player and board[7] == player and board[8] == player or
board[0] == player and board[3] == player and board[6] == player or
board[1] == player and board[4] == player and board[7] == player or
board[2] == player and board[5] == player and board[8] == player or
board[0] == player and board[4] == player and board[8] == player or
board[2] == player and board[4] == player and board[6] == player):
gameOver = True
if player == 'X':
print 'congratulations! You won!!'
endGame()
else:
print 'Better luck next time, you lost!'
endGame()
else:
gameOver = False
def mainGame():
gameOver = False
while gameOver == False:
displayBoard()
getspot()
didwin('X')
didwin('O')
mainGame()