c# - Tic Tac Toe 的 Minimax 算法

标签 c# algorithm recursion minimax

我正在尝试通过 Jason Fox ( http://neverstopbuilding.com/minimax ) 实现 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;
    }
}

最佳答案

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

关于c# - Tic Tac Toe 的 Minimax 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32425162/

相关文章:

c# - 如何将记录器实例传递给ConfigureServices中的自定义ModelBinderProvider?

c# - IdentityServer 为每个 Client 选择 UserService

c# - DbgHelp.dll : calling SymGetModuleInfo64 from C#

algorithm - 尾递归快速排序的空间复杂度是多少?

algorithm - 如何从另一个列表中删除一个列表的每个实例?

python - 对 python 中良好的递归性能感到惊讶

javascript - 递归 Javascript camelCase 解释

c# - 结构 - System.Runtime.Interoptservice C++ 与 C#

algorithm - 分析包含 while 循环的算法

excel - VBA递归 "For loops"排列?