Example #1
0
        public NormalMoveResultInfo NormalMove(NormalMovePositions positions, Type FigureType, Colors color)
        {
            Figure figure = this.chessBoard.GetFigureOnPosition(positions.InitialPosition);

            NormalMoveResultInfo normalMoveResultInfo = new NormalMoveResultInfo();

            normalMoveResultInfo.IsTheMoveValid = this.ValidateMove(positions, FigureType, color);

            if (normalMoveResultInfo.IsTheMoveValid == null)
            {
                chessBoard.RemoveFigureOnPosition(positions.InitialPosition);
                chessBoard.PutFigureOnPosition(positions.TargetPosition, figure);

                this.movesInTheGame.Add(new NormalMoveDabModel(positions.InitialPosition.Horizontal
                                                               , positions.InitialPosition.Vertical, positions.TargetPosition.Horizontal, positions.TargetPosition.Vertical
                                                               , FigureType.Name, color.ToString(), this.counterOfTheMoves));

                this.counterOfTheMoves++;

                if (figure is Pawn && ((Pawn)figure).isPositionProducable(positions.TargetPosition))
                {
                    normalMoveResultInfo.HasTheFigurePawnProducedItself = true;
                }

                this.PlayerOnTurn = ChangePlayer();
            }

            return(normalMoveResultInfo);
        }
Example #2
0
        public InvalidMoveReason?ValidateMoveWithoutCheck(NormalMovePositions positions, Type FigureType, Colors color, ChessBoard chessBoardParam)
        {
            Figure figureToMove           = chessBoardParam.GetFigureOnPosition(positions.InitialPosition);
            Figure figureOnTargetPosition = chessBoardParam.GetFigureOnPosition(positions.TargetPosition);

            if (figureToMove == null || figureToMove.GetType() != FigureType || figureToMove.color != color)
            {
                return(InvalidMoveReason.ThereIsntSuchFigureAndColorOnTheGivenPosition);
            }
            if (figureOnTargetPosition != null &&
                (figureOnTargetPosition.color == figureToMove.color))
            {
                return(InvalidMoveReason.TheFigureOnTheTargetPositionIsFriendlyOrEnemyKing);
            }

            if (figureToMove is Pawn)
            {
                if (((Pawn)figureToMove).AreMovePositionsPossible(positions) == true &&
                    figureOnTargetPosition != null)
                {
                    return(InvalidMoveReason.MovePositionsAreNotValid);
                }

                if (
                    ((((Pawn)figureToMove).AreMovePositionsPossible(positions) == false &&
                      ((Pawn)figureToMove).IsAttackingMovePossible(positions) == false) ||
                     (
                         ((Pawn)figureToMove).IsAttackingMovePossible(positions) &&
                         (figureOnTargetPosition == null ||
                          figureOnTargetPosition.color == figureToMove.color)
                     )
                    ))
                {
                    return(InvalidMoveReason.MovePositionsAreNotValid);
                }
            }
            else
            {
                if (figureToMove.AreMovePositionsPossible(positions) == false)
                {
                    return(InvalidMoveReason.MovePositionsAreNotValid);
                }
            }

            if (figureToMove is IUnableToJump)
            {
                foreach (var position in ((IUnableToJump)figureToMove).GetPositionsInTheWayOfMove(positions))
                {
                    if (chessBoardParam.GetFigureOnPosition(position) != null)
                    {
                        return(InvalidMoveReason.ThereAreOtherPiecesOnTheWay);
                    }
                }
            }

            return(null);
        }
Example #3
0
        public void NormalMoveWithoutValidation(NormalMovePositions normalMovePositions, Type type, Colors color)
        {
            Figure figure = this.chessBoard.GetFigureOnPosition(normalMovePositions.InitialPosition);

            //NormalMoveResultInfo normalMoveResultInfo = new NormalMoveResultInfo();



            chessBoard.RemoveFigureOnPosition(normalMovePositions.InitialPosition);
            chessBoard.PutFigureOnPosition(normalMovePositions.TargetPosition, figure);

            //this.movesInTheGame.Add(new NormalMoveDabModel(normalMovePositions.InitialPosition.Horizontal
            //    , normalMovePositions.InitialPosition.Vertical, normalMovePositions.TargetPosition.Horizontal, normalMovePositions.TargetPosition.Vertical
            //    , type.Name, color.ToString(), this.counterOfTheMoves));

            //this.counterOfTheMoves++;

            //if (figure is Pawn && ((Pawn)figure).isPositionProducable(normalMovePositions.TargetPosition))
            //{
            //    normalMoveResultInfo.HasTheFigurePawnProducedItself = true;
            //}

            this.PlayerOnTurn = ChangePlayer();
        }
Example #4
0
        public InvalidMoveReason?ValidateMove(NormalMovePositions positions, Type FigureType, Colors color)
        {
            Figure figureToMove           = this.chessBoard.GetFigureOnPosition(positions.InitialPosition);
            Type   actualFigureType       = figureToMove.GetType();
            Figure figureOnTargetPosition = this.chessBoard.GetFigureOnPosition(positions.TargetPosition);

            if (figureToMove == null || actualFigureType.FullName != FigureType.FullName || figureToMove.color != color)
            {
                return(InvalidMoveReason.ThereIsntSuchFigureAndColorOnTheGivenPosition);
            }
            if (figureOnTargetPosition != null &&
                (figureOnTargetPosition.GetType() == typeof(King) || figureOnTargetPosition.color == figureToMove.color))
            {
                return(InvalidMoveReason.TheFigureOnTheTargetPositionIsFriendlyOrEnemyKing);
            }

            if (figureToMove is Pawn)
            {
                if (((Pawn)figureToMove).AreMovePositionsPossible(positions) == true &&
                    figureOnTargetPosition != null)
                {
                    return(InvalidMoveReason.MovePositionsAreNotValid);
                }

                if (
                    ((((Pawn)figureToMove).AreMovePositionsPossible(positions) == false &&
                      ((Pawn)figureToMove).IsAttackingMovePossible(positions) == false) ||
                     (
                         ((Pawn)figureToMove).IsAttackingMovePossible(positions) &&
                         (figureOnTargetPosition == null ||
                          figureOnTargetPosition.color == figureToMove.color)
                     )
                    ))
                {
                    return(InvalidMoveReason.MovePositionsAreNotValid);
                }
            }
            else
            {
                if (figureToMove.AreMovePositionsPossible(positions) == false)
                {
                    return(InvalidMoveReason.MovePositionsAreNotValid);
                }
            }

            if (figureToMove is IUnableToJump)
            {
                foreach (var position in ((IUnableToJump)figureToMove).GetPositionsInTheWayOfMove(positions))
                {
                    if (this.chessBoard.GetFigureOnPosition(position) != null)
                    {
                        return(InvalidMoveReason.ThereAreOtherPiecesOnTheWay);
                    }
                }
            }

            ChessBoard chessBoard = this.chessBoard.GetVirtualChessBoardAfterMove(positions);

            if (CheckForCheck(chessBoard, color))
            {
                return(InvalidMoveReason.MovementResultsInCheckOfTheFriendlyKing);
            }

            return(null);
        }