-4
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()
4

1 回答 1

2

我很确定,无论是什么问题,都存在于您没有向我们展示的代码中。

endGame为了简单起见,我没有测试您的功能。但是我粘贴了您的其他代码,并添加了以下内容:

board = [0, 1, 2, 3, 4, 5, 6, 7, 8]
gameOver = False
def endGame():
    print 'endGame called'
for i in range(9):
    getCSpot()
    print(board)
    didwin('O')
    if gameOver:
        print 'gameOver'
        break

结果是:

[0, 'O', 2, 3, 4, 5, 6, 7, 8]
[0, 'O', 2, 3, 'O', 5, 6, 7, 8]
[0, 'O', 2, 3, 'O', 5, 'O', 7, 8]
[0, 'O', 2, 3, 'O', 5, 'O', 'O', 8]
Better luck next time, you lost!
endGame called
gameOver

当然序列是随机的,所以每次都不完全相同,但它总是一次用“O”替换一个随机数,直到有一个 3-in-a-row,然后告诉我我输了。

因此,它完全按预期工作。如果您的代码不起作用,则说明您做错了其他事情。也许您正在设置board或初始设置中有其他错误?

添加你的displayBoardendGame代码,它会一直工作,直到你说'y'来玩另一个游戏,此时它会调用一些名为但不存在的函数,从而game1引发.gameNameError

我还在你的 start 函数中看到(从你删除的评论中,所以这是从内存中看到的)你正在调用一个getspot()在游戏循环中调用的函数,而不是调用getCSpot(). 如果getspot在某处定义,则问题可能是您有一个正确的功能和一个不正确的功能。或者它可能没有在任何地方定义,并且“不工作”和“根本没有把 O 放在字符串中”你的意思是“没有被调用,因为在我有机会之前游戏停止了异常” ?

现在您已经发布了整个内容,但存在一些明显的问题。

首先,你永远不会getCSpot在任何地方打电话。这就是为什么它什么都不做。大概你想要这个:

def mainGame():
    gameOver = False
    while gameOver == False:
        displayBoard()
        getspot()
        didwin('X')
        didwin('O')
        getCSpot()
        didwin('X')
        didwin('O')

此外,在 中getspot,您的if陈述都是错误的。如果 ,您将允许移动board[playerSpot] != 'X',如果 也允许移动board[playerSpot] != 'O'- 换句话说,总是。然后你将把玩家算作移动,除非playerSpot == 'O'它是假的——它总是这样,因为它应该是一个数字。

我想你想要这个:

    if board[playerSpot] == 'X':
        playerSpot = raw_input('You have already chosen that spot. Please choose another. ')
    elif board[playerSpot] == 'O':
        playerSpot = raw_input('The computer chose that spot already. Please choose another. ')
    else:
        board[playerSpot] = 'X'
        spotchosen = True
于 2012-12-14T22:42:49.303 回答