示例#1
0
        public void GenerateMovesTest_BishopMovesReturnedWithCapture_WhiteSide()
        {
            var expectedBishopMoves = new[]
            {
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.E5),
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.C5),
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.E3),
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.C3)
            }.ToHashSet();

            var board = new ChessRepresentation()
            {
                CurrentPlayer = ChessPlayer.White,
            };

            board[Positions.D4] = new Bishop(ChessPlayer.White, true);
            board[Positions.F6] = new Pawn(ChessPlayer.White, false);
            board[Positions.B6] = new Queen(ChessPlayer.White, false);
            board[Positions.B2] = new Pawn(ChessPlayer.White, false);
            board[Positions.E3] = new Pawn(ChessPlayer.Black, false);
            board[Positions.E8] = new King(ChessPlayer.Black, false);

            var game        = new ChessMechanism();
            var moves       = game.GenerateMoves(board).ToList();
            var bishopMoves = moves.OfType <ChessMove>()
                              .Where(x => x.From == Positions.D4)
                              .Where(x => x.Owner == ChessPlayer.White)
                              .ToHashSet();

            Assert.Subset(expectedBishopMoves, bishopMoves);
            Assert.Subset(bishopMoves, expectedBishopMoves);
        }
示例#2
0
        private void DoRobotWork(ChessPlayer player)
        {
            if (game.CurrentPlayer != player)
            {
                Thread.Sleep(1000);
                return;
            }

            ChangeAlgorithm();

            var playerName = player == ChessPlayer.White ? "[WHITE]" : "[BLACK]";

            var stopWatch = new Stopwatch();

            UpdateLog($"{playerName}Running algorithm...");

            StartStopAlgorithmProgressbar(player, true);
            stopWatch.Reset();
            stopWatch.Start();
            var move = player == ChessPlayer.White
                                    ? _algorithmLeft.Calculate(game)
                                    : _algorithmRight.Calculate(game);

            stopWatch.Stop();
            StartStopAlgorithmProgressbar(player, false);
            UpdateLog($"{playerName}Algorithm finished in {stopWatch.Elapsed.TotalSeconds:F} seconds and generated move: {move}", 2);
            game = _mechanism.ApplyMove(game, move);
            InvokeIfRequired(chessBoardVisualizerPanel1, () =>
            {
                chessBoardVisualizerPanel1.ChessRepresentation = game;
                chessBoardVisualizerPanel1.Refresh();
            });
            InvokeIfRequired(labelGameStatus, () => { labelGameStatus.Text = _mechanism.GetGameState(game).ToString(); });
        }
示例#3
0
        public void GenerateMovesTest_KingMovesInChess_WhiteSide()
        {
            var expectedKnightMoves = new BaseMove[]
            {
                new SpecialMove(ChessPlayer.White, MessageType.Resign),
                new ChessMove(ChessPlayer.White, Positions.C4, Positions.E2),
                new ChessMove(ChessPlayer.White, Positions.G2, Positions.G3),
                new ChessMove(ChessPlayer.White, Positions.G2, Positions.G1),
                new ChessMove(ChessPlayer.White, Positions.G2, Positions.H3),
                new ChessMove(ChessPlayer.White, Positions.G2, Positions.H1),
                new ChessMove(ChessPlayer.White, Positions.G2, Positions.F3),
                new ChessMove(ChessPlayer.White, Positions.G2, Positions.F1)
            }.ToHashSet();

            var board = new ChessRepresentation
            {
                CurrentPlayer  = ChessPlayer.White,
                [Positions.G2] = new King(ChessPlayer.White, true),
                [Positions.E2] = new Rook(ChessPlayer.Black, false),
                [Positions.C4] = new Queen(ChessPlayer.White, false),
                [Positions.G3] = new Pawn(ChessPlayer.Black, false)
            };

            var game  = new ChessMechanism();
            var moves = game.GenerateMoves(board).ToHashSet();

            Assert.Subset(expectedKnightMoves, moves);
            Assert.Subset(moves, expectedKnightMoves);
        }
