示例#1
0
        public void GameTreeTest()
        {
            var gameTree = new GameTree();

            var board = Board.Factory.GetDefault();

            var moves  = gameTree.AlphaBetaRecursive(board, 6, true);
            var board2 = board.MakeMove(moves.First());

            var moves2 = gameTree.AlphaBetaRecursive(board2, 6, false);
            var board3 = board.MakeMove(moves2.First());

            var moves3 = gameTree.AlphaBetaRecursive(board3, 6, true);
            var board4 = board.MakeMove(moves3.First());
        }
示例#2
0
        public void PerformanceTests()
        {
            var gameTree = new GameTree();

            var board = Board.Factory.GetDefault();

            var sw    = Stopwatch.StartNew();
            var moves = gameTree.AlphaBetaRecursive(board, 6, true);
            var t1    = sw.ElapsedMilliseconds;
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Chess Console!");

            //var player = EnumPlayer.Undefined;

            //ConsoleKeyInfo keyInfo = new ConsoleKeyInfo();
            //do
            //{
            //    Console.Write("Please choose player (w/b):");
            //    keyInfo = Console.ReadKey();
            //    Console.Write(Environment.NewLine);
            //    if (keyInfo.Key == ConsoleKey.W) player = EnumPlayer.White;
            //    if (keyInfo.Key == ConsoleKey.B) player = EnumPlayer.Black;
            //} while (player == EnumPlayer.Undefined);

            var  inProgress  = true;
            bool isWhiteTurn = true;
            var  board       = Board.Factory.GetDefault();
            var  gameTree    = new GameTree();

            var history = new List <Board>()
            {
                board
            };

            while (inProgress)
            {
                if (isWhiteTurn)
                {
                    Console.Write("Make a move:");
                    var input = Console.ReadLine();
                    if (input.Length > 4 && input.Substring(0, 5) == "query")
                    {
                        if (Position.TryParse(input.Substring(6, input.Length - 6), out Position p))
                        {
                            Console.WriteLine($"{board.GetBoardSquare(p.rank, p.file)}");
                        }
                    }
                    else if (Chess.Old.Move.TryParse(input, out var move) &&
                             board.GetMovesForPiece(move.fromRank, move.fromFile).Any(x => x.ToIndex == board.GetBoardIndex(move.toRank, move.toFile)))
                    {
                        var newMove = board.GetMovesForPiece(move.fromRank, move.fromFile).First(x => x.ToIndex == board.GetBoardIndex(move.toRank, move.toFile));

                        board = board.MakeMove(newMove);

                        history.Add(board);

                        isWhiteTurn = false;
                    }
                    else
                    {
                        Console.Write("Invalid move! Please try again:");
                    }
                }
                else
                {
                    Console.Write("Computer to move:");

                    var moves = gameTree.AlphaBetaRecursive(board, 6, isWhiteTurn);
                    var move  = moves.First();
                    board = board.MakeMove(move);

                    history.Add(board);

                    string moveStr = $"{Interpreter.FileName(board.File(move.FromIndex))}" +
                                     $"{Interpreter.RankName(board.Rank(move.FromIndex))}" +
                                     $"{Interpreter.FileName(board.File(move.ToIndex))}" +
                                     $"{Interpreter.RankName(board.Rank(move.ToIndex))}";

                    Console.Write($"{moveStr}{Environment.NewLine}");

                    isWhiteTurn = true;
                }
            }

            Console.Write("Press any key to exist.");
            Console.ReadKey();
        }
示例#4
0
        static void Main(string[] args)
        {
            int maxDepth = 10;
            var rnd      = new Random();

            var gameTree = new GameTree();
            var board    = Board.Factory.GetDefault();

            for (int d = 4; d <= maxDepth; d++)
            {
                var sw    = Stopwatch.StartNew();
                var moves = gameTree.AlphaBetaRecursive(board, d, true);
                var t1    = sw.ElapsedMilliseconds;

                double dummy = 1;
                sw = Stopwatch.StartNew();
                for (int i = 0; i < gameTree._numberOfLeafEvaluations; i++)
                {
                    dummy *= rnd.NextDouble();
                }
                var t2 = sw.ElapsedMilliseconds;

                Console.WriteLine($"{board}: " +
                                  $"depth={d}, " +
                                  $"time={t1}ms, " +
                                  $"numerOfEvaluations={gameTree._numberOfLeafEvaluations}, " +
                                  $"numberOfBranchesPruned={gameTree._numberOfBranchesPruned}, " +
                                  $"moves[0]={moves[0].ToString()}, minTime={t2}, {(double)t1 / t2}");
            }

            //var gameTree2 = new Chess.V2.GameTree();
            //var board2 = Chess.V2.Board.Factory.GetDefault();

            //for (int d = 4; d <= maxDepth; d++)
            //{
            //    var sw = Stopwatch.StartNew();
            //    var moves = gameTree2.AlphaBetaRecursive(board2, d, true);
            //    var t1 = sw.ElapsedMilliseconds;

            //    var trueValue = moves.First().CalculateScoresThroughSequence(board2);

            //    double dummy = 1;
            //    sw = Stopwatch.StartNew();
            //    for (int i=0; i < gameTree2._numberOfLeafEvaluations; i++)
            //    {
            //        dummy *= rnd.NextDouble();
            //    }
            //    var t2 = sw.ElapsedMilliseconds;

            //    Console.WriteLine($"{board2}: " +
            //        $"depth={d}, " +
            //        $"time={t1}ms, " +
            //        $"numerOfEvaluations={gameTree2._numberOfLeafEvaluations}, " +
            //        $"numberOfBranchesPruned={gameTree2._numberOfBranchesPruned}, " +
            //        $"moves[0]={moves[0].Move.ToString()}, minTime={t2}, {(double)t1/t2}");
            //}


            var gameTree3 = new Chess.V3.GameTree();
            var board3    = Chess.V3.Board.Factory.GetDefault();

            for (int d = 4; d <= maxDepth; d++)
            {
                var             sw    = Stopwatch.StartNew();
                V3.MoveSequence alpha = new V3.MoveSequence {
                    Value = float.NegativeInfinity
                };
                V3.MoveSequence beta = new V3.MoveSequence {
                    Value = float.PositiveInfinity
                };
                var moves = gameTree3.GetMaximizingSequence(null, new V3.Move(), board3, d, alpha, beta);
                var t1    = sw.ElapsedMilliseconds;

                var trueValue = moves.CalculateScoresThroughSequence(board3);

                double dummy = 1;
                sw = Stopwatch.StartNew();
                for (int i = 0; i < gameTree3._numberOfLeafEvaluations; i++)
                {
                    dummy *= rnd.NextDouble();
                }
                var t2 = sw.ElapsedMilliseconds;

                Console.WriteLine($"{board3}: " +
                                  $"depth={d}, " +
                                  $"time={t1}ms, " +
                                  $"numerOfEvaluations={gameTree3._numberOfLeafEvaluations}, " +
                                  $"numberOfBranchesPruned={gameTree3._numberOfBranchesPruned}, " +
                                  $"moves[0]={moves.Move.ToString()}, minTime={t2}, {(double)t1 / t2}");
            }



            Console.ReadKey();
        }