Example #1
0
        /// <summary>
        ///     Creates a board with starting positions.
        /// </summary>
        public Board(IBoardState state)
        {
            State = state;

            State.Add(ChessPosition.WhiteStart);
            State.Add(ChessPosition.BlackStart);
        }
Example #2
0
        /// <summary>
        ///     Can any friendly piece block the attacker?
        /// </summary>
        /// <param name="threateningPiece"></param>
        /// <returns></returns>
        private bool CanFriendlyPieceMoveBetweenKingAndAttacker(IPiece threateningPiece, IBoardState boardState)
        {
            switch (threateningPiece)
            {
            // all cases fall through
            case IKnight _:   // knights jump pieces, cannot move between
            case IPawn _:     // pawns attack in an adjacent square, cannot move between
            case IKing _:     // king will never be checking another king.
                return(false);
            }

            foreach (IPiece piece in ActivePlayerPieces.Where(p => !(p is IKing) && p.Location != ChessPosition.None))
            {
                piece.GenerateMoves(boardState);

                // use a copy of the MoveSet to prevent the collection from being modified in the following loop
                IBoardState copyOfMoveSet = ModelLocator.BoardState;
                copyOfMoveSet.Add(piece.MoveSet);

                foreach (ChessPosition location in copyOfMoveSet)
                {
                    var move = ModelLocator.CreateMove(piece.Location, location);

                    var isMoveLegal            = IsMoveLegal(piece, move, boardState);
                    var isKingInCheckAfterMove = DoesPotentialMoveLeaveKingInCheck(move);

                    if (isMoveLegal && !isKingInCheckAfterMove)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #3
0
        /// <summary>
        ///     Returns whether the piece has any possible legal captures.
        /// </summary>
        /// <param name="piece"></param>
        /// <returns></returns>
        private bool DoesPieceHaveLegalCapture(IPiece piece)
        {
            piece.GenerateCaptures(GameBoard.State, ActivePlayerBoardState);
            ICapture capture = ModelLocator.Capture;

            capture.StartingPosition = piece.Location;

            IBoardState captureSet = ModelLocator.BoardState;

            captureSet.Add(piece.CaptureSet);

            foreach (ChessPosition position in captureSet)
            {
                capture.EndingPosition = position;
                if (IsCaptureLegal(piece, capture, GameBoard.State))
                {
                    return(true);
                }
                if (piece is IPawn && IsCaptureLegalEnPassant(piece, capture, GameBoard))
                {
                    return(true);
                }
            }

            return(false);
        }
Example #4
0
        /// <summary>
        ///     Creates the state of the enemy board.
        /// </summary>
        /// <param name="boardState"></param>
        /// <param name="owningPlayerBoardState"></param>
        /// <returns></returns>
        protected IBoardState CreateEnemyBoardState(IBoardState boardState, IBoardState owningPlayerBoardState)
        {
            IBoardState state = ModelLocator.BoardState;

            state.Add(boardState);
            state.Remove(owningPlayerBoardState);

            return(state);
        }
Example #5
0
        public void Should_AddLocation()
        {
            // setup
            IBoardState         state    = ModelLocator.BoardState;
            const ChessPosition POSITION = ChessPosition.A1;

            // execute
            state.Add(POSITION);

            // verify
            Assert.IsTrue(state.Contains(POSITION));
        }
Example #6
0
        public void Should_ReturnTrueWhenContains()
        {
            // setup
            IBoardState         state    = ModelLocator.BoardState;
            const ChessPosition POSITION = ChessPosition.A1;

            state.Add(POSITION);

            // execute
            bool contains = state.Contains(POSITION);

            // verify
            Assert.IsTrue(contains);
        }
Example #7
0
        public void Should_RemovePositions()
        {
            // setup
            IBoardState         state    = ModelLocator.BoardState;
            const ChessPosition POSITION = ChessPosition.A1;

            state.Add(POSITION);

            // execute
            state.Remove(POSITION);

            // verify
            Assert.IsFalse(state.Contains(POSITION));
        }
Example #8
0
        public void Should_ClearPositions()
        {
            // setup
            IBoardState state     = ModelLocator.BoardState;
            var         positions = new[] { ChessPosition.A2, ChessPosition.A1, ChessPosition.A4, ChessPosition.E3 };

            foreach (var position in positions)
            {
                state.Add(position);
            }

            // execute
            state.Clear();

            // verify
            Assert.IsTrue(positions.All(p => !state.Contains(p)));
        }
Example #9
0
        public void Should_AddAllPositionsFromBoardstate()
        {
            // setup
            IBoardState state     = ModelLocator.BoardState;
            var         positions = new[] { ChessPosition.A2, ChessPosition.A1, ChessPosition.A4, ChessPosition.E3 };

            foreach (var position in positions)
            {
                state.Add(position);
            }

            IBoardState state2 = ModelLocator.BoardState;

            // execute
            state2.Add(state);

            // verify
            Assert.IsTrue(positions.All(p => state2.Contains(p)));
        }
Example #10
0
        /// <summary>
        ///     Returns whether the piece has any possible legal moves.
        /// </summary>
        /// <param name="piece"></param>
        /// <returns></returns>
        private bool DoesPieceHaveLegalMove(IPiece piece)
        {
            piece.GenerateMoves(GameBoard.State);
            IMove move = ModelLocator.Move;

            move.StartingPosition = piece.Location;

            IBoardState moveSet = ModelLocator.BoardState;

            moveSet.Add(piece.MoveSet);

            foreach (ChessPosition position in moveSet)
            {
                move.EndingPosition = position;
                if (IsMoveLegal(piece, move, GameBoard.State))
                {
                    return(true);
                }
            }

            if (!(piece is IKing))
            {
                return(false);
            }

            IEnumerable <ChessPosition> moves = ModelLocator.CastlingHelper.GetCastleMovesForKing((IKing)piece);

            foreach (ChessPosition castleMove in moves)
            {
                move.EndingPosition = castleMove;

                if (IsCastleLegal(piece, move, GameBoard))
                {
                    return(true);
                }
            }

            return(false);
        }