Gomoku (连续 5 次) Negamax AI 不会阻止我获胜

Gomoku (5 in a row) Negamax AI not stopping me from winning

提问人:eligolf 提问时间:11/11/2023 更新时间:11/11/2023 访问量:19

问:

我正在为 Gomoku 开发 AI(连续 5 次),但当我获胜时,AI 没有阻止我,这让我遇到了一些问题。它似乎只是在玩随机动作。

negamax 基本上取自我做的 connect 4 游戏,其中 AI 似乎玩得很好并阻止我获胜。以下是我用于 AI Negamax 函数的代码:

private int bestMove;

public int GetBestMove(Gamestate _gamestate, int _maxDepth)
{
    // Run negamax function
    int originalAlpha = -1000000000;
    int originalBeta = 1000000000;
    int score = Negamax(_gamestate, _maxDepth, originalAlpha, originalBeta, _gamestate.playerToMove);

    return bestMove;
}

public int Negamax(Gamestate _gamestate, int depth, int alpha, int beta, int currentPlayer)
{
    // Check for terminal node
    int winningPlayer = _gamestate.GetWinner();
    if (depth == 0 || winningPlayer != -1 || _gamestate.movesMade == _gamestate.size)
    {
        if (winningPlayer != -1)
        {
            if (currentPlayer == winningPlayer)
            {
                return 10000000;
            }
            else
            {
                return -10000000;
            }
        }
        else if (_gamestate.movesMade == _gamestate.size)
        {
            return 0;
        }
        else
        {
            return 0; // Just to test the basic win/loss finding
            //return evaluator.EvaluatePosition(_gamestate);
        }
    }

    // Initialize score and bestMove and loop through all possible moves
    int score = int.MinValue;
    List<int> possibleMoves = _gamestate.possibleMoves;
    bestMove = possibleMoves[0];

    for (int i = 0; i < possibleMoves.Count; i++)
    {
        int move = possibleMoves[i];

        // Make the move, score it, and then unmake the move again
        _gamestate.MakeMove(move);
        int newScore = -Negamax(_gamestate, depth - 1, -beta, -alpha, 1 - currentPlayer);
        _gamestate.UnmakeMove(move);

        if (newScore > score)
        {
            score = newScore;
            bestMove = move;
        }

        alpha = Mathf.Max(alpha, score);
        if (alpha >= beta)
        {
            break;
        }
    }

    return score;
}

**怎么了**

  • 为了调试,我在 5x5 板上玩。当我连续有 4 个时,AI 不会阻止我。
  • AI 似乎在玩随机移动,即使我在无终端节点上的评估现在只返回 0。

我试过什么

  • 我已经检查了我的 GetWinner() 函数是否正确并返回获胜的玩家(第一个玩家为 0,另一个玩家为 1)
  • 我的移动生成只是当我移动时,我从所有方块列表中删除最新播放的方块。并在我撤消移动时再次添加它。所以目前似乎工作正常。
C# 五子

评论


答:

-1赞 Ali Shahzad 11/11/2023 #1

评估功能:确保它不仅检查输赢,还检查每个玩家拥有的 2、3 或 4 颗石头的“开放式”行数。这可以让你的人工智能有远见地阻止你的获胜动作。 优先动作:你的人工智能应该优先考虑阻止对手在下一回合获胜的动作。因此,如果有一个点为您完成了一行 4 个,AI 应该将其视为一个大红旗并首先移动到那里。 搜索深度:有时,人工智能需要向前看几步。确保您的_maxDepth参数足以让 AI 预测未来几步的潜在输赢。 调试:当连续有四个调试器时,使用调试器单步执行 Negamax 函数。观察它为移动生成的分数——那里可能有线索。 请记住,强大的 AI 的关键在于评估的细节。这与随机移动无关;这是关于聪明人的!

如果这个建议能帮助你的人工智能在游戏中崭露头角,请把它看作是一场胜利并接受它。😊

祝你好运, 阿里·沙赫扎德

评论

0赞 eligolf 11/11/2023
正如代码中提到的,我的评估函数现在只返回 0,因为我想检查它是否找到输赢,而不会在其他方面变得太复杂。我的深度在 10 左右,深度 2 应该足以让它在下一步行动中输/赢。