示例#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
        }
示例#2
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");
            }
        }
示例#3
0
文件: Engine.cs 项目: Rillehh/chess
        private void MoveFigure(int x, int y, int xNew, int yNew, Figure[,] table = null)
        {
            if (table == null)
            {
                table = figureTable;
            }
            else
            {
                table = tempTable;
            }

            Figure oldFigure = table[x, y];

            oldFigure.Move(xNew, yNew);

            if (table == figureTable)
            {
                previousMove = new Tuple <PlayerType, FigureType, int, int, int, int>(onMove, oldFigure.type, x, y, xNew, yNew);
            }

            if (table[x, y] != null && table[x, y] is Pawn)
            {
                Pawn pawn = table[x, y] as Pawn;

                // Check if en passant
                if (xNew == x + pawn.Direction && (yNew == y - 1 || yNew == y + 1) && table[xNew, yNew] == null)
                {
                    table[x, yNew] = null;
                }

                table[xNew, yNew] = oldFigure;
                table[x, y]       = null;

                // Check for promotion (Upgrade this code!!!) - only in real move mode
                if (table == figureTable && (xNew == 0 || xNew == 7))
                {
                    new Promotion(onMove).ShowDialog();
                    FigureType promotionFigure = Promotion.promotedFigure;

                    switch (promotionFigure)
                    {
                    case FigureType.KNIGHT:
                        table[xNew, yNew] = new Knight(xNew, yNew, onMove, promotionFigure, this);
                        break;

                    case FigureType.BISHOP:
                        table[xNew, yNew] = new Bishop(xNew, yNew, onMove, promotionFigure, this);
                        break;

                    case FigureType.ROOK:
                        table[xNew, yNew] = new Rook(xNew, yNew, onMove, promotionFigure, this);
                        break;

                    case FigureType.QUEEN:
                        table[xNew, yNew] = new Queen(xNew, yNew, onMove, promotionFigure, this);
                        break;

                    default:
                        break;
                    }
                }
            }
            // Rokada (Castling)
            else if (table == figureTable && table[x, y] != null && table[x, y] is King && Math.Abs(yNew - y) == 2)
            {
                // Move king
                table[xNew, yNew] = oldFigure;
                table[x, y]       = null;

                // Small - move rook
                if (yNew > y)
                {
                    table[x, yNew + 1].Move(x, yNew - 1);

                    table[x, yNew - 1] = table[x, yNew + 1];
                    table[x, yNew + 1] = null;
                }
                // Big - move rook
                else
                {
                    table[x, yNew - 2].Move(x, yNew + 1);

                    table[x, yNew + 1] = table[x, yNew - 2];
                    table[x, yNew - 2] = null;
                }
            }
            else
            {
                table[xNew, yNew] = oldFigure;
                table[x, y]       = null;
            }
        }