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