Example #1
0
 public void ProducePawn(PositionOnTheBoard positionOnTheBoard, Figure prodeucedFigure, Colors colors)
 {
     this.chessBoard.PutFigureOnPosition(positionOnTheBoard, prodeucedFigure);
     this.movesInTheGame.Add(new ProducingPawn()
     {
         OrderInTheGame = this.counterOfTheMoves
     });
     this.counterOfTheMoves++;
 }
Example #2
0
        private bool IsValidCastling(PositionOnTheBoard kingPosition, PositionOnTheBoard rookPosition
                                     , Colors colorOfTheFigures)
        {
            Figure kingFigure           = this.chessBoard.GetFigureOnPosition(kingPosition);
            Figure rookFigure           = this.chessBoard.GetFigureOnPosition(rookPosition);
            Type   actualKingFigureType = kingFigure?.GetType();
            Type   actualRookFigureType = rookFigure?.GetType();

            if (kingFigure == null || actualKingFigureType.FullName != typeof(King).FullName || kingFigure.color != colorOfTheFigures)
            {
                return(false);
            }
            if (rookFigure == null || actualRookFigureType.FullName != typeof(Rook).FullName || rookFigure.color != colorOfTheFigures)
            {
                return(false);
            }
            if (((King)kingFigure).HasBeenMovedFromTheStartOfTheGame || ((Rook)rookFigure).HasBeenMovedFromTheStartOfTheGame)
            {
                return(false);
            }

            if (this.CheckForCheck(this.chessBoard, colorOfTheFigures))
            {
                return(false);
            }
            //if (figureOnTargetPosition != null &&
            //    (figureOnTargetPosition.GetType() == typeof(King) || figureOnTargetPosition.color == figureToMove.color))
            //{
            //    return InvalidMoveReason.TheFigureOnTheTargetPositionIsFriendlyOrEnemyKing;
            //}
            List <PositionOnTheBoard> positionOnTheBoardBetweenRookAndKingAndRookPosition = ((Rook)rookFigure).GetPositionsInTheWayOfMove
                                                                                                (new NormalMovePositions(rookPosition.Horizontal, rookPosition.Vertical, kingPosition.Horizontal, kingPosition.Vertical));

            positionOnTheBoardBetweenRookAndKingAndRookPosition.Add(rookPosition);

            foreach (var pos in positionOnTheBoardBetweenRookAndKingAndRookPosition)
            {
                if (pos.Equals(rookPosition) == false)
                {
                    if (this.chessBoard.GetFigureOnPosition(pos) != null)
                    {
                        return(false);
                    }
                }

                var virtualBoard = this.chessBoard.GetVirtualChessBoardAfterMove(new NormalMovePositions
                                                                                     (kingPosition.Horizontal, kingPosition.Vertical, pos.Horizontal, pos.Vertical));
                if (this.CheckForCheck(virtualBoard, colorOfTheFigures))
                {
                    return(false);
                }
            }
            return(true);
        }
Example #3
0
        public List <PositionOnTheBoard> PossiblePositionsMovement(Type figure, PositionOnTheBoard position, Colors colors, ChessBoard chessBoardParam)
        {
            List <PositionOnTheBoard> positions = new List <PositionOnTheBoard>();

            for (char horizontal = 'a'; horizontal <= 'h'; horizontal++)
            {
                for (int vertical = 1; vertical <= 8; vertical++)
                {
                    if (this.ValidateMoveWithoutCheck(new NormalMovePositions(position.Horizontal, position.Vertical, horizontal, vertical)
                                                      , figure, colors, chessBoardParam) == null)
                    {
                        positions.Add(new PositionOnTheBoard(horizontal, vertical));
                    }
                }
            }

            return(positions);
        }
