コード例 #1
0
        public void ChessBoardTest_SameOutcomeDifferentHistory_BoardsAreNonEqual()
        {
            var mechanism = new ChessMechanism();
            var game1     = new ChessRepresentationInitializer().Create();
            var game2     = new ChessRepresentationInitializer().Create();

            var move1 = new ChessMove(ChessPlayer.White, Positions.B1, Positions.C3);
            var move2 = new ChessMove(ChessPlayer.Black, Positions.B8, Positions.C6);

            game1 = mechanism.ApplyMove(game1, move1);
            game1 = mechanism.ApplyMove(game1, move2);
            game2 = mechanism.ApplyMove(game2, move1);
            game2 = mechanism.ApplyMove(game2, move2);

            var move3 = new ChessMove(ChessPlayer.White, Positions.C3, Positions.E4);
            var move4 = new ChessMove(ChessPlayer.Black, Positions.C6, Positions.E5);
            var move5 = new ChessMove(ChessPlayer.White, Positions.E4, Positions.C3);
            var move6 = new ChessMove(ChessPlayer.Black, Positions.E5, Positions.C6);

            game1 = mechanism.ApplyMove(game1, move3);
            game1 = mechanism.ApplyMove(game1, move4);
            game1 = mechanism.ApplyMove(game1, move5);
            game1 = mechanism.ApplyMove(game1, move6);

            Assert.NotEqual(game1, game2);
        }
コード例 #2
0
        /// <inheritdoc />
        public ChessGameDetails ConvertToChessGameDetails(DbChessGame source)
        {
            if (source == null)
            {
                return(null);
            }

            var history = source.History?.OrderBy(x => x.CreatedAt).Select(CovertToChessMove).ToList() ?? new List <BaseMove>();

            var representation = new ChessRepresentationInitializer().Create();
            var chessMechanism = new ChessMechanism();

            foreach (var move in history)
            {
                representation = chessMechanism.ApplyMove(representation, move);
            }

            return(new ChessGameDetails
            {
                ChallengeDate = source.ChallengeDate,
                Id = source.Id,
                InitiatedBy = ConvertUser(source.InitiatedBy),
                LastMoveDate = source.LastMoveDate,
                Name = source.Name,
                Opponent = ConvertUser(source.Opponent),
                BlackPlayer = ConvertUser(source.BlackPlayer),
                WhitePlayer = ConvertUser(source.WhitePlayer),
                Representation = representation
            });
        }
コード例 #3
0
        public void ValidationTest_InvalidMove_IllegalExceptionThrown(BaseMove move)
        {
            var game      = new ChessRepresentationInitializer().Create();
            var mechanism = new ChessMechanism();

            Assert.Throws <ChessIllegalMoveException>(() => { mechanism.ApplyMove(game, move); });
        }
コード例 #4
0
        public void GameStateCheck_CheckMateTest_WhiteWins()
        {
            var expected = GameState.WhiteWon;

            //Scholar's Mate
            var game      = new ChessRepresentationInitializer().Create();
            var mechanism = new ChessMechanism();
            var moves     = new List <BaseMove>(7)
            {
                new ChessMove(ChessPlayer.White, Positions.E2, Positions.E4),
                new ChessMove(ChessPlayer.Black, Positions.E7, Positions.E5),
                new ChessMove(ChessPlayer.White, Positions.F1, Positions.C4),
                new ChessMove(ChessPlayer.Black, Positions.B8, Positions.C6),
                new ChessMove(ChessPlayer.White, Positions.D1, Positions.H5),
                new ChessMove(ChessPlayer.Black, Positions.G8, Positions.F6),
                new ChessMove(ChessPlayer.White, Positions.H5, Positions.F7)
            };

            foreach (var move in moves)
            {
                game = mechanism.ApplyMove(game, move);
            }

            var actual = mechanism.GetGameState(game);

            Assert.Equal(expected, actual);
        }
