public void IsCheck_BlackTeam_BlackChecked_Bishop_ShouldReturnFalse()
        {
            // arrange
            var board = new OrdinaryChessBoard();
            var blackPawnDeletePosition  = new Position(1, 3);
            var whiteBishopStartPosition = new Position(7, 5);
            var whiteBishopEndPosition   = new Position(4, 0);

            board.RemoveFigure(blackPawnDeletePosition);
            var bishop = board.RemoveFigure(whiteBishopStartPosition);

            bishop.Move(whiteBishopEndPosition);
            board.SetFigure(bishop, whiteBishopEndPosition);

            var moveValidator = new OrdinaryBoardMoveValidator(board);
            var verifier      = new OrdinaryBoardCheckVerifier(board, moveValidator);

            TeamColor teamColor = TeamColor.Black;

            // act
            var result = verifier.IsCheck(teamColor);

            // assert
            Assert.False(result);
        }
        public void IsCheck_BlackTeam_Check_Bishop_ShouldReturnTrue()
        {
            // arrange
            var board = new OrdinaryChessBoard();
            var pawnDeletePosition  = new Position(6, 3);
            var bishopStartPosition = new Position(0, 5);
            var bishopEndPosition   = new Position(3, 0);

            board.RemoveFigure(pawnDeletePosition);
            var bishop = board.RemoveFigure(bishopStartPosition);

            bishop.Move(bishopEndPosition);
            board.SetFigure(bishop, bishopEndPosition);

            var moveValidator = new OrdinaryBoardMoveValidator(board);
            var verifier      = new OrdinaryBoardCheckVerifier(board, moveValidator);

            TeamColor teamColor = TeamColor.Black;

            // act
            var result = verifier.IsCheck(teamColor);

            // assert
            Assert.True(result);
        }
Пример #3
0
        /// <summary>
        /// Helper function to IsCheckMate
        /// Verify if Any of king possible move is not Check
        /// </summary>
        /// <param name="king"></param>
        /// <param name="teamColor"></param>
        /// <returns></returns>
        private bool VerifyIfKingMayEscape(Figure king, TeamColor teamColor)
        {
            IBoard copyBoard = _board.Copy();
            var    validator = new OrdinaryBoardMoveValidator(copyBoard);
            var    verifier  = new OrdinaryBoardCheckVerifier(copyBoard, validator);

            foreach (var possibleKingMove in GetPossibleKingMoves(king))
            {
                King newKing = new King(possibleKingMove, king.TeamColor);
                copyBoard.RemoveFigure(king.Position);
                var figureAtKingMove = copyBoard.RemoveFigure(possibleKingMove);
                copyBoard.SetFigure(newKing, possibleKingMove);
                if (!verifier.IsCheck(teamColor))
                {
                    return(true);
                }
                copyBoard.SetFigure(figureAtKingMove, possibleKingMove);
                copyBoard.SetFigure(king, king.Position);
            }
            return(false);
        }
        public void IsCheck_WhiteTeam_NotCheck_ShouldReturnFalse()
        {
            // arrange
            var board = new OrdinaryChessBoard();
            var startBlackPawnPosition = new Position(1, 5);
            var endBlackPawnPosition   = new Position(3, 5);

            var blackPawn = board.RemoveFigure(startBlackPawnPosition);

            blackPawn.Move(endBlackPawnPosition);
            board.SetFigure(blackPawn, endBlackPawnPosition);
            var moveValidator = new OrdinaryBoardMoveValidator(board);
            var checkVerifier = new OrdinaryBoardCheckVerifier(board, moveValidator);

            TeamColor teamColor = TeamColor.White;

            // act
            var result = checkVerifier.IsCheck(teamColor);

            // assert
            Assert.False(result);
        }
        [Test] public void IsCheck_WhiteTeam_WhiteChecked_Knight_ShouldReturnFalse()
        {
            // arrange
            var board = new OrdinaryChessBoard();
            var knightStartPosition = new Position(0, 1);
            var knightEndPosition   = new Position(5, 3);

            var knight = board.RemoveFigure(knightStartPosition);

            knight.Move(knightEndPosition);
            board.SetFigure(knight, knightEndPosition);

            var moveValidator = new OrdinaryBoardMoveValidator(board);
            var verifier      = new OrdinaryBoardCheckVerifier(board, moveValidator);

            TeamColor teamColor = TeamColor.White;

            // act
            var result = verifier.IsCheck(teamColor);

            // assert
            Assert.False(result);
        }
Пример #6
0
        /// <summary>
        /// Helper function to IsCheckMate
        /// Verify if after killing there is not Check
        /// </summary>
        /// <param name="aimPosition">Position of figure which cause Check</param>
        /// <param name="teamColor">Color of checked Team</param>
        /// <returns></returns>
        private bool VerifyIfAfterKillKingWillBeFree(Position aimPosition, TeamColor teamColor)
        {
            IBoard copyBoard = _board.Copy();
            var    validator = new OrdinaryBoardMoveValidator(copyBoard);
            var    verifier  = new OrdinaryBoardCheckVerifier(copyBoard, validator);

            copyBoard.RemoveFigure(aimPosition);
            foreach (var possibleKiller in FindPossibleKillers(aimPosition, teamColor))
            {
                var startPosition = possibleKiller.Position;
                var killer        = copyBoard.RemoveFigure(startPosition);
                killer.Move(aimPosition);
                copyBoard.SetFigure(killer, aimPosition);
                if (!verifier.IsCheck(teamColor))
                {
                    killer.Move(startPosition);
                    return(true);
                }
                killer.Move(startPosition);
                copyBoard.SetFigure(killer, startPosition);
            }
            return(false);
        }
Пример #7
0
        private bool VerifyIfOtherFigureMayBlock(Figure culprit, Position kingPosition, TeamColor teamColor)
        {
            IBoard copyBoard = _board.Copy();
            var    validator = new OrdinaryBoardMoveValidator(copyBoard);
            var    verifier  = new OrdinaryBoardCheckVerifier(copyBoard, validator);

            foreach (var figureAndPos in FindPossibleBlockers(culprit, kingPosition, teamColor))
            {
                var startPosition  = figureAndPos.Item1.Position;
                var blocker        = copyBoard.RemoveFigure(startPosition);
                var figureAtEndPos = copyBoard.RemoveFigure(figureAndPos.Item2);
                blocker.Move(figureAndPos.Item2);
                copyBoard.SetFigure(blocker, figureAndPos.Item2);
                if (!verifier.IsCheck(teamColor))
                {
                    blocker.Move(startPosition);
                    return(true);
                }
                blocker.Move(startPosition);
                copyBoard.SetFigure(blocker, startPosition);
                copyBoard.SetFigure(figureAtEndPos, figureAndPos.Item2);
            }
            return(false);
        }