public Piece Move(Position origin, Position destination) { Piece piece = Board.RemPiece(origin); piece.IncrementQtyMovement(); Piece capturedPiece = Board.RemPiece(destination); Board.AddPiece(piece, destination); if (capturedPiece != null) { CapturedPieces.Add(capturedPiece); } // Castling if (piece is King && destination.Col == origin.Col + 2) { Position originRook = new Position(origin.Row, origin.Col + 3); Position destinationRook = new Position(origin.Row, origin.Col + 1); Piece rook = Board.RemPiece(originRook); rook.IncrementQtyMovement(); Board.AddPiece(rook, destinationRook); } if (piece is King && destination.Col == origin.Col - 2) { Position originRook = new Position(origin.Row, origin.Col - 4); Position destinationRook = new Position(origin.Row, origin.Col - 1); Piece rook = Board.RemPiece(originRook); rook.IncrementQtyMovement(); Board.AddPiece(rook, destinationRook); } // En Passant if (piece is Pawn) { if (origin.Col != destination.Col && capturedPiece == null) { Position pawnPos; if (piece.Color == Color.White) { pawnPos = new Position(destination.Row + 1, destination.Col); } else { pawnPos = new Position(destination.Row - 1, destination.Col); } capturedPiece = Board.RemPiece(pawnPos); CapturedPieces.Add(capturedPiece); } } return(capturedPiece); }
public void UndoMove(Position origin, Position destination, Piece capturedPiece) { Piece piece = Board.RemPiece(destination); piece.DecrementQtyMovement(); if (capturedPiece != null) { Board.AddPiece(capturedPiece, destination); CapturedPieces.Remove(capturedPiece); } Board.AddPiece(piece, origin); // Castling if (piece is King && destination.Col == origin.Col + 2) { Position originRook = new Position(origin.Row, origin.Col + 3); Position destinationRook = new Position(origin.Row, origin.Col + 1); Piece rook = Board.RemPiece(destinationRook); rook.DecrementQtyMovement(); Board.AddPiece(rook, originRook); } if (piece is King && destination.Col == origin.Col - 2) { Position originRook = new Position(origin.Row, origin.Col - 4); Position destinationRook = new Position(origin.Row, origin.Col - 1); Piece rook = Board.RemPiece(destinationRook); rook.DecrementQtyMovement(); Board.AddPiece(rook, originRook); } // En Passant if (piece is Pawn) { if (origin.Col != destination.Col && capturedPiece == VulnerableEnPassant) { Piece pawn = Board.RemPiece(destination); Position pawnPos; if (piece.Color == Color.White) { pawnPos = new Position(3, destination.Col); } else { pawnPos = new Position(4, destination.Col); } Board.AddPiece(pawn, pawnPos); } } }
public void ExecuteRound(Position origin, Position destination) { Piece capturedPiece = Move(origin, destination); if (IsInCheck(CurrentPlayer)) { UndoMove(origin, destination, capturedPiece); throw new BoardException("You cannot put yourself in Check!"); } Piece piece = Board.GetPiece(destination); // Pawn Promotion if (piece is Pawn) { if ((piece.Color == Color.White && destination.Row == 0) || (piece.Color == Color.Black && destination.Row == 7)) { piece = Board.RemPiece(destination); Pieces.Remove(piece); piece = new Queen(Board, piece.Color); Board.AddPiece(piece, destination); Pieces.Add(piece); } } Check = IsInCheck(OpponentColor(CurrentPlayer)); if (IsInCheckMate(OpponentColor(CurrentPlayer))) { Finished = true; } else { ChangePlayer(); Round++; } // En Passant if (piece is Pawn && (destination.Row == origin.Row - 2 || destination.Row == origin.Row + 2)) { VulnerableEnPassant = piece; } else { VulnerableEnPassant = null; } }