Example #4
0
 public PositionOnTheBoard GetPossiblePositionOfKingWhenCastlingTheRook(PositionOnTheBoard rookPosition,
                                                                        Colors rookFigureColor)
 {
     if (rookFigureColor == Colors.White)
     {
         if (this.IsValidCastling(new PositionOnTheBoard('e', 1), rookPosition, rookFigureColor))
         {
             return(new PositionOnTheBoard('e', 1));
         }
     }
     else if (rookFigureColor == Colors.Black)
     {
         if (this.IsValidCastling(new PositionOnTheBoard('e', 8), rookPosition, rookFigureColor))
         {
             return(new PositionOnTheBoard('e', 8));
         }
     }
     return(null);
 }
Example #5
0
        public bool MakeCastling(PositionOnTheBoard kingPosition, PositionOnTheBoard rookPosition
                                 , Colors colorOfTheFigures)
        {
            if (this.IsValidCastling(kingPosition, rookPosition, colorOfTheFigures) == false)
            {
                return(false);
            }
            Figure kingFigure = this.chessBoard.GetFigureOnPosition(kingPosition);
            Figure rookFigure = this.chessBoard.GetFigureOnPosition(rookPosition);

            this.chessBoard.RemoveFigureOnPosition(kingPosition);
            this.chessBoard.RemoveFigureOnPosition(rookPosition);

            if (colorOfTheFigures == Colors.White)
            {
                if (rookPosition.Horizontal == 'a')
                {
                    this.chessBoard.PutFigureOnPosition(new PositionOnTheBoard('c', 1), kingFigure);
                    this.chessBoard.PutFigureOnPosition(new PositionOnTheBoard('d', 1), rookFigure);
                }
                else if (rookPosition.Horizontal == 'h')
                {
                    this.chessBoard.PutFigureOnPosition(new PositionOnTheBoard('g', 1), kingFigure);
                    this.chessBoard.PutFigureOnPosition(new PositionOnTheBoard('f', 1), rookFigure);
                }
            }
            else if (colorOfTheFigures == Colors.Black)
            {
                if (rookPosition.Horizontal == 'a')
                {
                    this.chessBoard.PutFigureOnPosition(new PositionOnTheBoard('c', 8), kingFigure);
                    this.chessBoard.PutFigureOnPosition(new PositionOnTheBoard('d', 8), rookFigure);
                }
                else if (rookPosition.Horizontal == 'h')
                {
                    this.chessBoard.PutFigureOnPosition(new PositionOnTheBoard('g', 8), kingFigure);
                    this.chessBoard.PutFigureOnPosition(new PositionOnTheBoard('f', 8), rookFigure);
                }
            }
            this.PlayerOnTurn = ChangePlayer();
            return(true);
        }
Example #6
0
        public List <PositionOnTheBoard> GetAllPossiblePositionsOfPlacingTheFigure(PositionOnTheBoard positionOnTheBoard
                                                                                   , Type figureType, Colors chessFigureColor)
        {
            List <PositionOnTheBoard> attackingPos = new List <PositionOnTheBoard>();

            for (char horizontal = 'a'; horizontal <= 'h'; horizontal++)
            {
                for (int vertical = 1; vertical <= 8; vertical++)
                {
                    if (this.ValidateMove(new NormalMovePositions(positionOnTheBoard
                                                                  .Horizontal, positionOnTheBoard.Vertical
                                                                  , horizontal, vertical),
                                          figureType
                                          , chessFigureColor) == null)
                    {
                        attackingPos.Add(new PositionOnTheBoard(horizontal, vertical));
                    }
                }
            }
            return(attackingPos);
        }
Example #7
0
        public List <PositionOnTheBoard> GetAllPossiblePositionsOfRookWhenCastlingTheKing(PositionOnTheBoard kingPosition,
                                                                                          Colors kingFigureColor)
        {
            List <PositionOnTheBoard> rookPositions = new List <PositionOnTheBoard>();

            for (char horizontal = 'a'; horizontal <= 'h'; horizontal++)
            {
                for (int vertical = 1; vertical <= 8; vertical++)
                {
                    if (this.IsValidCastling(kingPosition, new PositionOnTheBoard(horizontal, vertical), kingFigureColor))
                    {
                        rookPositions.Add(new PositionOnTheBoard(horizontal, vertical));
                    }
                }
            }

            return(rookPositions);
        }