Ejemplo n.º 1
0
        public ChessBoard()
        {
            chessBoard = new ChessBox[8, 8];

            //Fill The Board with ChessBoxes;
            bool isWhite = true;
            int  arrArgX = 0;

            for (int i = 1; (i < 9); i++)
            {
                int arrArgY = 0;

                for (char a = 'A'; a < 'I'; a++)
                {
                    chessBoard[arrArgX, arrArgY] = new ChessBox(a, i, isWhite);
                    if (a != 'H')
                    {
                        isWhite = !isWhite;
                    }

                    arrArgY++;
                }
                arrArgX++;
            }

            //inTheMatrix
            //MatrixCoordinates->ChessBoxCoordinates
            //X->  0-->'A' ,1-->'B'..
            //Y->  0--> 1,  1--> 2 ..
        }
Ejemplo n.º 2
0
 public void Move(ChessBox start, ChessBox end, bool isWhiteColorPlayer)
 {
     if (lastMovedWhite == isWhiteColorPlayer)
     {
         throw new ArgumentException("It's the other player's turn!");
     }
     if ((start.Figure == null) || (start.Figure.isWhite != isWhiteColorPlayer))
     {
         throw new InvalidFigureMovementExeption("Invalid operation! You should move one of your figures!");
     }
     board.Move(start, end);
     lastMovedWhite = !lastMovedWhite;
 }
Ejemplo n.º 3
0
        public void Move(char figureX, int figureY, char destX, int destY, bool isWhiteColorPlayer)
        {
            if (lastMovedWhite == isWhiteColorPlayer)
            {
                throw new ArgumentException("It's the other player's turn!");
            }
            ChessBox figureBox = board.getChessBoxByCoordinates(figureX, figureY);

            if ((figureBox.Figure == null) || (figureBox.Figure.isWhite != isWhiteColorPlayer))
            {
                throw new InvalidFigureMovementExeption("Invalid operation! You should move one of your figures!");
            }
            ChessBox endBox = board.getChessBoxByCoordinates(figureX, figureY);

            board.Move(figureBox, endBox);
            lastMovedWhite = !lastMovedWhite;
        }
