1

さて、私は三目並べゲームの作成に取り組んでおり、解決できないように見えるかなり悪いエラーに遭遇しました。プレイヤーが勝ちそうになった場合にコンピューターがプレイヤーをブロックする機能を作成しましたが、一度ブロックに成功すると、条件が満たされてもトリガーされなくなります。関数のコードは次のとおりです。

def block():
for t in range(0, 9, 3):
        if slot[t] == user_team and slot[t+1] == user_team and (slot[t+2] \
           != user_team) and (slot[t+2] != computer_team):
            slot[int(t+2)] = computer_team
            return
        elif slot[t+1] == user_team and slot[t+2] == user_team and (slot[t] \
             != user_team) and (slot[t] != computer_team):
            slot[int(t)] = computer_team
            return
        elif slot[t] == user_team and slot[t+2] == user_team and (slot[t+1] \
             != user_team) and (slot[t+1] != computer_team):
            slot[int(t+1)] = computer_team
            return

for t in range(3):
        if slot[t] == user_team and slot[t+3] == user_team and (slot[t + 6] \
           != user_team) and (slot[t+6] != computer_team):
            slot[int(t+6)] = computer_team
            return
        elif slot[t+3] == user_team and slot[t+6] == user_team and (slot[t] \
             != user_team) and (slot[t] != computer_team):
            slot[int(t)] = computer_team
            return
        elif slot[t] == user_team and slot[t+6] == user_team and (slot[t+3] \
             != user_team) and (slot[t+3] != computer_team):
            slot[int(t+3)] = computer_team

また、user_team と computer_team は、そのプレイヤーが X か O かを示しslot[int()] = computer_team、盤上に移動を配置するために使用されます。以下は、関数が呼び出される場所です (ここで失敗した場合に備えて)。

else:
    draw_board()
    '''win()'''
    block()
    cmove()
    turn = "user"
    if end_game() == True:
        computer_win += 1
        draw_board()
        print ("The computer has won! But... We already knew that would happen. (:")
        playing = False
    elif end_game() == "Tie":
        tie_win += 1
        draw_board()
        print ("The game is a tie. You're going to have to try harder " \
               + "\n" + "if you wish to beat the computer!" + "\n")
        playing = False
    else:
        pass

どこで私が間違っていたのか教えてくれる人がいれば、それは私の一日になります。c:

ボード (印刷物はインデントされています。ここにインデントしたくないだけです。)

def draw_board():
'''Opted to use lists so that the numbers can be replaced with either
    X or O later on and so that testing whether the game is over is simpler'''
print (" " + str(slot[0]) + " | " + str(slot[1]) + " | " + str(slot[2]))
print ("-----------")
print (" " + str(slot[3]) + " | " + str(slot[4]) + " | " + str(slot[5]))
print ("-----------")
print (" " + str(slot[6]) + " | " + str(slot[7]) + " | " + str(slot[8]))
print ("\n")

新しいエラー:

これは私が手を入れた後の私のボードです 4

X | お | バツ

お | 4 | 5

X | 7 | バツ

4 手目の後のコンピューターのボード (2 つの手、および x を置き換えます)

X | お | バツ

お | 4 | 〇

お | 7 | バツ

4

1 に答える 1

2

あなたの問題はあなたのblock機能のロジックにあると思います。

これがあなたのボードです:

0 1 2
3 4 5
6 7 8

ネストされたforループの最初のペアを見て、コードが何をするか見てみましょう。

for t in range(0,9,3):
    for y in range(1, 9, 3):

これにより、次のペアが得られますt, y:(0,1)、(0,4)、(0,7)、(3,1)、(3,4)、(3,7)、(6,1) 、(6,4)、および (6,7)。すぐに、これはあなたが意図したものではないと思います。私が知る限り、プレイヤーが 2 つのマークを続けて持っているかどうかを確認しようとしています。

forこの問題は簡単に修正できます。2 つのループは必要ありません。代わりに、tt+1、および を使用してt+2ください。

次に、行を考えます。

0 1 2

チェックする条件は 3 つあります。プレイヤーは 0 と 1、0 と 2、または 1 と 2 にマークを持っています。これらの条件のうち、0 と 1、および 1 と 2 の 2 つだけをチェックします。

さらに、ifステートメントはあなたが思っていることをしていません:

if ... and slot[y+1] != user_team and computer_team:

これは次と同等です。

if ... and (slot[y+1] != user_team) and computer_team:

computer_team'x'orであると仮定します。この場合、python はステートメント'o'と同じようTrueに使用します。ifあなたが欲しいのはこれです:

if ... and (slot[y+1] != user_team) and (slot[y+1] != computer_team):

これはおそらく、コードが一度しか機能しない理由でもあります。次回、以前に見つかった同じ行または列を評価するときに、ifステートメントはTrue再度評価され、同じスペースが再度設定されます。それは何もしていません。

列をチェックするコードにも同じ問題があります。私が指摘した問題が、コードの修正方法を理解するのに十分であることを願っています。幸運を!

于 2013-10-04T22:05:29.533 に答える