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"); } }
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 }