示例#4
0
        public void GenerateMovesTest_KnightMovesReturnedWithCapture_WhiteSide()
        {
            var expectedKnightMoves = new[]
            {
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.C2),
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.E2),
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.B3),
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.F3),
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.F5),
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.C6),
                new ChessMove(ChessPlayer.White, Positions.D4, Positions.E6)
            }.ToHashSet();

            var board = new ChessRepresentation()
            {
                CurrentPlayer = ChessPlayer.White,
            };

            board[Positions.D4] = new Knight(ChessPlayer.White, true);
            board[Positions.B5] = new Pawn(ChessPlayer.White, false);
            board[Positions.B2] = new Pawn(ChessPlayer.White, false);
            board[Positions.E6] = new Pawn(ChessPlayer.Black, false);
            board[Positions.F5] = new Pawn(ChessPlayer.Black, false);

            var game        = new ChessMechanism();
            var moves       = game.GenerateMoves(board).ToList();
            var knightMoves = moves.OfType <ChessMove>()
                              .Where(x => x.From == Positions.D4)
                              .Where(x => x.Owner == ChessPlayer.White)
                              .ToHashSet();

            Assert.Subset(expectedKnightMoves, knightMoves);
            Assert.Subset(knightMoves, expectedKnightMoves);
        }
示例#5
0
        public void ChessBoardTest_NewlyCreated_BoardIsEmpty()
        {
            // Arrange
            var board = new ChessRepresentation();

            // Act

            // Assert
            Assert.All(Helpers.PositionList, x => { Assert.Null(board[x]); });
        }
示例#6
0
        public void ChessBoardTest_AddingItem_BoardIsEmptyExceptInGivenPosition()
        {
            // Arrange
            var positionOfChessPiece = Positions.H3;
            var board = new ChessRepresentation();
            var expectedChessPiece = new Bishop(ChessPlayer.Black);
            var emptyPositions     = Helpers.PositionList.Where(x => x != positionOfChessPiece);

            // Act
            board[positionOfChessPiece] = expectedChessPiece;

            // Assert
            Assert.All(emptyPositions, x => { Assert.Null(board[x]); });
            Assert.Equal(expectedChessPiece, board[positionOfChessPiece]);
        }
示例#7
0
        public void GameStateCheck_KingVsKing_Draw()
        {
            var expected = GameState.Draw;

            // Draw
            var game = new ChessRepresentation()
            {
                History = new List <BaseMove>(),
                ["C5"]  = new King(ChessPlayer.White, true),
                ["F4"]  = new King(ChessPlayer.Black, true)
            };
            var mechanism = new ChessMechanism();

            var actual = mechanism.GetGameState(game);

            Assert.Equal(expected, actual);
        }
示例#8
0
        public void GameStateCheck_KingBishopVsKingBishopDifferentColour_InProgress()
        {
            var expected = GameState.InProgress;

            // Draw
            var game = new ChessRepresentation()
            {
                History = new List <BaseMove>(),
                ["C5"]  = new King(ChessPlayer.White, true),
                ["F4"]  = new King(ChessPlayer.Black, true),
                ["C8"]  = new Bishop(ChessPlayer.White, true),
                ["G1"]  = new Bishop(ChessPlayer.Black, true),
            };
            var mechanism = new ChessMechanism();

            var actual = mechanism.GetGameState(game);

            Assert.Equal(expected, actual);
        }
示例#9
0
        public void GenerateMovesTest_CastlingsReturned_BlackSide()
        {
            var expected = 2;

            var board = new ChessRepresentation()
            {
                CurrentPlayer = ChessPlayer.Black,
            };

            board[Positions.E8] = new King(ChessPlayer.Black, false);
            board[Positions.A8] = new Rook(ChessPlayer.Black, false);
            board[Positions.H8] = new Rook(ChessPlayer.Black, false);
            board[Positions.E1] = new King(ChessPlayer.White, false);

            var game   = new ChessMechanism();
            var moves  = game.GenerateMoves(board).OfType <KingCastlingMove>();
            var result = moves.Count();

            Assert.Equal(expected, result);
        }
