/// <summary> /// Checks if last play was a castling and move the respective tower /// </summary> /// <param name="king">King</param> private void ExecuteCastling(Piece king) { Piece tower = null; Position origin = null; Position target = null; if (king.Position.Column == 6) { tower = Board.Piece(king.Position.Row, king.Position.Column + 1); if (tower.NumberOfMovements == 0) { origin = tower.Position; target = new Position(king.Position.Row, king.Position.Column - 1); Board.RemovePiece(origin); Board.PutPiece(tower, target); StringBuilder text = new StringBuilder(); text.AppendLine("Execute castling piece"); text.AppendLine(string.Concat("Piece: ", tower.GetType())); text.AppendLine(string.Concat("Color: ", tower.Color.ToString())); text.AppendLine(string.Concat("Origin ChessPosition: ", origin.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", origin.Column, ", ", origin.Row)); text.AppendLine(string.Concat("Target ChessPosition: ", target.ToChessPosition().ToString(), " Target CanvasPosition(Column, Row): ", target.Column, ", ", target.Row)); text.AppendLine(string.Concat("Turn: ", TotalMoves)); text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString())); WriteLog.ChecksLogs(text); tower.IncrementMovements(); } } else if (king.Position.Column == 2) { tower = Board.Piece(king.Position.Row, king.Position.Column - 2); if (tower.NumberOfMovements == 0) { origin = tower.Position; target = new Position(king.Position.Row, king.Position.Column + 1); Board.RemovePiece(origin); Board.PutPiece(tower, target); StringBuilder text = new StringBuilder(); text.AppendLine("Execute castling piece"); text.AppendLine(string.Concat("Piece: ", tower.GetType())); text.AppendLine(string.Concat("Color: ", tower.Color.ToString())); text.AppendLine(string.Concat("Origin ChessPosition: ", origin.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", origin.Column, ", ", origin.Row)); text.AppendLine(string.Concat("Target ChessPosition: ", target.ToChessPosition().ToString(), " Target CanvasPosition(Column, Row): ", target.Column, ", ", target.Row)); text.AppendLine(string.Concat("Turn: ", TotalMoves)); text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString())); WriteLog.ChecksLogs(text); tower.IncrementMovements(); } } if (KingIsCheck(king.Color) && origin != null) { UndoMovement(origin, target, null); } }
public void PerformMovement(Position origin, Position destination) { Piece piece = Board.RemovePiece(origin); piece.IncreaseNumberOfMovies(); Piece capturedPiece = Board.RemovePiece(destination); Board.PutPiece(piece, destination); }
/// <summary> /// Put piece /// </summary> /// <param name="piece">Piece</param> /// <param name="column">Column</param> /// <param name="line">Row</param> private void PutPiece(Piece piece, char column, int line) { Board.PutPiece(piece, new ChessPosition(column, line).ToPosition()); Pieces.Add(piece); StringBuilder text = new StringBuilder(); text.AppendLine("Put piece"); text.AppendLine(string.Concat("Piece: ", piece.GetType())); text.AppendLine(string.Concat("Color: ", piece.Color.ToString())); text.AppendLine(string.Concat("ChessPosition: ", column, line, " CanvasPosition(Column, Row): ", piece.Position.Column, piece.Position.Row)); text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString())); WriteLog.ChecksLogs(text); }
private void PutPiecesOnTheBoard() { Board.PutPiece(new Rook(Board, Color.White), new PositionChess('c', 1).ToPosition()); Board.PutPiece(new Rook(Board, Color.White), new PositionChess('e', 1).ToPosition()); Board.PutPiece(new King(Board, Color.White), new PositionChess('d', 1).ToPosition()); Board.PutPiece(new Rook(Board, Color.White), new PositionChess('c', 2).ToPosition()); Board.PutPiece(new Rook(Board, Color.White), new PositionChess('d', 2).ToPosition()); Board.PutPiece(new Rook(Board, Color.White), new PositionChess('e', 2).ToPosition()); Board.PutPiece(new Rook(Board, Color.Black), new PositionChess('c', 8).ToPosition()); Board.PutPiece(new Rook(Board, Color.Black), new PositionChess('e', 8).ToPosition()); Board.PutPiece(new King(Board, Color.Black), new PositionChess('d', 8).ToPosition()); Board.PutPiece(new Rook(Board, Color.Black), new PositionChess('c', 7).ToPosition()); Board.PutPiece(new Rook(Board, Color.Black), new PositionChess('d', 7).ToPosition()); Board.PutPiece(new Rook(Board, Color.Black), new PositionChess('e', 7).ToPosition()); }
/// <summary> /// Execute move of chess piece /// </summary> /// <param name="origin">Origin of piece</param> /// <param name="target">Target position</param> public Piece ExecuteMove(Position origin, Position target) { Piece piece = Board.RemovePiece(origin); Piece capturedPiece = Board.RemovePiece(target); Board.PutPiece(piece, target); // Log StringBuilder text = new StringBuilder(); text.AppendLine("Move piece"); text.AppendLine(string.Concat("Piece: ", piece.GetType())); text.AppendLine(string.Concat("Color: ", piece.Color.ToString())); text.AppendLine(string.Concat("Origin ChessPosition: ", origin.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", origin.Column, ", ", origin.Row)); text.AppendLine(string.Concat("Target ChessPosition: ", target.ToChessPosition().ToString(), " Target CanvasPosition(Column, Row): ", target.Column, ", ", target.Row)); text.AppendLine(string.Concat("Turn: ", TotalMoves)); text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString())); piece.IncrementMovements(); if (capturedPiece != null) { CapturedPieces.Add(capturedPiece); text.AppendLine(string.Concat("Captured: ", capturedPiece.GetType())); } WriteLog.ChecksLogs(text); if (Board.Piece(target) is King && ((target.Column + 2 == origin.Column) || (target.Column - 2 == origin.Column))) { ExecuteCastling(Board.Piece(target)); } if (Board.Piece(target) != null && Board.Piece(target) is Pawn && origin.Column != target.Column && capturedPiece == null) { capturedPiece = ExecuteEnPassant(Board.Piece(target), origin); } if (capturedPiece != null) { CapturedPieces.Add(capturedPiece); } return(capturedPiece); }
/// <summary> /// Undo the movement itself that would put in check /// </summary> /// <param name="piece">Current piece</param> /// <param name="capturedPiece">Captured piece</param> /// <param name="origin">Origin of current piece</param> private void UndoMovement(Position origin, Position target, Piece captured) { Piece piece = Board.RemovePiece(target); piece.DecrementMovements(); StringBuilder text = new StringBuilder(); Position originAux = null; if (captured != null) { text.AppendLine("Undo captured"); text.AppendLine(string.Concat("Captured Piece: ", captured.GetType())); text.AppendLine(string.Concat("Color: ", captured.Color.ToString())); if (captured != EnableEnPassant) { Board.PutPiece(captured, target); text.AppendLine(string.Concat("Origin ChessPosition: ", origin.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", origin.Column, ", ", origin.Row)); CapturedPieces.Remove(captured); } else { originAux = new Position(origin.Row, target.Column); Board.PutPiece(captured, originAux); CapturedPieces.Remove(captured); text.AppendLine(string.Concat("Origin ChessPosition: ", originAux.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", originAux.Column, ", ", originAux.Row)); } text.AppendLine(string.Concat("Turn: ", TotalMoves)); text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString())); text.AppendLine(); } Board.PutPiece(piece, origin); text.AppendLine(string.Concat("Undo Piece: ", captured.GetType())); text.AppendLine(string.Concat("Color: ", captured.Color.ToString())); text.AppendLine(string.Concat("Origin ChessPosition: ", origin.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", origin.Column, ", ", origin.Row)); text.AppendLine(string.Concat("Turn: ", TotalMoves)); text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString())); WriteLog.ChecksLogs(text); }
/// <summary> /// Checks is pawn can promotion /// </summary> /// <param name="color">To checks if a black or white</param> public void MakePromotion(Color color) { if (color is Color.Black) { if (Position.Row == 7) { Console.WriteLine("This pawn was promote, you can promotion a new piece."); Console.WriteLine("Choose any T: Tower, B: Bishop, N: Knight and Q: Queen: "); string choose = Console.ReadLine().ToUpper(); Promotions promotion = 0; if (Enum.TryParse <Promotions>(choose, out promotion)) { Position pos = Position; Board.RemovePiece(Position); switch (promotion) { case Promotions.T: Board.PutPiece(new Rook(Board, color), pos); break; case Promotions.B: Board.PutPiece(new Bishop(Board, color), pos); break; case Promotions.N: Board.PutPiece(new Knight(Board, color), pos); break; case Promotions.Q: Board.PutPiece(new Queen(Board, color), pos); break; } } else { throw new BoardException("Invalid promotion"); } } } else if (color is Color.White) { if (Position.Row == 0) { Console.WriteLine("This pawn was promote, you can promotion a new piece."); Console.WriteLine("Choose any T: Tower, B: Bishop, N: Knight and Q: Queen: "); string choose = Console.ReadLine().ToUpper(); Promotions promotion; if (Enum.TryParse(choose, out promotion)) { Position pos = Position; Board.RemovePiece(Position); StringBuilder text = new StringBuilder(); switch (promotion) { case Promotions.T: Rook rook = new Rook(Board, color); Board.PutPiece(rook, pos); text.AppendLine(string.Concat("Promote pawn to: ", rook.GetType())); text.AppendLine(string.Concat("ChessPosition: ", rook.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", rook.Position.Column, ", ", rook.Position.Row)); break; case Promotions.B: Bishop bishop = new Bishop(Board, color); Board.PutPiece(bishop, pos); text.AppendLine(string.Concat("Promote pawn to: ", bishop.GetType())); text.AppendLine(string.Concat("ChessPosition: ", bishop.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", bishop.Position.Column, ", ", bishop.Position.Row)); break; case Promotions.N: Knight knight = new Knight(Board, color); Board.PutPiece(knight, pos); text.AppendLine(string.Concat("Promote pawn to: ", knight.GetType())); text.AppendLine(string.Concat("ChessPosition: ", knight.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", knight.Position.Column, ", ", knight.Position.Row)); break; case Promotions.Q: Queen queen = new Queen(Board, color); Board.PutPiece(queen, pos); text.AppendLine(string.Concat("Promote pawn to: ", queen.GetType())); text.AppendLine(string.Concat("ChessPosition: ", queen.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", queen.Position.Column, ", ", queen.Position.Row)); break; } text.AppendLine(string.Concat("Color: ", color)); WriteLog.ChecksLogs(text); } else { throw new BoardException("Invalid promotion"); } } } }