public void White_Castiling_Movement_From_King_To_Right_Rook_Should_Satisfy()
        {
            Board board     = Board.Create();
            Piece whiteKing = Piece.Create(PieceType.King, PieceColor.White);

            board.AddPiece(whiteKing, "E1");
            board.AddPiece(Piece.Create(PieceType.Rook, PieceColor.White), "H1");

            CastlingMovementSpecification castlingMovementSpecification = CastlingMovementSpecification.Create(board);
            bool isSatisfied = castlingMovementSpecification.IsSatisfied(PieceMovement.Create(whiteKing, Position.Create("E1"), Position.Create("G1")));

            Assert.IsTrue(isSatisfied);
        }
        public void Black_Castiling_Movement_From_King_To_Left_Rook_Should_Satisfy()
        {
            Board board     = Board.Create();
            Piece blackKing = Piece.Create(PieceType.King, PieceColor.Black);

            board.AddPiece(blackKing, "E8");
            board.AddPiece(Piece.Create(PieceType.Rook, PieceColor.Black), "A8");

            CastlingMovementSpecification castlingMovementSpecification = CastlingMovementSpecification.Create(board);
            bool isSatisfied = castlingMovementSpecification.IsSatisfied(PieceMovement.Create(blackKing, Position.Create("E8"), Position.Create("C8")));

            Assert.IsTrue(isSatisfied);
        }
        public void Diagonal_Not_Jumper_Movement_Should_Not_Satisfy()
        {
            Board board = Board.Create();

            board.AddPiece(Piece.Create(PieceType.Bishop, PieceColor.White), "C1");

            JumperMovementSpecification jumperMovementSpecification = JumperMovementSpecification.Create(board);

            PieceMovement pieceMovement = PieceMovement.Create(board.GetSquare("C1").Piece,
                                                               Position.Create("C1"),
                                                               Position.Create("F4"));

            Assert.IsFalse(jumperMovementSpecification.IsSatisfied(pieceMovement));
        }
Beispiel #4
0
        public void StartNewGame_And_Move_To_Oponent_Square_Should_Register_Logs()
        {
            Game  game      = Game.StartEmptyGame("Carlos", "Marta");
            Piece whitePawn = Piece.Create(PieceType.Pawn, PieceColor.White);

            game.Board.AddPiece(whitePawn, "B3");
            game.Board.AddPiece(Piece.Create(PieceType.Pawn, PieceColor.Black), "C4");

            bool success = game.Board.HandleMove(PieceMovement.Create(game.Board.GetSquare("B3").Piece, Position.Create("B3"), Position.Create("C4")));

            Assert.IsTrue(success);
            Assert.AreEqual(1, game.WhitesPlayer.TurnLogs.Count);
            Assert.AreEqual(2, game.WhitesPlayer.TurnLogs[0].TurnEvents.Count);
        }
Beispiel #5
0
        public void Capturing_The_King_Ends_Game()
        {
            Game game = Game.StartEmptyGame("Carlos", "Marta");

            game.Board.AddPiece(Piece.Create(PieceType.King, PieceColor.Black), "D4");
            Piece whiteRook = Piece.Create(PieceType.Rook, PieceColor.White);

            game.Board.AddPiece(whiteRook, "D1");

            bool success = game.Board.HandleMove(PieceMovement.Create(whiteRook, Position.Create("D1"), Position.Create("D4")));

            Assert.IsTrue(success);
            Assert.IsTrue(game.IsOver);
            Assert.AreEqual(game.WhitesPlayer, game.Winner);
        }
Beispiel #6
0
        public void Inalid_Rook_Movement_Should_Not_Satisfy()
        {
            Piece whiteRook = Piece.Create(PieceType.Rook, PieceColor.White);
            Piece blackRook = Piece.Create(PieceType.Rook, PieceColor.Black);

            ISpecification <PieceMovement> specification = specification = MovementValidator.ResolveSpecification(_board, PieceType.Rook);

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whiteRook, Position.Create("A1"), Position.Create("C3")))
                );

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackRook, Position.Create("A8"), Position.Create("C6")))
                );
        }
