Esempio n. 1
0
        private void MovePawn(Figure f, char X, int Y)
        {
            var chessBoxStart = this.getChessBoxByCoordinates(f.Xpositon, f.Ypositon);
            var chessBoxEnd   = this.getChessBoxByCoordinates(X, Y);

            if (PathBetweenBoxesFree(chessBoxStart, chessBoxEnd))
            {
                if ((chessBoxStart.Xcoord - chessBoxEnd.Xcoord != 0))
                { //check if the pawn is trying to capture a figure
                    if (chessBoxEnd.isFigureOn())
                    {
                        if (chessBoxEnd.Figure.isWhite != f.isWhite)
                        {
                            f.Move(X, Y);
                            Figure FigureToKill = chessBoxEnd.Figure;
                            chessBoxEnd.deleteFigure();
                            chessBoxStart.Figure = null;
                            chessBoxEnd.Figure   = f;
                            if (isInChess(f.isWhite))
                            {
                                chessBoxStart.Figure = chessBoxEnd.Figure;
                                chessBoxEnd.Figure.BackToLastPosition();
                                FigureToKill.BackToLastPosition();
                                chessBoxEnd.Figure = FigureToKill;
                                throw new KingInChessExeption("You cannot move here! You will be in chess!");
                            }
                        }
                        else
                        {
                            throw new InvalidAttackExeption("You cannot capture your own figure!");
                        }
                    }
                    else if (getChessBoxByCoordinates(chessBoxEnd.Xcoord, chessBoxEnd.Ycoord - 1).isFigureOn() && getChessBoxByCoordinates(chessBoxEnd.Xcoord, chessBoxEnd.Ycoord - 1).Figure.getFigureType() == "Pawn")
                    { //checking for "En Passant"
                        ChessBox thePawnToBeCaptured = this.getChessBoxByCoordinates(chessBoxEnd.Xcoord, chessBoxEnd.Ycoord - 1);
                        Pawn     EndPostionFigure    = (Pawn)this.getChessBoxByCoordinates(chessBoxEnd.Xcoord, chessBoxEnd.Ycoord - 1).Figure;
                        if ((f.isWhite != EndPostionFigure.isWhite) && (EndPostionFigure.lastMoveWasTwoSquares))
                        {
                            f.Move(X, Y);
                            //Figure FigureToKill = chessBoxEnd.Figure;
                            thePawnToBeCaptured.deleteFigure();
                            chessBoxStart.Figure = null;
                            chessBoxEnd.Figure   = f;
                            if (isInChess(f.isWhite))
                            {
                                chessBoxStart.Figure = chessBoxEnd.Figure;
                                chessBoxEnd.Figure.BackToLastPosition();

                                EndPostionFigure.pawnReborn();
                                getChessBoxByCoordinates(EndPostionFigure.Xpositon, EndPostionFigure.Ypositon).Figure = EndPostionFigure;
                                chessBoxEnd.Figure = null;
                                //chessBoxEnd.Figure = FigureToKill;
                                throw new KingInChessExeption("You cannot move here! You will be in chess!");
                            }
                        }
                        else
                        {
                            throw new InvalidFigureMovementExeption("Invalid pawn coordinates!");
                        }
                    }
                    else if (getChessBoxByCoordinates(chessBoxEnd.Xcoord, chessBoxEnd.Ycoord + 1).isFigureOn() && getChessBoxByCoordinates(chessBoxEnd.Xcoord, chessBoxEnd.Ycoord + 1).Figure.getFigureType() == "Pawn")
                    { //checking for "En Passant" 2
                        ChessBox thePawnToBeCaptured = this.getChessBoxByCoordinates(chessBoxEnd.Xcoord, chessBoxEnd.Ycoord + 1);
                        Pawn     EndPostionFigure    = (Pawn)this.getChessBoxByCoordinates(chessBoxEnd.Xcoord, chessBoxEnd.Ycoord + 1).Figure;
                        if ((f.isWhite != EndPostionFigure.isWhite) && (EndPostionFigure.lastMoveWasTwoSquares))
                        {
                            f.Move(X, Y);
                            //Figure FigureToKill = chessBoxEnd.Figure;
                            thePawnToBeCaptured.deleteFigure();
                            chessBoxStart.Figure = null;
                            chessBoxEnd.Figure   = f;
                            if (isInChess(f.isWhite))
                            {
                                chessBoxStart.Figure = chessBoxEnd.Figure;
                                chessBoxEnd.Figure.BackToLastPosition();

                                EndPostionFigure.pawnReborn();
                                getChessBoxByCoordinates(EndPostionFigure.Xpositon, EndPostionFigure.Ypositon).Figure = EndPostionFigure;
                                chessBoxEnd.Figure = null;
                                //chessBoxEnd.Figure = FigureToKill;
                                throw new KingInChessExeption("You cannot move here! You will be in chess!");
                            }
                        }
                        else
                        {
                            throw new InvalidFigureMovementExeption("Invalid pawn coordinates!");
                        }
                    }
                    else
                    {
                        throw new InvalidFigureMovementExeption("Invalid pawn coordinates!");
                    }
                }
                else
                {
                    if (!chessBoxEnd.isFigureOn())
                    {
                        f.Move(X, Y);
                        chessBoxStart.Figure = null;
                        chessBoxEnd.Figure   = f;
                        if (isInChess(f.isWhite))
                        {
                            chessBoxStart.Figure = chessBoxEnd.Figure;
                            chessBoxEnd.Figure.BackToLastPosition();
                            chessBoxEnd.Figure = null;
                            throw new KingInChessExeption("You cannot move here! You will be in chess!");
                        }
                    }
                    else
                    {
                        throw new InvalidAttackExeption("Pawn cannot capture figure on this coordiantes!");
                    }
                }
            }
            else
            {
                throw new PathBetweenBoxesNotFreeExeption("The path between the boxes is not free");
            }
        }