さて、私が構築している三目並べゲームについて別の質問があります。:P 私が取り組んでいるのは、無敵の AI をコーディングすることです。したがって、私が今持っているのは、コンピューターが最初に起動すると、勝つか引き分けるために必要な手順に従うということですが、方法はわかりませんアルゴリズム、主に minimax() を使用する必要があり (これは私が作成する最初の実際のプログラムであるため)、すべての if - ステートメントを単純化する方法が完全にはわかりません。
これまでの AI のコードは次のとおりです。
if strategy == False:
slot[0] = computer_team
if slot[1] == user_team or slot [2] == user_team and (slot[3] != user_team) \
and (slot[3] != computer_team):
slot[3] = computer_team
if slot[6] == user_team and (slot[4] != user_team) and (slot[4] != computer_team):
slot[4] = computer_team
else:
return
if slot[3] == user_team or slot[4] == user_team or slot[6] == user_team \
and (slot[1] != user_team) and (slot[1] != computer_team):
slot[1] = computer_team
if slot[2] == user_team and (slot[4] != user_team) and (slot[4] != computer_team):
slot[4] = computer_team
elif slot[2] == user_team and slot[4] == user_team and (slot[6] != user_team) \
and (slot[6] != computer_team):
slot[6] = computer_team
if slot[4] == user_team and (slot[5] != user_team) and (slot[5] != computer_team):
slot[5] = computer_team
elif slot[7] == user_team and slot[5] == computer_team and (slot[8] != user_team) and (slot[8] != computer_team):
slot[8] = computer_team
else:
return
else:
return
問題であることがわかったのは、if ステートメントが入れ子になると実行が停止することです (この場合、最後の elif スロット [7])。このコードが非効率的であることはわかっていますが、それを実行する唯一の方法です。(ここでステートメントまたは範囲をどのように使用するかわかりません)。したがって、アルゴリズムに関する提案や、ネストが混乱している場合にこれを単純化する方法があれば、喜んで聞いてください。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")
また、user_team/computer_team は、プレイヤーがどちらを使用するかによって、"X" または "O" のいずれかを保持します。