Beispiel #7
0
        public void Diagonal_Pawn_Movement_Should_Satisfy()
        {
            Piece whitePawn = Piece.Create(PieceType.Pawn, PieceColor.White);
            Piece blackPawn = Piece.Create(PieceType.Pawn, PieceColor.Black);

            _board.AddPiece(whitePawn, "D4");
            _board.AddPiece(blackPawn, "C5");

            ISpecification <PieceMovement> specification = specification = MovementValidator.ResolveSpecification(_board, PieceType.Pawn);

            Assert.IsTrue(
                specification.IsSatisfied(PieceMovement.Create(whitePawn, Position.Create("D4"), Position.Create("C5")))
                );
            Assert.IsTrue(
                specification.IsSatisfied(PieceMovement.Create(blackPawn, Position.Create("C5"), Position.Create("D4")))
                );
        }
Beispiel #8
0
        public void NotInitial_Valid_Pawn_Movement_Should_Satisfy()
        {
            Piece whitePawn = Piece.Create(PieceType.Pawn, PieceColor.White);
            Piece blackPawn = Piece.Create(PieceType.Pawn, PieceColor.Black);

            whitePawn.Moved();
            blackPawn.Moved();

            ISpecification <PieceMovement> specification = specification = MovementValidator.ResolveSpecification(_board, PieceType.Pawn);

            Assert.IsTrue(
                specification.IsSatisfied(PieceMovement.Create(whitePawn, Position.Create("A2"), Position.Create("A3")))
                );
            Assert.IsTrue(
                specification.IsSatisfied(PieceMovement.Create(blackPawn, Position.Create("A7"), Position.Create("A6")))
                );
        }
Beispiel #9
0
        public void HandleMove_To_Oponent_Piece_Should_Capture_Piece()
        {
            Board board     = Board.Create();
            Piece whitePawn = Piece.Create(PieceType.Pawn, PieceColor.White);

            board.AddPiece(whitePawn, "C3");
            board.AddPiece(Piece.Create(PieceType.Pawn, PieceColor.Black), "B4");

            Assert.AreEqual(2, board.Pieces.Count);

            board.HandleMove(PieceMovement.Create(whitePawn, Position.Create("C3"), Position.Create("B4")));

            Square destinationSquare = board.GetSquare("B4");

            Assert.IsFalse(destinationSquare.IsEmpty);
            Assert.AreEqual(PieceColor.White, destinationSquare.Piece.Color);
            Assert.IsTrue(board.GetSquare("C3").IsEmpty);
            Assert.AreEqual(1, board.Pieces.Count);
        }
Beispiel #10
0
        public void Initial_Invalid_Pawn_Movement_Should_Not_Satisfy()
        {
            Piece whitePawn = Piece.Create(PieceType.Pawn, PieceColor.White);
            Piece blackPawn = Piece.Create(PieceType.Pawn, PieceColor.Black);

            ISpecification <PieceMovement> specification = specification = MovementValidator.ResolveSpecification(_board, PieceType.Pawn);

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whitePawn, Position.Create("A2"), Position.Create("A5")))
                );
            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whitePawn, Position.Create("A2"), Position.Create("A1")))
                );

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackPawn, Position.Create("A7"), Position.Create("A4")))
                );
            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackPawn, Position.Create("A7"), Position.Create("A8")))
                );
        }
Beispiel #11
0
        public void Inalid_King_Movement_Should_Not_Satisfy()
        {
            Piece whiteKing = Piece.Create(PieceType.King, PieceColor.White);
            Piece blackKing = Piece.Create(PieceType.King, PieceColor.Black);

            ISpecification <PieceMovement> specification = specification = MovementValidator.ResolveSpecification(_board, PieceType.King);

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whiteKing, Position.Create("E1"), Position.Create("E3")))
                );

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whiteKing, Position.Create("E1"), Position.Create("H1")))
                );

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackKing, Position.Create("E8"), Position.Create("G6")))
                );
            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackKing, Position.Create("E8"), Position.Create("E1")))
                );
        }