示例#10
0
        public MainForm()
        {
            game = new ChessRepresentationInitializer().Create();

            _mechanism = new ChessMechanism(true);
            _evaluator = new Evaluator(_mechanism);
            _generator = new MoveGenerator(_mechanism);
            _applier   = new MoveApplier(_mechanism);

            taskRight = new Task(() =>
            {
                do
                {
                    if (!_isActive || _mechanism.GetGameState(game) != GameState.InProgress)
                    {
                        InvokeIfRequired(progressbarBotActiveRight, () =>
                        {
                            progressbarBotActiveRight.MarqueeAnimationSpeed = 0;
                        });

                        // Not doing anything, checking in every second...
                        Thread.Sleep(1000);
                        continue;
                    }

                    InvokeIfRequired(progressbarBotActiveRight, () =>
                    {
                        if (progressbarBotActiveRight.MarqueeAnimationSpeed != 10)
                        {
                            progressbarBotActiveRight.MarqueeAnimationSpeed = 10;
                        }
                    });

                    DoRobotWork(ChessPlayer.White);

                    Thread.Sleep(1000);
                } while (true);
            });

            taskLeft = new Task(() =>
            {
                do
                {
                    if (!_isActive || _mechanism.GetGameState(game) != GameState.InProgress)
                    {
                        InvokeIfRequired(progressbarBotActiveLeft, () =>
                        {
                            progressbarBotActiveLeft.MarqueeAnimationSpeed = 0;
                        });

                        // Not doing anything, checking in every second...
                        Thread.Sleep(1000);
                        continue;
                    }

                    InvokeIfRequired(progressbarBotActiveLeft, () =>
                    {
                        if (progressbarBotActiveLeft.MarqueeAnimationSpeed != 10)
                        {
                            progressbarBotActiveLeft.MarqueeAnimationSpeed = 10;
                        }
                    });

                    DoRobotWork(ChessPlayer.Black);

                    Thread.Sleep(1000);
                } while (true);
            });

            InitializeComponent();

            listboxAlgorithmsRight.Items.Add(Algorithms.Minimax);
            listboxAlgorithmsRight.Items.Add(Algorithms.MinimaxAverage);
            listboxAlgorithmsRight.Items.Add(Algorithms.AlphaBeta);
            listboxAlgorithmsRight.Items.Add(Algorithms.Greedy);
            listboxAlgorithmsRight.Items.Add(Algorithms.Random);
            listboxAlgorithmsRight.SelectedIndex = Randomizer.Next(0, listboxAlgorithmsRight.Items.Count - 1);

            listboxAlgorithmsLeft.Items.Add(Algorithms.Minimax);
            listboxAlgorithmsLeft.Items.Add(Algorithms.MinimaxAverage);
            listboxAlgorithmsLeft.Items.Add(Algorithms.AlphaBeta);
            listboxAlgorithmsLeft.Items.Add(Algorithms.Greedy);
            listboxAlgorithmsLeft.Items.Add(Algorithms.Random);
            listboxAlgorithmsLeft.SelectedIndex = Randomizer.Next(0, listboxAlgorithmsLeft.Items.Count - 1);

            chessBoardVisualizerPanel1.ChessRepresentation = game;
            chessBoardVisualizerPanel1.Refresh();

            taskLeft.Start();
            taskRight.Start();
        }
示例#11
0
 private void btnRestart_Click(object sender, EventArgs e)
 {
     _isActive = false;
     game      = new ChessRepresentationInitializer().Create();
     chessBoardVisualizerPanel1.Refresh();
 }