-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 をまったく入れません。O または X が 3 つ並んでいる場合に勝者が存在するかどうかを判断するコードの別の部分があり、それも機能しません。これがそのコードです。どんな助けでも大歓迎です。

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

もちろん、シーケンスはランダムなので、毎回完全に同じというわけではありませんが、3列になるまで、一度に1つの乱数を常に「O」に置き換えてから、負けたと通知します。

したがって、意図したとおりに機能します。コードが機能しない場合は、別の問題が発生しています。セットアップboard中か、初期セットアップで何か問題がありますか?

displayBoardあなたとコードを追加するとendGame、別のゲームをプレイするために「y」と言うまで機能します。その時点で、名前が付けられgame1game存在しないいくつかの関数が呼び出され、が発生しNameErrorます。

また、start関数(削除してから削除したコメントから、これはメモリからです)で、をgetspot()呼び出す代わりに、ゲームループで呼び出される関数を呼び出していることもわかりましたgetCSpot()。がどこかで定義されている場合getspot、問題はおそらく、1つの正しい関数と1つの間違った関数があることです。あるいは、どこにも定義されておらず、「機能していない」および「文字列に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'次に、 falseでない限り、プレーヤーを移動したものとしてカウントします。これは、数値であると想定されているため、常にそうです。

私はあなたがこれを望んでいると思います:

    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 に答える