private void UndoMove(Position source, Position target, Piece capturedPiece) { Piece p = Board.RemovePiece(target); p.DecreaseMoveCount(); if (capturedPiece != null) { Board.PlacePiece(capturedPiece, target); Captured.Remove(capturedPiece); } Board.PlacePiece(p, source); // #Special move - Castling - Kingside Rook if (p is King && target.Column == source.Column + 2) { Position rookSource = new Position(source.Row, source.Column + 3); Position rookTarget = new Position(source.Row, source.Column + 1); Piece rook = Board.RemovePiece(rookTarget); rook.DecreaseMoveCount(); Board.PlacePiece(rook, rookSource); } // #Special move - Castling - Queenside Rook if (p is King && target.Column == source.Column - 2) { Position rookSource = new Position(source.Row, source.Column - 4); Position rookTarget = new Position(source.Row, source.Column - 1); Piece rook = Board.RemovePiece(rookTarget); rook.DecreaseMoveCount(); Board.PlacePiece(rook, rookSource); } // #Special move - En Passant if (p is Pawn) { if (source.Column != target.Column && capturedPiece == EnPassantVulnerable) { Piece pawn = Board.RemovePiece(target); Position pawnPosition; if (p.Color == Color.White) { pawnPosition = new Position(3, target.Column); } else { pawnPosition = new Position(4, target.Column); } Board.PlacePiece(pawn, pawnPosition); } } }
/// <summary> /// Undoes the movement /// </summary> /// <param name="origin"></param> /// <param name="destination"></param> /// <param name="capturedPiece"></param> public void UndoMovement(Position origin, Position destination, Piece capturedPiece) { Piece p = Board.RemovePiece(destination); p.DecrementMovements(); if (capturedPiece != null) { Board.PlacePiece(capturedPiece, destination); Captured.Remove(capturedPiece); } Board.PlacePiece(p, origin); // SPECIAL PLAY SMALL CASTLING if (p is King && destination.Column == origin.Column + 2) { Position rookOrigin = new Position(origin.Line, origin.Column + 3); Position rookDest = new Position(origin.Line, origin.Column + 1); Piece rook = Board.RemovePiece(rookDest); rook.IncrementMovements(); Board.PlacePiece(rook, rookOrigin); } // SPECIAL PLAY BIG CASTLING if (p is King && destination.Column == origin.Column - 2) { Position rookOrigin = new Position(origin.Line, origin.Column - 4); Position rookDest = new Position(origin.Line, origin.Column - 1); Piece rook = Board.RemovePiece(rookDest); rook.IncrementMovements(); Board.PlacePiece(rook, rookOrigin); } // SPECIAL PLAY EN PASSANT if (p is Pawn) { if (origin.Column != destination.Column && capturedPiece == VulnerableEnPassant) { Piece pawn = Board.RemovePiece(destination); Position pawnPos; if (p.Color == Color.White) { pawnPos = new Position(3, destination.Column); } else { pawnPos = new Position(4, destination.Column); } Board.PlacePiece(pawn, pawnPos); } } }