コード例 #5
0
ファイル: MainForm.cs プロジェクト: poseen/ChessBotArena
        private void RefreshGame()
        {
            if (_client.IsAnonymous || _client.CurrentGame?.Representation == null)
            {
                listboxMoves.Items.Clear();
                chessBoardGamePanel1.ChessRepresentation = new ChessRepresentation();
                chessBoardGamePanel1.Enabled             = false;
                chessBoardGamePanel1.Refresh();
                return;
            }

            var details = _client.CurrentGame;

            listboxMoves.Items.Clear();

            var game = new ChessRepresentationInitializer().Create();

            foreach (var t in details.Representation.History)
            {
                game = _mechanism.ApplyMove(game, t);
                listboxMoves.Items.Add(t.ToString());
            }

            chessBoardGamePanel1.ChessRepresentation = game;

            labelGameState.Text = GetStatusLabelText();

            var isItMyTurn = _client.IsItMyTurn ?? false;

            if (!isItMyTurn)
            {
                btnAcceptDraw.Enabled        = false;
                btnDeclineDraw.Enabled       = false;
                btnOfferDraw.Enabled         = false;
                btnResign.Enabled            = false;
                chessBoardGamePanel1.Enabled = false;
            }
            else
            {
                var myColor = _client.CurrentGame.Representation.CurrentPlayer;
                var possibleSpecialMoves = _mechanism.GenerateMoves(game).Where(x => x.Owner == myColor).OfType <SpecialMove>().ToList();

                btnAcceptDraw.Enabled        = possibleSpecialMoves.Any(x => x.Message == MessageType.DrawAccept);
                btnDeclineDraw.Enabled       = possibleSpecialMoves.Any(x => x.Message == MessageType.DrawDecline);
                btnOfferDraw.Enabled         = possibleSpecialMoves.Any(x => x.Message == MessageType.DrawOffer);
                btnResign.Enabled            = possibleSpecialMoves.Any(x => x.Message == MessageType.Resign);
                chessBoardGamePanel1.Enabled = true;
            }

            // Scroll into view the last item...
            if (listboxMoves.Items.Count > 0)
            {
                listboxMoves.SelectedIndex = listboxMoves.Items.Count - 1;
            }
            chessBoardGamePanel1.Refresh();
        }
コード例 #6
0
        public void ChessRepresentation_GetHashCodeTest_AreEqual()
        {
            var representation1 = new ChessRepresentationInitializer().Create();
            var representation2 = new ChessRepresentationInitializer().Create();

            var hash1 = representation1.GetHashCode();
            var hash2 = representation2.GetHashCode();

            Assert.Equal(hash1, hash2);
        }
コード例 #7
0
 protected BaseChessBoardVisualizerPanel()
 {
     _chessRepresentation = new ChessRepresentationInitializer().Create();
     BlackSquare          = Color.SandyBrown;
     WhiteSquare          = Color.BlanchedAlmond;
     Bevel             = Color.Brown;
     InterpolationMode = InterpolationMode.NearestNeighbor;
     MouseMove        += OnMouseMove;
     MouseClick       += OnMouseClick;
 }
コード例 #8
0
        private ChessRepresentation CalculateBoard(IReadOnlyList <BaseMove> history, int index)
        {
            var newBoard  = new ChessRepresentationInitializer().Create();
            var mechanism = new ChessMechanism();

            for (var i = 0; i < index; i++)
            {
                newBoard = mechanism.ApplyMove(newBoard, history[i]);
            }

            return(newBoard);
        }
コード例 #9
0
        public void GenerateMovesTest_Initial_AppropriateMovesReturned()
        {
            var expected = 21;

            var representation = new ChessRepresentationInitializer().Create();

            var game = new ChessMechanism();

            var moves  = game.GenerateMoves(representation);
            var result = moves.Count();

            Assert.Equal(expected, result);
        }
コード例 #10
0
        public void ChessBoardTest_EqualityCheck_BoardsAreEqual()
        {
            var generatorMechanism = new ChessMechanism();
            var applierMechanism1  = new ChessMechanism();
            var applierMechanism2  = new ChessMechanism();

            var numberOfTries = 10;

            var result = true;

            for (var i = 0; i < numberOfTries; i++)
            {
                var game1         = new ChessRepresentationInitializer().Create();
                var game2         = new ChessRepresentationInitializer().Create();
                var referenceGame = new ChessRepresentationInitializer().Create();

                var count = 0;
                while (true)
                {
                    count++;
                    var move = generatorMechanism.GenerateMoves(referenceGame)
                               .OfType <BaseChessMove>()
                               .OrderBy(x => Guid.NewGuid())
                               .FirstOrDefault();

                    if (move == null || count > 20)
                    {
                        break;
                    }

                    game1         = applierMechanism1.ApplyMove(game1, move);
                    game2         = applierMechanism2.ApplyMove(game2, move);
                    referenceGame = generatorMechanism.ApplyMove(referenceGame, move);
                }

                result = result && game1.Equals(game1);
                result = result && game2.Equals(game2);
                result = result && referenceGame.Equals(referenceGame);

                result = result && game1.Equals(game2);
                result = result && game2.Equals(referenceGame);
                result = result && referenceGame.Equals(game1);

                result = result && game1.Equals(referenceGame);
                result = result && referenceGame.Equals(game2);
                result = result && game2.Equals(game1);
            }

            Assert.True(result);
        }
