井字游戏中的Minimax算法 [英] The Minimax algorithm in Tic Tac Toe

查看:68
本文介绍了井字游戏中的Minimax算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿朋友,

我试图在我的Tic Tac Toe程序中实现Minimax算法.

不幸的是,我在德国Wikipedia网站上使用了有关该定律的示例,但它不起作用:(

最终您可以给我小费,因为我的大脑没主意了.这是我的代码中的方法...等等实际上是不言自明的:

私有TicTacToeMove SavedMove = null;

        公共列表< TicTacToeMove> GetPossibleMoves(TicTacToe电路板,TicTacToePlayer电流)
        {
            列表< TicTacToeMove> moves = new List< TicTacToeMove>();

            对于(int x = 0; x< board.Width; x ++)
            {
                为(int y = 0; y< board.Height; y ++)
                {
                    如果(board.Get(x,y)== null)
                    {
                        move.Add(new TicTacToeMove {X = x,Y = y,Sign = current.Sign});
                    }
                }
            }

            退回动作;
        }

        私有TicTacToePlayer GetOpponent(当前TicTacToePlayer)
        {
            如果(当前== _engine.PlayerOne)
            {
                return _engine.PlayerTwo;
            }

            返回_engine.PlayerOne;
        }

        private int评估(TicTacToeBoard板,TicTacToePlayer当前,TicTacToePlayer对手)
        {
            如果(board.HasWinner()== current.Sign)
            {
                返回+15;
            }

            如果(board.HasWinner()==对手.签名)
            {
                返回-20;
            }
            
            返回0;
        }

        

        public int Max(TicTacToeBoard板,TicTacToePlayer当前电流,int深度)
        {
            TicTacToeBoard clonedBoard = board.Clone();
            TicTacToePlayer对手= GetOpponent(当前);

            如果(深度== 0 || clonedBoard.IsFull()||!string.IsNullOrEmpty(clonedBoard.HasWinner()))
            {
                返回Evaluate(clonedBoard,当前,对手);
            }


            int maxWert = 5;
            列表< TicTacToeMove> moves = GetPossibleMoves(board,current);
            一会儿(moves.Count> 0)
            {
                TicTacToeMove move = moves [0];
                clonedBoard.Set(move.X,move.Y,move.Sign);
                int wert = Min(clonedBoard,对手,深度-1);
                moves.RemoveAt(0);

                如果(wert> maxWert)
                {
                    maxWert =湿润;
                    SavedMove =移动;

                    如果(maxWert == 15)
                        move.Clear();
                }
            }

            返回maxWert;
        }

        

        公共int最小值(TicTacToeBoard板,TicTacToePlayer电流,int深度)
        {
            TicTacToeBoard clonedBoard = board.Clone();
            TicTacToePlayer对手= GetOpponent(当前);

            如果(深度== 0 || clonedBoard.IsFull()||!string.IsNullOrEmpty(clonedBoard.HasWinner()))
            {
                返回Evaluate(clonedBoard,当前,对手);
            }


            int minWert = int.MaxValue;

            列表< TicTacToeMove> moves = GetPossibleMoves(clonedBoard,当前);
            一会儿(moves.Count> 0)
            {
                TicTacToeMove move = moves [0];
                clonedBoard.Set(move.X,move.Y,move.Sign);
                int wert = Max(clonedBoard,对手,深度-1);
                moves.RemoveAt(0);

                如果(wert< minWert)
                {
                    minWert =湿润;
                    //SavedMove = move;
                }
            }

            返回minWert;
        }

        公用TicTacToeMove CalculateMoveHard(TicTacToe电路板,TicTacToePlayer当前)
        {
            int分数=最大值(板,当前,5);

            如果(SavedMove == null)
                返回SavedMove; // (画?!)
            别的
                返回SavedMove;
        } 

如果有人对我有一个主意< 3

解决方案

/SL设计器,Visual Studio指导自动化工具包,开发人员文档和帮助系统以及Visual Studio编辑器. >

此致

private TicTacToeMove SavedMove = null; public List<TicTacToeMove> GetPossibleMoves(TicTacToeBoard board, TicTacToePlayer current) { List<TicTacToeMove> moves = new List<TicTacToeMove>(); for (int x = 0; x < board.Width; x++) { for (int y = 0; y < board.Height; y++) { if (board.Get(x, y) == null) { moves.Add(new TicTacToeMove { X = x, Y = y, Sign = current.Sign }); } } } return moves; } private TicTacToePlayer GetOpponent(TicTacToePlayer current) { if (current == _engine.PlayerOne) { return _engine.PlayerTwo; } return _engine.PlayerOne; } private int Evaluate(TicTacToeBoard board, TicTacToePlayer current, TicTacToePlayer opponent) { if (board.HasWinner() == current.Sign) { return +15; } if (board.HasWinner() == opponent.Sign) { return -20; } return 0; } public int Max(TicTacToeBoard board, TicTacToePlayer current, int depth) { TicTacToeBoard clonedBoard = board.Clone(); TicTacToePlayer opponent = GetOpponent(current); if (depth == 0 || clonedBoard.IsFull() || !string.IsNullOrEmpty(clonedBoard.HasWinner())) { return Evaluate(clonedBoard, current, opponent); } int maxWert = 5; List<TicTacToeMove> moves = GetPossibleMoves(board, current); while (moves.Count > 0) { TicTacToeMove move = moves[0]; clonedBoard.Set(move.X, move.Y, move.Sign); int wert = Min(clonedBoard, opponent, depth - 1); moves.RemoveAt(0); if (wert > maxWert) { maxWert = wert; SavedMove = move; if (maxWert == 15) moves.Clear(); } } return maxWert; } public int Min(TicTacToeBoard board, TicTacToePlayer current, int depth) { TicTacToeBoard clonedBoard = board.Clone(); TicTacToePlayer opponent = GetOpponent(current); if (depth == 0 || clonedBoard.IsFull() || !string.IsNullOrEmpty(clonedBoard.HasWinner())) { return Evaluate(clonedBoard, current, opponent); } int minWert = int.MaxValue; List<TicTacToeMove> moves = GetPossibleMoves(clonedBoard, current); while (moves.Count > 0) { TicTacToeMove move = moves[0]; clonedBoard.Set(move.X, move.Y, move.Sign); int wert = Max(clonedBoard, opponent, depth - 1); moves.RemoveAt(0); if (wert < minWert) { minWert = wert; //SavedMove = move; } } return minWert; } public TicTacToeMove CalculateMoveHard(TicTacToeBoard board, TicTacToePlayer current) { int score = Max(board, current, 5); if (SavedMove == null) return SavedMove; // (Draw?!) else return SavedMove; }

It would be wonderful if anyone have an idea for me <3

解决方案

Hi LucasProgrammer,

This forum is discussing Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor.

Your issue is related to C# develop, I will move this thread to corresponding forum for a professional answer.

Sincerely,

Oscar


这篇关于井字游戏中的Minimax算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