public Piece ChessMove(Position origin, Position destiny) { Piece movedPiece = Board.RemovePiece(origin); movedPiece.IncreaseQtyMovement(); Piece takenPiece = Board.RemovePiece(destiny); Board.AddressPiece(movedPiece, destiny); if (takenPiece != null) { CapturedPieces.Add(takenPiece); } //Special move: Castling ////Short Castling if (movedPiece 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 movedRook = Board.RemovePiece(rookOrigin); movedRook.IncreaseQtyMovement(); Board.AddressPiece(movedRook, rookDestiny); } ////Long Castling if (movedPiece 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 movedRook = Board.RemovePiece(rookOrigin); movedRook.IncreaseQtyMovement(); Board.AddressPiece(movedRook, rookDestiny); } //Special move: En passant if (movedPiece is Pawn) { if (origin.Column != destiny.Column) { Position EnPassantCapture; if (movedPiece.Color == Color.White) { EnPassantCapture = new Position(destiny.Line + 1, destiny.Column); } else { EnPassantCapture = new Position(destiny.Line - 1, destiny.Column); } takenPiece = Board.RemovePiece(EnPassantCapture); CapturedPieces.Add(takenPiece); } } return(takenPiece); }
public void UndoChessMove(Position origin, Position destiny, Piece takenPiece) { Piece returnedPiece = Board.RemovePiece(destiny); returnedPiece.DecreaseQtyMovement(); Board.AddressPiece(returnedPiece, origin); if (takenPiece != null) { Board.AddressPiece(takenPiece, destiny); CapturedPieces.Remove(takenPiece); } //Special move: Castling //Short Castling if (returnedPiece 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 movedRook = Board.RemovePiece(rookDestiny); movedRook.DecreaseQtyMovement(); Board.AddressPiece(movedRook, rookOrigin); } //Long Castling if (returnedPiece 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 movedRook = Board.RemovePiece(rookDestiny); movedRook.DecreaseQtyMovement(); Board.AddressPiece(movedRook, rookOrigin); } //Special move: En passant if (returnedPiece is Pawn) { if (origin.Column != destiny.Column && takenPiece == VulnerableToEnPassant) { Piece EnPassantCapture = Board.RemovePiece(destiny); Position EnPassantReturnPosition; if (returnedPiece.Color == Color.White) { EnPassantReturnPosition = new Position(3, destiny.Column); } else { EnPassantReturnPosition = new Position(4, destiny.Column); } Board.AddressPiece(EnPassantCapture, EnPassantReturnPosition); } } }
public void StartNewPiece(char column, int line, Piece piece) { Board.AddressPiece(piece, new ChessPosition(column, line).TranslateChessToZeroBased()); GamePieces.Add(piece); }
public void MakeMove(Position origin, Position destiny) { Piece takenPiece = ChessMove(origin, destiny); if (IsInCheck(CurrentPlayer)) { UndoChessMove(origin, destiny, takenPiece); throw new BoardExceptions("You can not put your self in check"); } Piece movedPiece = Board.GetPiece(destiny); //Special move: Pawn Promotion if (movedPiece is Pawn) { if ((movedPiece.Color == Color.White && destiny.Line == 0) || (movedPiece.Color == Color.Black && destiny.Line == 7)) { Board.RemovePiece(destiny); GamePieces.Remove(movedPiece); Console.WriteLine("Type the kind of piece you want to promote your pawn to"); Console.Write("(QUEEN / ROOK / KNIGHT / BISHOP): "); string playerChoice = Console.ReadLine(); if (!ScreenController.CheckPlayerChoice(playerChoice)) { throw new BoardExceptions("Not valid option"); } playerChoice = playerChoice.ToLower(); switch (playerChoice) { case "queen": { Piece newPiece = new Queen(movedPiece.Color, Board); Board.AddressPiece(newPiece, destiny); GamePieces.Add(newPiece); break; } case "rook": { Piece newPiece = new Rook(movedPiece.Color, Board); Board.AddressPiece(newPiece, destiny); GamePieces.Add(newPiece); break; } case "knight": { Piece newPiece = new Knight(movedPiece.Color, Board); Board.AddressPiece(newPiece, destiny); GamePieces.Add(newPiece); break; } case "bishop": { Piece newPiece = new Bishop(movedPiece.Color, Board); Board.AddressPiece(newPiece, destiny); GamePieces.Add(newPiece); break; } } } } //EndGame Pawn Promotion if (IsInCheck(EnemyIs(CurrentPlayer))) { PlayerInCheck = true; } else { PlayerInCheck = false; } if (IsInCheckMate(EnemyIs(CurrentPlayer))) { EndGame = true; } else { Turn++; MudaJogador(); } //Special move: en passant if (movedPiece is Pawn && (destiny.Line == origin.Line + 2 || destiny.Line == origin.Line - 2)) { VulnerableToEnPassant = movedPiece; } else { VulnerableToEnPassant = null; } }