Ejemplo n.º 4
0
        private bool isInChess(bool IswhiteKing)
        {
            ChessBox kingSBox = getKingChessBox(IswhiteKing);

            int[] xChange = { -1, 1, 0, 0 };
            int[] yChange = { 0, 0, 1, -1 };
            for (int i = 0; i < 4; i++)//check rook and queen
            {
                char x = kingSBox.Xcoord;
                int  y = kingSBox.Ycoord;
                x = Convert.ToChar(x + xChange[i]);
                y = y + yChange[i];
                while (areValidChessBoxCoordinates(x, y))
                {
                    ChessBox boxToCheck = getChessBoxByCoordinates(x, y);
                    if (boxToCheck.isFigureOn())
                    {
                        if (boxToCheck.Figure.isWhite != IswhiteKing)
                        {
                            if (boxToCheck.Figure.getFigureType().Equals("Rook") || (boxToCheck.Figure.getFigureType().Equals("Queen")))
                            {
                                return(true);
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    x = Convert.ToChar(x + xChange[i]);
                    y = y + yChange[i];
                }
            }
            //check knight
            int[] xKnightMovment = { 1, 1, -1, -1, 2, -2, 2, -2 };
            int[] YKnightMovment = { 2, -2, 2, -2, 1, 1, -1, -1 };

            for (int i = 0; i < 8; i++)
            {
                char xCord = kingSBox.Xcoord;
                int  yCord = kingSBox.Ycoord;
                xCord = Convert.ToChar(xCord + xKnightMovment[i]);
                yCord = yCord + YKnightMovment[i];

                if (areValidChessBoxCoordinates(xCord, yCord))
                {
                    ChessBox chessBoxToCheck = getChessBoxByCoordinates(xCord, yCord);

                    if ((chessBoxToCheck.isFigureOn()) && (chessBoxToCheck.Figure.isWhite != IswhiteKing))
                    {
                        if (chessBoxToCheck.Figure.getFigureType().Equals("Knight"))
                        {
                            return(true);
                        }
                    }
                }
            }
            int[] xBishopMovment = { 1, 1, -1, -1 };
            int[] yBishopMovment = { -1, 1, -1, 1 };
            ///check Bishop
            for (int i = 0; i < 4; i++)
            {
                char xBishopCheck = kingSBox.Xcoord;
                int  yBishopCheck = kingSBox.Ycoord;
                xBishopCheck = Convert.ToChar(xBishopCheck + xBishopMovment[i]);
                yBishopCheck = yBishopCheck + yBishopMovment[i];
                while (areValidChessBoxCoordinates(xBishopCheck, yBishopCheck))
                {
                    ChessBox boxToCheck = getChessBoxByCoordinates(xBishopCheck, yBishopCheck);
                    if (boxToCheck.isFigureOn())
                    {
                        if (boxToCheck.Figure.isWhite != IswhiteKing)
                        {
                            if (boxToCheck.Figure.getFigureType().Equals("Bishop") || (boxToCheck.Figure.getFigureType().Equals("Queen")))
                            {
                                return(true);
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    xBishopCheck = Convert.ToChar(xBishopCheck + xBishopMovment[i]);
                    yBishopCheck = yBishopCheck + yBishopMovment[i];
                }
            }
            ///check King
            int[] xKingMovment = { 1, 1, 1, 0, 0, -1, -1, -1 };
            int[] yKingMovment = { 1, 0, -1, 1, -1, -1, 0, 1 };
            for (int i = 0; i < 8; i++)
            {
                char xKingCheck = kingSBox.Xcoord;
                int  yKingCheck = kingSBox.Ycoord;
                xKingCheck = Convert.ToChar(xKingCheck + xKingMovment[i]);
                yKingCheck = yKingCheck + yKingMovment[i];
                if (areValidChessBoxCoordinates(xKingCheck, yKingCheck))
                {
                    ChessBox boxToCheck = getChessBoxByCoordinates(xKingCheck, yKingCheck);
                    if (boxToCheck.isFigureOn())
                    {
                        if (boxToCheck.Figure.getFigureType().Equals("King") && (boxToCheck.Figure.isWhite != IswhiteKing))
                        {
                            return(true);
                        }
                    }
                }
            }
            ///check Pawn
            char xPawnCheck = kingSBox.Xcoord;
            int  yPawnCheck = kingSBox.Ycoord;

            if (IswhiteKing)
            {
                if (areValidChessBoxCoordinates(Convert.ToChar(xPawnCheck - 1), yPawnCheck - 1))
                {
                    ChessBox boxToCheck = getChessBoxByCoordinates(Convert.ToChar(xPawnCheck - 1), yPawnCheck - 1);
                    if ((boxToCheck.isFigureOn()) && ((boxToCheck.Figure.isWhite != IswhiteKing) && (boxToCheck.Figure.getFigureType().Equals("Pawn"))))
                    {
                        return(true);
                    }
                }
                if (areValidChessBoxCoordinates(Convert.ToChar(xPawnCheck + 1), yPawnCheck - 1))
                {
                    ChessBox boxToCheck = getChessBoxByCoordinates(Convert.ToChar(xPawnCheck + 1), yPawnCheck - 1);
                    if ((boxToCheck.isFigureOn()) && ((boxToCheck.Figure.isWhite != IswhiteKing) && (boxToCheck.Figure.getFigureType().Equals("Pawn"))))
                    {
                        return(true);
                    }
                }
            }
            else
            {
                if (areValidChessBoxCoordinates(Convert.ToChar(xPawnCheck + 1), yPawnCheck + 1))
                {
                    ChessBox boxToCheck = getChessBoxByCoordinates(Convert.ToChar(xPawnCheck + 1), yPawnCheck + 1);
                    if ((boxToCheck.isFigureOn()) && ((boxToCheck.Figure.isWhite != IswhiteKing) && (boxToCheck.Figure.getFigureType().Equals("Pawn"))))
                    {
                        return(true);
                    }
                }
                if (areValidChessBoxCoordinates(Convert.ToChar(xPawnCheck - 1), yPawnCheck + 1))
                {
                    ChessBox boxToCheck = getChessBoxByCoordinates(Convert.ToChar(xPawnCheck - 1), yPawnCheck + 1);
                    if ((boxToCheck.isFigureOn()) && ((boxToCheck.Figure.isWhite != IswhiteKing) && (boxToCheck.Figure.getFigureType().Equals("Pawn"))))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 5
0
 public void Move(ChessBox start, ChessBox end)
 {
     Move(start.Figure, end.Xcoord, end.Ycoord);
 }
Ejemplo n.º 6
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");
            }
        }
Ejemplo n.º 7
0
        public bool PathBetweenBoxesFree(ChessBox start, ChessBox end)
        {
            if (start.Xcoord == end.Xcoord)
            {
                char sameX = start.Xcoord;

                if (start.Ycoord < end.Ycoord)
                {
                    for (int i = start.Ycoord + 1; i < end.Ycoord; i++)
                    {
                        if (getChessBoxByCoordinates(sameX, i).isFigureOn())
                        {
                            return(false);
                        }
                    }
                    return(true);
                }
                else if (start.Ycoord > end.Ycoord)
                {
                    for (int i = end.Ycoord + 1; i < start.Ycoord; i++)
                    {
                        if (getChessBoxByCoordinates(sameX, i).isFigureOn())
                        {
                            return(false);
                        }
                    }
                    return(true);
                }
            }
            else if (start.Ycoord == end.Ycoord)
            {
                int sameY = start.Ycoord;

                if (start.Xcoord < end.Xcoord)
                {
                    for (int i = start.Xcoord + 1; i < end.Xcoord; i++)
                    {
                        if (getChessBoxByCoordinates(Convert.ToChar(i), sameY).isFigureOn())
                        {
                            return(false);
                        }
                    }
                    return(true);
                }
                else if (start.Xcoord > end.Xcoord)
                {
                    for (int i = end.Xcoord + 1; i < start.Xcoord; i++)
                    {
                        if (getChessBoxByCoordinates(Convert.ToChar(i), sameY).isFigureOn())
                        {
                            return(false);
                        }
                    }
                    return(true);
                }
            }
            else if (((end.Xcoord > end.Ycoord) && (end.Ycoord > start.Ycoord)) && (((end.Xcoord - start.Xcoord) == (end.Ycoord - start.Ycoord))))
            {    /// down right
                char indexX = Convert.ToChar(start.Xcoord + 1);
                int  indexY = start.Ycoord + 1;
                while (indexY < end.Ycoord)
                {
                    if (getChessBoxByCoordinates(indexX, indexY).isFigureOn())
                    {
                        return(false);
                    }
                    indexX++;
                    indexY++;
                }
                return(true);
            }
            else if (((end.Xcoord > start.Xcoord) && (start.Ycoord > end.Ycoord)) && ((end.Xcoord - start.Xcoord) == (start.Ycoord - end.Ycoord)))
            {    /// up right
                char indexX = Convert.ToChar(start.Xcoord + 1);
                int  indexY = start.Ycoord - 1;
                while (indexX < end.Xcoord)
                {
                    if (getChessBoxByCoordinates(indexX, indexY).isFigureOn())
                    {
                        return(false);
                    }
                    indexX++;
                    indexY--;
                }
                return(true);
            }
            else if ((((start.Xcoord > end.Xcoord) && (end.Ycoord > start.Ycoord))) && ((start.Xcoord - end.Xcoord) == (end.Ycoord - start.Ycoord)))
            {    /// down left
                char indexX = Convert.ToChar(start.Xcoord - 1);
                int  indexY = start.Ycoord + 1;
                while (indexY < end.Ycoord)
                {
                    if (getChessBoxByCoordinates(indexX, indexY).isFigureOn())
                    {
                        return(false);
                    }
                    indexX--;
                    indexY++;
                }
                return(true);
            }
            else if ((((start.Xcoord > end.Xcoord) && (start.Ycoord > end.Ycoord))) && ((start.Xcoord - end.Xcoord) == (start.Ycoord - end.Ycoord)))
            {    /// up left
                char indexX = Convert.ToChar(start.Xcoord - 1);
                int  indexY = start.Ycoord - 1;

                while (indexY > end.Ycoord)
                {
                    if (getChessBoxByCoordinates(indexX, indexY).isFigureOn())
                    {
                        return(false);
                    }
                    indexX--;
                    indexY--;
                }
                return(true);
            }

            return(true);
        }