Beispiel #12
0
        public void Inalid_Knight_Movement_Should_Not_Satisfy()
        {
            Piece whiteKnight = Piece.Create(PieceType.Knight, PieceColor.White);
            Piece blackKnight = Piece.Create(PieceType.Knight, PieceColor.Black);

            ISpecification <PieceMovement> specification = specification = MovementValidator.ResolveSpecification(_board, PieceType.Knight);

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whiteKnight, Position.Create("B1"), Position.Create("B3")))
                );

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whiteKnight, Position.Create("B1"), Position.Create("C2")))
                );

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackKnight, Position.Create("B8"), Position.Create("B5")))
                );
            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackKnight, Position.Create("B8"), Position.Create("D8")))
                );
        }
Beispiel #13
0
        public void Inalid_Bishop_Movement_Should_Not_Satisfy()
        {
            Piece whiteBishop = Piece.Create(PieceType.Bishop, PieceColor.White);
            Piece blackBishop = Piece.Create(PieceType.Bishop, PieceColor.Black);

            ISpecification <PieceMovement> specification = specification = MovementValidator.ResolveSpecification(_board, PieceType.Bishop);

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whiteBishop, Position.Create("C1"), Position.Create("C3")))
                );

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whiteBishop, Position.Create("C1"), Position.Create("H1")))
                );

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackBishop, Position.Create("C8"), Position.Create("D6")))
                );
            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackBishop, Position.Create("C8"), Position.Create("A8")))
                );
        }
Beispiel #14
0
        public void Inalid_Queen_Movement_Should_Not_Satisfy()
        {
            Piece whiteQueen = Piece.Create(PieceType.Queen, PieceColor.White);
            Piece blackQueen = Piece.Create(PieceType.Queen, PieceColor.Black);

            ISpecification <PieceMovement> specification = specification = MovementValidator.ResolveSpecification(_board, PieceType.Queen);

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whiteQueen, Position.Create("D1"), Position.Create("C3")))
                );

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(whiteQueen, Position.Create("D1"), Position.Create("A5")))
                );

            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackQueen, Position.Create("D8"), Position.Create("E6")))
                );
            Assert.IsFalse(
                specification.IsSatisfied(PieceMovement.Create(blackQueen, Position.Create("D8"), Position.Create("F5")))
                );
        }
Beispiel #15
0
        public MakeMoveResult MakeMove(Guid gameId, Position origin, Position destination)
        {
            Game game = GetGame(gameId);

            if (!game.CanStart)
            {
                return(MakeMoveResult.CreateFailedResult("The game needs two players to start."));
            }
            if (game.IsOver)
            {
                return(MakeMoveResult.CreateFailedResult("Game is over, no more movements allowed."));
            }

            if (game.CurrentTurnPlayer.Id != _playerSession.PlayerId)
            {
                return(MakeMoveResult.CreateFailedResult($"Player trying to move is not {game.CurrentTurnPlayer.Color} player."));
            }

            Square originSquare = game.Board.GetSquare(origin.Id);

            if (originSquare.IsEmpty)
            {
                return(MakeMoveResult.CreateFailedResult($"Origin square {origin.Id} is empty."));
            }

            if (originSquare.Piece.Color != game.CurrentTurnPlayer.Color)
            {
                return(MakeMoveResult.CreateFailedResult($"Piece to be moved does not belong to the {game.CurrentTurnPlayer.Color} player."));
            }

            Piece  piece             = originSquare.Piece;
            Player currentTurnPlayer = game.CurrentTurnPlayer;
            bool   success           = game.Board.HandleMove(PieceMovement.Create(piece, origin, destination));

            return(success ?
                   MakeMoveResult.CreateSuccessResult(currentTurnPlayer.GetLastTurn())
                        : MakeMoveResult.CreateFailedResult($"Movement was denied."));
        }
        public void Castling_To_Left_Rook_Should_Return_Rook_Movement()
        {
            Board board     = Board.Create();
            Piece whiteKing = Piece.Create(PieceType.King, PieceColor.White);

            board.AddPiece(whiteKing, "E1");
            board.AddPiece(Piece.Create(PieceType.Rook, PieceColor.White), "A1");

            CastlingEvaluationResult result = CastlingEvaluator.EvaluateCastling(board, PieceMovement.Create(whiteKing, Position.Create("E1"), Position.Create("C1")));

            Assert.IsNotNull(result.Rook);
            Assert.AreEqual("A1", result.From.Id);
            Assert.AreEqual("D1", result.To.Id);
        }