【问题标题】:Minimax algorithm for Tic Tac Toe井字游戏的 Minimax 算法
【发布时间】:2015-12-02 05:05:34
【问题描述】:

我正在尝试实现 Jason Fox (http://neverstopbuilding.com/minimax) 的极小极大算法,但它不起作用,计算机只是在随机移动。这是我编写的Game 类(我必须创建一个cloneGrid 函数,因为数组作为引用传递,有没有更好的方法来解决这个问题?):

class Game
{ 
    public enum Piece { Empty = 0, X = 1, O = 2 };

    static int[,] winConditions = new int[8, 3]
    {
        { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 },
        { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 },
        { 0, 4, 8 }, { 2, 4, 6 }
    };

    public Piece[] Grid = new Piece[9];

    public Piece CurrentTurn = Piece.X;

    int Choice = 0;

    public Piece Computer;
    public Piece Player;

    public Game()
    {
        CurrentTurn = Piece.X;
        Player = Piece.X;
    }

    public void Reset()
    {
        CurrentTurn = Piece.X;
        SetPlayer(Piece.X);
        Grid = new Piece[9];
    }

    public void SetPlayer(Piece Player)
    {
        this.Player = Player;
        this.Computer = switchPiece(Player);
    }

    public void MakeMove(int Move)
    {
        if(CurrentTurn == Player)
        {
            Grid = makeGridMove(Grid, CurrentTurn, Move);
            CurrentTurn = switchPiece(CurrentTurn);
        }
        else if(CurrentTurn == Computer)
        {
            minimax(cloneGrid(Grid), CurrentTurn);
            Grid = makeGridMove(Grid, CurrentTurn, Choice);
            CurrentTurn = switchPiece(CurrentTurn);
            Console.WriteLine(Choice.ToString());
        }
    }

    int minimax(Piece[] InputGrid, Piece Player)
    {
        Piece[] Grid = cloneGrid(InputGrid);

        if (checkScore(Grid, Player) != 0)
            return checkScore(Grid, Player);
        else if (checkGameEnd(Grid)) return 0;

        List<int> scores = new List<int>();
        List<int> moves = new List<int>();

        for (int i = 0; i < 9; i++)
        {
            if (Grid[i] == Piece.Empty)
            {
                scores.Add(minimax(makeGridMove(Grid, Player, i), switchPiece(Player)));
                moves.Add(i);
            }
        }

        if(Player == Computer)
        {
            int MaxScoreIndex = scores.IndexOf(scores.Max());
            Choice = moves[MaxScoreIndex];
            return scores.Max();
        }
        else
        {
            int MinScoreIndex = scores.IndexOf(scores.Min());
            Choice = moves[MinScoreIndex];
            return scores.Min();
        }
    }

    static int checkScore(Piece[] Grid, Piece Player)
    {
        if (checkGameWin(Grid, Player)) return 10;

        else if (checkGameWin(Grid, switchPiece(Player))) return -10;

        else return 0;
    }

    static bool checkGameWin(Piece[] Grid, Piece Player)
    {
        for(int i = 0; i < 8; i++)
        {
            if
            (
                Grid[winConditions[i, 0]] == Player &&
                Grid[winConditions[i, 1]] == Player &&
                Grid[winConditions[i, 2]] == Player
            )
            {
                return true;
            }
        }
        return false;
    }

    static bool checkGameEnd(Piece[] Grid)
    {
        foreach (Piece p in Grid) if (p == Piece.Empty) return false;
        return true;
    }

    static Piece switchPiece(Piece Piece)
    {
        if (Piece == Piece.X) return Piece.O;
        else return Piece.X;
    }

    static Piece[] cloneGrid(Piece[] Grid)
    {
        Piece[] Clone = new Piece[9];
        for (int i = 0; i < 9; i++) Clone[i] = Grid[i];

        return Clone;
    }

    static Piece[] makeGridMove(Piece[] Grid, Piece Move, int Position)
    {
        Piece[] newGrid = cloneGrid(Grid);
        newGrid[Position] = Move;
        return newGrid;
    }
}

【问题讨论】:

  • @JuanM.Elosegui - 不,这根本不是骗子。只是同一个主题。
  • 不要误会我的意思,这是一个有趣的问题,但基本上我们必须在我们的脑海中播放调试器,或者复制并粘贴它并在机器上使用调试器 - 所以在我们这样做之前:你有吗使用调试器单步执行您的代码?如果是:任何提示问题出在哪里?
  • @talhaLodhi - 你有一个选择:学习使用调试器或学习编写单元测试。
  • @Carsten 不太熟悉使用调试器,但我尝试通过使用 console.writeline 添加调试代码来检查分数值,这太慢了,因为它是一个递归函数,但从那以后,我做到了请注意,大多数分数都是 -10,很少有零,没有 10,还有,你知道在 VS 中调试和单元测试有什么好的视频教程吗?

标签: c# algorithm recursion minimax


【解决方案1】:

这很愚蠢,但错误是我正在检查当前玩家的分数,但应该只检查人工智能。所以解决方案是使用:checkScore(Grid, Computer),而不是 checkScore(Grid, Player)

【讨论】:

    【解决方案2】:

    太好了,非常感谢。我必须稍微更改代码才能使算法正确地为我工作。所以我在这里发布我所做的。

        static currentPlayer;
    
        static float minimax(int[,] board, bool isMinimizing)
        {
    
            int[,] copy = board.Clone() as int[,];
    
            //if I won - > return 10
            if (isWinner(currentPlayer, copy))
                return 10f;
           // if opponent won - > return -10
            if (isWinner(OtherPlayer(currentPlayer), copy))
                return -10f;
            // if no moves left - > return 0
            if (isFull(copy))
                return 0;
    
            List<float> scores = new List<float>();
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (copy[i, j] == -1)
                    {
                        copy[i, j] = isMinimizing? currentPlayer : OtherPlayer(currentPlayer);
                        float score = minimax(copy, !isMinimizing);
                        copy[i, j] = -1;
                        scores.Add(score);
                    }
                }
            }
            if (isMinimizing)
                return scores.Max();
    
            return scores.Min();
    
        }
    

    【讨论】:

    • 次要性能点:在我看来,您不需要在此处复制板子-您仅在位置为-1时更改位置,并在递归调用后再次将其重置。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多