public void ValidateDestinationPosition(Position origin, Position destination) { if (!Gboard.GetPiece(origin).CanMoveTo(destination)) { throw new GameBoardException("Impossible movement."); } }
public void ValidateOriginPosition(Position pos) { if (Gboard.GetPiece(pos) == null) { throw new GameBoardException("There is no piece in the specified location."); } if (Gboard.GetPiece(pos).Color != currentPlayer) { throw new GameBoardException("That piece is not yours."); } if (!Gboard.GetPiece(pos).ExistsPossibleMovement()) { throw new GameBoardException("That piece is blocked and there is no possible movements."); } }
// Here we make the tests public void DoTheMove(Position origin, Position destination) { Piece capturedPiece = MovePiece(origin, destination); // Current player getting itself in check if (Check == true) { if (InCheck(currentPlayer)) { UndoMovement(origin, destination, capturedPiece); throw new GameBoardException("You can not let your own king in check, try to save him!"); } } else { if (InCheck(currentPlayer)) { UndoMovement(origin, destination, capturedPiece); throw new GameBoardException("You must not put your own king in check!"); } } Piece p = Gboard.GetPiece(destination); // #SpecialMovement : Pawn promotion if (p is Pawn) { if (p.Color == Color.White && destination.Line == 0 || p.Color == Color.Black && destination.Line == 7) { p = Gboard.LiftPiece(destination); PiecesInGame.Remove(p); Piece queen = new Queen(Gboard, p.Color); Gboard.PutPiece(queen, destination); PiecesInGame.Add(queen); } } // Current player cheking his adversary if (InCheck(AdversaryOf(currentPlayer))) { Check = true; } else { Check = false; } if (CheckMate(AdversaryOf(currentPlayer))) { MatchEnded = true; } else { turn++; ChangePlayer(); } // #SpecialMovement : En passant if (p is Pawn && (destination.Line == origin.Line - 2 || destination.Line == origin.Line + 2)) { VulnerableEnPassant = p; } else { VulnerableEnPassant = null; } }