Пример #1
0
        public static GameNode GetBestMoves(Chess.Chess.Game _game, int stepsForward, DateTime finishTime, double alpha = -1000, double beta = 1000)
        {
            if (DateTime.Now >= finishTime)
            {
                throw new TimeoutException("Time limit exceeded.");
            }
            var gT = new GameNode(_game);

            gT.score = gT.game.WhiteNext ? -1000 : 1000;
            if (stepsForward <= 0)
            {
                gT.score = gT.game.evaluate();
                return(gT);
            }
            else
            {
                if (gT.game.WhiteNext)
                {
                    foreach (var move in _game.GetNextMoves())
                    {
                        var goodMoves = GetBestMoves(move, stepsForward - 1, finishTime, alpha, beta);
                        if (goodMoves.score > gT.score)
                        {
                            gT.score    = goodMoves.score;
                            gT.BestNode = goodMoves;
                        }
                        alpha = Math.Max(alpha, gT.score);
                        if (alpha >= beta)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    foreach (var move in _game.GetNextMoves())
                    {
                        var goodMoves = GetBestMoves(move, stepsForward - 1, finishTime, alpha, beta);
                        if (goodMoves.score < gT.score)
                        {
                            gT.score    = goodMoves.score;
                            gT.BestNode = goodMoves;
                        }
                        beta = Math.Min(beta, gT.score);
                        if (alpha >= beta)
                        {
                            break;
                        }
                    }
                }
            }
            //Console.WriteLine(" " + stepsForward);
            //Console.Write(_game.ToString());
            //Console.WriteLine("Score: " + gT.score);
            //Console.WriteLine("--------------------------------");
            return(gT);
        }
Пример #2
0
        public static GameNode GetBestMoves(Chess.Chess.Game _game, int stepsForward, int alpha = -1000, int beta = 1000)
        {
            var gT = new GameNode(_game);

            gT.score = gT.game.WhiteNext ? -1000 : 1000;
            if (stepsForward <= 0)
            {
                gT.score = gT.game.evaluate();
                return(gT);
            }
            else
            {
                if (gT.game.WhiteNext)
                {
                    foreach (var move in _game.GetNextMoves())
                    {
                        var goodMoves = GetBestMoves(move, stepsForward - 1, alpha, beta);
                        if (goodMoves.score > gT.score)
                        {
                            gT.score    = goodMoves.score;
                            gT.BestNode = goodMoves;
                        }
                        alpha = Math.Max(alpha, gT.score);
                        if (alpha >= beta)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    foreach (var move in _game.GetNextMoves())
                    {
                        var goodMoves = GetBestMoves(move, stepsForward - 1, alpha, beta);
                        if (goodMoves.score < gT.score)
                        {
                            gT.score    = goodMoves.score;
                            gT.BestNode = goodMoves;
                        }
                        beta = Math.Min(beta, gT.score);
                        if (alpha >= beta)
                        {
                            break;
                        }
                    }
                }
            }
            //Console.WriteLine(" " + stepsForward);
            //Console.Write(_game.ToString());
            //Console.WriteLine("Score: " + gT.score);
            //Console.WriteLine("--------------------------------");
            return(gT);
        }