コード例 #11
0
        private async void listboxMatches_SelectedIndexChanged(object sender, EventArgs e)
        {
            var match     = (DecoratedMatch)listboxMatches.SelectedItem;
            var mechanism = new ChessMechanism();
            var board     = new ChessRepresentationInitializer().Create();

            if (match == null)
            {
                return;
            }

            labelStatus.Text = $"Getting details of selected match... ({match.Name})";
            var details = await _client.GetMatchAsync(_jwtToken, match.Id.ToString());

            listboxGameHistory.Items.Clear();

            if (details?.Representation?.History == null)
            {
                MessageBox.Show("Couldn't get details of selected match. (Maybe unauthorized?)");
                labelStatus.Text = "Error while getting details of match";
                return;
            }

            listboxGameHistory.Items.Add(new ChessRepresentationStage()
            {
                AfterMove           = null,
                ChessRepresentation = board
            });

            foreach (var move in details.Representation.History)
            {
                board = mechanism.ApplyMove(board, move);

                var item = new ChessRepresentationStage
                {
                    AfterMove           = move,
                    ChessRepresentation = board
                };

                listboxGameHistory.Items.Add(item);
            }
        }
コード例 #12
0
        public void GameStateCheck_StaleMateTest_Draw()
        {
            var expected = GameState.Draw;

            //Stalemate
            var game      = new ChessRepresentationInitializer().Create();
            var mechanism = new ChessMechanism();
            var moves     = new List <BaseMove>(19)
            {
                new ChessMove(ChessPlayer.White, Positions.E2, Positions.E3),
                new ChessMove(ChessPlayer.Black, Positions.A7, Positions.A5),
                new ChessMove(ChessPlayer.White, Positions.D1, Positions.H5),
                new ChessMove(ChessPlayer.Black, Positions.A8, Positions.A6),
                new ChessMove(ChessPlayer.White, Positions.H5, Positions.A5),
                new ChessMove(ChessPlayer.Black, Positions.H7, Positions.H5),
                new ChessMove(ChessPlayer.White, Positions.H2, Positions.H4),
                new ChessMove(ChessPlayer.Black, Positions.A6, Positions.H6),
                new ChessMove(ChessPlayer.White, Positions.A5, Positions.C7),
                new ChessMove(ChessPlayer.Black, Positions.F7, Positions.F6),
                new ChessMove(ChessPlayer.White, Positions.C7, Positions.D7),
                new ChessMove(ChessPlayer.Black, Positions.E8, Positions.F7),
                new ChessMove(ChessPlayer.White, Positions.D7, Positions.B7),
                new ChessMove(ChessPlayer.Black, Positions.D8, Positions.D3),
                new ChessMove(ChessPlayer.White, Positions.B7, Positions.B8),
                new ChessMove(ChessPlayer.Black, Positions.D3, Positions.H7),
                new ChessMove(ChessPlayer.White, Positions.B8, Positions.C8),
                new ChessMove(ChessPlayer.Black, Positions.F7, Positions.G6),
                new ChessMove(ChessPlayer.White, Positions.C8, Positions.E6)
            };

            foreach (var move in moves)
            {
                game = mechanism.ApplyMove(game, move);
            }

            var actual = mechanism.GetGameState(game);

            Assert.Equal(expected, actual);
        }
コード例 #13
0
ファイル: TestConsole.cs プロジェクト: poseen/ChessBotArena
        static void Main(string[] args)
        {
            var board = new ChessRepresentationInitializer().Create();

            board.CurrentPlayer = ChessPlayer.White;

            var manager = new ChessMechanism();

            var step1 = new ChessMove(ChessPlayer.White, (Position)"B2", (Position)"B4");
            var step2 = new ChessMove(ChessPlayer.Black, (Position)"E7", (Position)"E5");
            var step3 = new ChessMove(ChessPlayer.White, (Position)"B4", (Position)"B5");
            var step4 = new ChessMove(ChessPlayer.Black, (Position)"B8", (Position)"C6");
            var step5 = new ChessMove(ChessPlayer.White, (Position)"B5", (Position)"C6");

            board = manager.ApplyMove(board, step1);
            board = manager.ApplyMove(board, step2);
            board = manager.ApplyMove(board, step3);
            board = manager.ApplyMove(board, step4);
            board = manager.ApplyMove(board, step5);

            Tools.ChessboardVisualizer.ChessboardVisualizer.TestShowVisualizer(board);
        }
コード例 #14
0
        public void GameStateCheck_CheckMateTest_BlackWins()
        {
            var expected = GameState.BlackWon;

            // Fool's mate
            var game      = new ChessRepresentationInitializer().Create();
            var mechanism = new ChessMechanism();
            var moves     = new List <BaseMove>(4)
            {
                new ChessMove(ChessPlayer.White, Positions.G2, Positions.G4),
                new ChessMove(ChessPlayer.Black, Positions.E7, Positions.E5),
                new ChessMove(ChessPlayer.White, Positions.F2, Positions.F3),
                new ChessMove(ChessPlayer.Black, Positions.D8, Positions.H4)
            };

            foreach (var move in moves)
            {
                game = mechanism.ApplyMove(game, move);
            }

            var actual = mechanism.GetGameState(game);

            Assert.Equal(expected, actual);
        }
