0

さて、私が構築している三目並べゲームについて別の質問があります。: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" のいずれかを保持します。

4

2 に答える 2

0

あなたは見てみたいと思うかもしれません

http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe_AI.html

三目並べのソリューションを構築するための手順を順を追って説明します。ここで行ったことと同様のテーブルベースの実装を導入することから始めて、ツリーベースのソリューションの使用を開始することに注意してください。

于 2013-10-05T03:05:30.137 に答える
0

他にもいくつかの「バグ」を見つけましたが、あなたの質問に答えるために...

あなたの問題はここにあるようです:

elif slot[2] == user_team and slot[4] == user_team and (slot[6] != user_team) \
and (slot[6] != computer_team):

提供したパターンに従うと4, 2, 3, 7、ボードは次のようになります。

 O | O | X
-----------
 X | X | O
-----------
 O | X | 8

上記の条件を満たさないもの。たとえば、あなたが書いたものは次のとおりです。

if Slot2 = User, Slot4 = User, Slot6 != User, and Slot6 != Computer either... 

また、ボードには次のように書かれています。

Slot2 = User, Slot4 = User, Slot6 != User, and Slot6 = Computer

私は192行目から削除and (slot[6] != computer_team)しましたが、今はうまく機能しているようです。

これで最初の問題 (多くの場合があります) が修正されたかどうか教えてください (他にもエラッタのポイントがいくつかありますが、それをうまく処理すれば、すぐにすべてを見つけることができます)。

これはしっかりしています。よくできました。

于 2013-10-05T03:56:13.407 に答える