Exemplo n.º 1
0
        public void Move(Figure f, char x, int y)

        {
            var  chessBoxStart = this.getChessBoxByCoordinates(f.Xpositon, f.Ypositon);
            var  chessBoxEnd   = this.getChessBoxByCoordinates(x, y);
            bool isFigureWhite = f.isWhite;

            if (f.getFigureType().Equals("Pawn"))
            {
                this.MovePawn(f, x, y);
            }
            else if (f.getFigureType().Equals("King") && chessBoxEnd.isFigureOn() && chessBoxEnd.Figure.getFigureType().Equals("Rook") && chessBoxStart.Figure.isWhite == chessBoxEnd.Figure.isWhite)
            {//checking for Rocade
                King king = (King)f;
                Rook rook = (Rook)chessBoxEnd.Figure;

                if (PathBetweenBoxesFree(chessBoxStart, chessBoxEnd))
                {
                    if ((!king.isMoved) && (!rook.isMoved))
                    {
                        if (rook.Xpositon == 'A')
                        {
                            chessBoxEnd.Figure.Move('D', chessBoxEnd.Ycoord);
                            this.getChessBoxByCoordinates('D', chessBoxEnd.Ycoord).Figure = chessBoxEnd.Figure;
                            chessBoxEnd.deleteFigure();

                            chessBoxStart.Figure.Move('C', chessBoxStart.Ycoord);
                            this.getChessBoxByCoordinates('C', chessBoxStart.Ycoord).Figure = chessBoxStart.Figure;
                            chessBoxStart.deleteFigure();

                            if (isInChess(f.isWhite))
                            {
                                king.BackToLastPosition();
                                rook.BackToLastPosition();
                                chessBoxEnd.Figure   = this.getChessBoxByCoordinates('D', chessBoxEnd.Ycoord).Figure;
                                chessBoxStart.Figure = this.getChessBoxByCoordinates('C', chessBoxStart.Ycoord).Figure;
                                this.getChessBoxByCoordinates('D', chessBoxEnd.Ycoord).deleteFigure();
                                this.getChessBoxByCoordinates('C', chessBoxStart.Ycoord).deleteFigure();
                                throw new KingInChessExeption("You cannot move here! You will be in chess!");
                            }
                        }
                        else
                        {
                            chessBoxEnd.Figure.Move('F', chessBoxEnd.Ycoord);
                            this.getChessBoxByCoordinates('F', chessBoxEnd.Ycoord).Figure = chessBoxEnd.Figure;
                            chessBoxEnd.deleteFigure();

                            chessBoxStart.Figure.Move('G', chessBoxStart.Ycoord);
                            this.getChessBoxByCoordinates('G', chessBoxStart.Ycoord).Figure = chessBoxStart.Figure;
                            chessBoxStart.deleteFigure();

                            if (isInChess(f.isWhite))
                            {
                                king.BackToLastPosition();
                                rook.BackToLastPosition();
                                chessBoxEnd.Figure   = this.getChessBoxByCoordinates('F', chessBoxEnd.Ycoord).Figure;
                                chessBoxStart.Figure = this.getChessBoxByCoordinates('G', chessBoxStart.Ycoord).Figure;
                                this.getChessBoxByCoordinates('F', chessBoxEnd.Ycoord).deleteFigure();
                                this.getChessBoxByCoordinates('G', chessBoxStart.Ycoord).deleteFigure();
                                throw new KingInChessExeption("You cannot move here! You will be in chess!");
                            }
                        }
                    }
                    else
                    {
                        throw new RocadeNotPossibleExeption("Rocade not possible!The figures has been moved");
                    }
                }
                else
                {
                    throw new RocadeNotPossibleExeption("Rocade not possible!There is a figure on the way!");
                }
            }
            else
            {
                if (PathBetweenBoxesFree(chessBoxStart, chessBoxEnd))
                {
                    if (chessBoxEnd.isFigureOn())
                    {
                        if (chessBoxStart.Figure.isWhite != chessBoxEnd.Figure.isWhite)
                        {
                            Figure figureToDestroy = chessBoxEnd.Figure;
                            f.Move(x, y);
                            chessBoxEnd.Figure.DestroyFigure();
                            chessBoxStart.Figure = null;
                            chessBoxEnd.Figure   = f;
                            if (isInChess(f.isWhite))
                            {
                                chessBoxStart.Figure = f;
                                f.BackToLastPosition();
                                figureToDestroy.BackToLastPosition();
                                chessBoxEnd.Figure = figureToDestroy;
                                throw new KingInChessExeption("You cannot move here! You will be in chess!");
                            }
                        }
                        else
                        {
                            throw new InvalidAttackExeption("Invalid movement! You cannot attack your figure!");
                        }
                    }
                    else
                    {
                        f.Move(x, y);
                        chessBoxStart.Figure = null;
                        chessBoxEnd.Figure   = f;
                        if (isInChess(f.isWhite))
                        {
                            f.BackToLastPosition();
                            chessBoxStart.Figure = f;
                            chessBoxEnd.Figure   = null;
                            throw new KingInChessExeption("You cannot move here! You will be in chess!");
                        }
                    }
                }
                else
                {
                    throw new PathBetweenBoxesNotFreeExeption("The path between the boxes is not free");
                }
            }

            ///if Move coordinates invalid figure.move -argument exeption
        }