コード例 #15
0
        public void ChessBoardInitializerTest_InitBoard_PiecesAtTheirDefaultPositions()
        {
            var initializer = new ChessRepresentationInitializer();
            var board       = initializer.Create();

            Assert.Equal(ChessPieces.WhiteRook, board[Positions.A1]);
            Assert.Equal(ChessPieces.WhiteKnight, board[Positions.B1]);
            Assert.Equal(ChessPieces.WhiteBishop, board[Positions.C1]);
            Assert.Equal(ChessPieces.WhiteQueen, board[Positions.D1]);
            Assert.Equal(ChessPieces.WhiteKing, board[Positions.E1]);
            Assert.Equal(ChessPieces.WhiteBishop, board[Positions.F1]);
            Assert.Equal(ChessPieces.WhiteKnight, board[Positions.G1]);
            Assert.Equal(ChessPieces.WhiteRook, board[Positions.H1]);

            Assert.Equal(ChessPieces.WhitePawn, board[Positions.A2]);
            Assert.Equal(ChessPieces.WhitePawn, board[Positions.B2]);
            Assert.Equal(ChessPieces.WhitePawn, board[Positions.C2]);
            Assert.Equal(ChessPieces.WhitePawn, board[Positions.D2]);
            Assert.Equal(ChessPieces.WhitePawn, board[Positions.E2]);
            Assert.Equal(ChessPieces.WhitePawn, board[Positions.F2]);
            Assert.Equal(ChessPieces.WhitePawn, board[Positions.G2]);
            Assert.Equal(ChessPieces.WhitePawn, board[Positions.H2]);

            Assert.Null(board[Positions.A3]);
            Assert.Null(board[Positions.B3]);
            Assert.Null(board[Positions.C3]);
            Assert.Null(board[Positions.D3]);
            Assert.Null(board[Positions.E3]);
            Assert.Null(board[Positions.F3]);
            Assert.Null(board[Positions.G3]);
            Assert.Null(board[Positions.H3]);

            Assert.Null(board[Positions.A4]);
            Assert.Null(board[Positions.B4]);
            Assert.Null(board[Positions.C4]);
            Assert.Null(board[Positions.D4]);
            Assert.Null(board[Positions.E4]);
            Assert.Null(board[Positions.F4]);
            Assert.Null(board[Positions.G4]);
            Assert.Null(board[Positions.H4]);

            Assert.Null(board[Positions.A5]);
            Assert.Null(board[Positions.B5]);
            Assert.Null(board[Positions.C5]);
            Assert.Null(board[Positions.D5]);
            Assert.Null(board[Positions.E5]);
            Assert.Null(board[Positions.F5]);
            Assert.Null(board[Positions.G5]);
            Assert.Null(board[Positions.H5]);

            Assert.Null(board[Positions.A6]);
            Assert.Null(board[Positions.B6]);
            Assert.Null(board[Positions.C6]);
            Assert.Null(board[Positions.D6]);
            Assert.Null(board[Positions.E6]);
            Assert.Null(board[Positions.F6]);
            Assert.Null(board[Positions.G6]);
            Assert.Null(board[Positions.H6]);

            Assert.Equal(ChessPieces.BlackPawn, board[Positions.A7]);
            Assert.Equal(ChessPieces.BlackPawn, board[Positions.B7]);
            Assert.Equal(ChessPieces.BlackPawn, board[Positions.C7]);
            Assert.Equal(ChessPieces.BlackPawn, board[Positions.D7]);
            Assert.Equal(ChessPieces.BlackPawn, board[Positions.E7]);
            Assert.Equal(ChessPieces.BlackPawn, board[Positions.F7]);
            Assert.Equal(ChessPieces.BlackPawn, board[Positions.G7]);
            Assert.Equal(ChessPieces.BlackPawn, board[Positions.H7]);

            Assert.Equal(ChessPieces.BlackRook, board[Positions.A8]);
            Assert.Equal(ChessPieces.BlackKnight, board[Positions.B8]);
            Assert.Equal(ChessPieces.BlackBishop, board[Positions.C8]);
            Assert.Equal(ChessPieces.BlackQueen, board[Positions.D8]);
            Assert.Equal(ChessPieces.BlackKing, board[Positions.E8]);
            Assert.Equal(ChessPieces.BlackBishop, board[Positions.F8]);
            Assert.Equal(ChessPieces.BlackKnight, board[Positions.G8]);
            Assert.Equal(ChessPieces.BlackRook, board[Positions.H8]);
        }