public void MakeAPlay(Position origin, Position destiny) { Piece capturedPiece = ExecuteAMovement(origin, destiny); if (ItIsInCheck(CurrentPlayer)) { UndoTheMovement(origin, destiny, capturedPiece); throw new BoardException("You cannot put yourself in check!"); } Piece piece = Board.Piece(destiny); // #SpecialPlay Promotion if ((piece.Color == Color.White && destiny.Line == 0) || (piece.Color == Color.Black && destiny.Line == 7)) { piece = Board.RemovePiece(destiny); Pieces.Remove(piece); Piece queen = new Queen(Board, piece.Color); Board.InsertPiece(queen, destiny); Pieces.Add(queen); } if (ItIsInCheck(Opponent(CurrentPlayer))) { Check = true; } else { Check = false; } if (TestCheckMate(Opponent(CurrentPlayer))) { Finished = true; } else { Shift++; ChangePlayer(); } //#SpecialPlay EnPassant if (piece is Pawn && (destiny.Line == origin.Line - 2 || destiny.Line == origin.Line + 2)) { VulnerableEnPassant = piece; } else { VulnerableEnPassant = null; } }
public Piece ExecuteAMovement(Position origin, Position destiny) { Piece piece = Board.RemovePiece(origin); piece.IncrementeNumberOfMovements(); Piece capturedPiece = Board.RemovePiece(destiny); Board.InsertPiece(piece, destiny); if (capturedPiece != null) { Captured.Add(capturedPiece); } //#SpecialPlay Castling short if (piece is King && destiny.Column == origin.Column + 2) { Position rookOrigin = new Position(origin.Line, origin.Column + 3); Position rookDestiny = new Position(origin.Line, origin.Column + 1); Piece rook = Board.RemovePiece(rookOrigin); rook.IncrementeNumberOfMovements(); Board.InsertPiece(rook, rookDestiny); } //#SpecialPlay Castling long if (piece is King && destiny.Column == origin.Column - 2) { Position rookOrigin = new Position(origin.Line, origin.Column - 4); Position rookDestiny = new Position(origin.Line, origin.Column - 1); Piece rook = Board.RemovePiece(rookOrigin); rook.IncrementeNumberOfMovements(); Board.InsertPiece(rook, rookDestiny); } //#SpecialPlay EnPassant if (piece is Pawn) { if (origin.Column != destiny.Column && capturedPiece == null) { Position pawnPosition; if (piece.Color == Color.White) { pawnPosition = new Position(destiny.Line + 1, destiny.Column); } else { pawnPosition = new Position(destiny.Line - 1, destiny.Column); } capturedPiece = Board.RemovePiece(pawnPosition); Captured.Add(capturedPiece); } } return(capturedPiece); }
public void UndoTheMovement(Position origin, Position destiny, Piece capturedPice) { Piece piece = Board.RemovePiece(destiny); piece.DecrementNumberOfMovements(); if (capturedPice != null) { Board.InsertPiece(capturedPice, destiny); Captured.Remove(capturedPice); } Board.InsertPiece(piece, origin); //#SpecialPlay Castling short if (piece is King && destiny.Column == origin.Column + 2) { Position rookOrigin = new Position(origin.Line, origin.Column + 3); Position rookDestiny = new Position(origin.Line, origin.Column + 1); Piece rook = Board.RemovePiece(rookDestiny); rook.DecrementNumberOfMovements(); Board.InsertPiece(rook, rookOrigin); } //#SpecialPlay Castling long if (piece is King && destiny.Column == origin.Column - 2) { Position rookOrigin = new Position(origin.Line, origin.Column - 4); Position rookDestiny = new Position(origin.Line, origin.Column - 1); Piece rook = Board.RemovePiece(rookDestiny); rook.DecrementNumberOfMovements(); Board.InsertPiece(rook, rookOrigin); } //#SpecialPlay En Passant if (piece is Pawn) { if (origin.Column != destiny.Column && capturedPice == VulnerableEnPassant) { Piece pawn = Board.RemovePiece(destiny); Position pawnPosition; if (piece.Color == Color.White) { pawnPosition = new Position(3, destiny.Column); } else { pawnPosition = new Position(4, destiny.Column); } Board.InsertPiece(pawn, pawnPosition); } } }
public void InsertNewPiece(char column, int line, Piece piece) { Board.InsertPiece(piece, new ChessPosition(column, line).ToPosition()); Pieces.Add(piece); }