1

Connect 4 に NegaMax AI を実装しようとしています。アルゴリズムがうまく機能する場合があり、AI が勝つことができます。ただし、相手の 3 連続を完全にブロックできなかったり、3 連続でウィニング ショットを打てなかったりする場合があります。

評価関数は、グリッド (水平、垂直、斜め上、斜め下) を反復処理し、4 つの正方形のすべてのセットを取得します。次に、これらの各セット内をチェックし、これに基づいて評価します。

ここで提供される評価コードに基づいて関数を作成しました: http://blogs.skicelab.com/maurizio/connect-four.html

私の機能は次のとおりです。

    //All sets of four tiles are evaluated before this
    //and values for the following variables are set.
    if (redFoursInARow != 0)
    {
        redScore = INT_MAX;
    }
    else 
    {
        redScore = (redThreesInARow * threeWeight) + (redTwosInARow * twoWeight);
    }
    int yellowScore = 0;
    if (yellowFoursInARow != 0)
    {
        yellowScore = INT_MAX;
    }
    else
    {
        yellowScore = (yellowThreesInARow * threeWeight) + (yellowTwosInARow * twoWeight);
    }
    int finalScore = yellowScore - redScore;

    return turn ? finalScore : -finalScore; //If this is an ai turn, return finalScore. Else return -finalScore.

私の negamax 関数は次のようになります。

    inline int NegaMax(char g[6][7], int depth, int &bestMove, int row, int col, bool aiTurn)
{
    {
        char c = CheckForWinner(g);
        if ('E' != c || 0 == depth)
        {
            return EvaluatePosition(g, aiTurn);
        }
    }
    int bestScore = INT_MIN;

    for (int i = 0; i < 7; ++i)
    {
        if (CanMakeMove(g, i)) //If column i is not full...
        {
            {
                //...then make a move in that column.
                //Grid is a 2d char array.
                //'E' = empty tile, 'Y' = yellow, 'R' = red.
                char newPos[6][7];
                memcpy(newPos, g, sizeof(char) * 6 * 7);
                int newRow = GetNextEmptyInCol(g, i);
                if (aiTurn)
                {
                    UpdateGrid(newPos, i, 'Y');
                }
                else 
                {
                    UpdateGrid(newPos, i, 'R');
                }
                int newScore = 0; int newMove = 0;
                newScore = NegaMax(newPos, depth - 1, newMove, newRow, i, !aiTurn);
                newScore = -newScore;
                if (newScore > bestScore)
                {
                    bestMove = i;
                    bestScore = newScore;
                }
            }
        }
    }
    return bestScore;
}

接続 4 が解決されたことは承知していますが、これには間違いなくより良い方法がありますが、これを修正/改善するためのヘルプや提案は大歓迎です. ありがとう!

4

0 に答える 0