public static void PrintChessboard(Chessboard chessboard, bool[,] possiblePositions) { ConsoleColor originBackground = Console.BackgroundColor; ConsoleColor changedBackground = ConsoleColor.DarkGray; for (int i = 0; i < chessboard.Rows; i++) { Console.Write(8 - i + " "); for (int j = 0; j < chessboard.Columns; j++) { if (possiblePositions[i, j]) { Console.BackgroundColor = changedBackground; } else { Console.BackgroundColor = originBackground; } PrintChessPiece(chessboard.ChessPiece(i, j)); Console.BackgroundColor = originBackground; } Console.WriteLine(); } Console.WriteLine(); Console.WriteLine(" A B C D E F G H"); Console.BackgroundColor = originBackground; }
public void DestinyPositionValidate(Position origin, Position destiny) { if (!Chessboard.ChessPiece(origin).PossibleMovement(destiny)) { throw new ChessboardException("Invalid destiny position!"); } }
public override bool[,] PossibleMovements() { bool[,] match = new bool[Chessboard.Rows, Chessboard.Columns]; Position position = new Position(0, 0); // Up-Left position.SetValues(Position.Row - 1, Position.Column - 1); while (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; if (Chessboard.ChessPiece(position) != null && Chessboard.ChessPiece(position).Color != Color) { break; } position.SetValues(position.Row - 1, position.Column - 1); } // Up-Right position.SetValues(Position.Row - 1, Position.Column + 1); while (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; if (Chessboard.ChessPiece(position) != null && Chessboard.ChessPiece(position).Color != Color) { break; } position.SetValues(position.Row - 1, position.Column + 1); } // Down-Right position.SetValues(Position.Row + 1, Position.Column + 1); while (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; if (Chessboard.ChessPiece(position) != null && Chessboard.ChessPiece(position).Color != Color) { break; } position.SetValues(position.Row + 1, position.Column + 1); } // Down-Left position.SetValues(Position.Row + 1, Position.Column - 1); while (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; if (Chessboard.ChessPiece(position) != null && Chessboard.ChessPiece(position).Color != Color) { break; } position.SetValues(position.Row + 1, position.Column - 1); } return(match); }
public void DoTheMove(Position origin, Position destiny) { ChessPiece capturedPiece = MakeTheMove(origin, destiny); if (IsOnCheck(CurrentPlayer)) { UndoMove(origin, destiny, capturedPiece); throw new ChessboardException("You cannot put yourself on check!"); } ChessPiece cp = Chessboard.ChessPiece(destiny); // #SpecialMove Promotion if (cp is Pawn) { if ((cp.Color == Color.White && destiny.Row == 0) || (cp.Color == Color.Black && destiny.Row == 7)) { cp = Chessboard.RemoveChessPiece(destiny); ChessPieces.Remove(cp); ChessPiece queen = new Queen(cp.Color, Chessboard); Chessboard.ChessPiecePosition(queen, destiny); ChessPieces.Add(queen); } } if (IsOnCheck(Opponent(CurrentPlayer))) { Check = true; } else { Check = false; } if (IsOnCheckmate(Opponent(CurrentPlayer))) { GameOver = true; } else { Turn++; ChangePlayer(); } // #SpecialMove En Passant if (cp is Pawn && (destiny.Row == origin.Row - 2 || destiny.Row == origin.Row + 2)) { VulnerableEnPassant = cp; } else { VulnerableEnPassant = null; } }
public static void PrintChessboard(Chessboard chessboard) { for (int i = 0; i < chessboard.Rows; i++) { Console.Write(8 - i + " "); for (int j = 0; j < chessboard.Columns; j++) { PrintChessPiece(chessboard.ChessPiece(i, j)); } Console.WriteLine(); } Console.WriteLine(); Console.WriteLine(" A B C D E F G H"); }
public void OriginPositionValidate(Position position) { if (Chessboard.ChessPiece(position) == null) { throw new ChessboardException("There is no chess piece on this position!"); } if (CurrentPlayer != Chessboard.ChessPiece(position).Color) { throw new ChessboardException("The origin chess piece is not yours!"); } if (!Chessboard.ChessPiece(position).IsTherePossibleMovements()) { throw new ChessboardException("There is no possible movements to this origin chess piece!"); } }
public override bool[,] PossibleMovements() { bool[,] match = new bool[Chessboard.Rows, Chessboard.Columns]; Position position = new Position(0, 0); // Up position.SetValues(Position.Row - 1, Position.Column); if (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; } // Diagonal Up-Right position.SetValues(Position.Row - 1, Position.Column + 1); if (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; } // Right position.SetValues(Position.Row, Position.Column + 1); if (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; } // Diagonal Down-Right position.SetValues(Position.Row + 1, Position.Column + 1); if (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; } // Down position.SetValues(Position.Row + 1, Position.Column); if (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; } // Diagonal Down-Left position.SetValues(Position.Row + 1, Position.Column - 1); if (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; } // Left position.SetValues(Position.Row, Position.Column - 1); if (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; } // Diagonal Up-Left position.SetValues(Position.Row - 1, Position.Column - 1); if (Chessboard.ValidPosition(position) && CanMove(position)) { match[position.Row, position.Column] = true; } // #SpecialMove Castle if (QuantityMovements == 0 && !Game.Check) { // #SpecialMove Small Castle Position posR1 = new Position(Position.Row, Position.Column + 3); if (TestRookToCastle(posR1)) { Position p1 = new Position(Position.Row, Position.Column + 1); Position p2 = new Position(Position.Row, Position.Column + 2); if (Chessboard.ChessPiece(p1) == null && Chessboard.ChessPiece(p2) == null) { match[Position.Row, Position.Column + 2] = true; } } // #SpecialMove Big Castle Position posR2 = new Position(Position.Row, Position.Column - 4); if (TestRookToCastle(posR2)) { Position p1 = new Position(Position.Row, Position.Column - 1); Position p2 = new Position(Position.Row, Position.Column - 2); Position p3 = new Position(Position.Row, Position.Column - 3); if (Chessboard.ChessPiece(p1) == null && Chessboard.ChessPiece(p2) == null && Chessboard.ChessPiece(p3) == null) { match[Position.Row, Position.Column - 2] = true; } } } return(match); }
private bool TestRookToCastle(Position position) { ChessPiece cp = Chessboard.ChessPiece(position); return(cp != null && cp is Rook && cp.Color == Color && cp.QuantityMovements == 0); }
private bool CanMove(Position position) { ChessPiece cp = Chessboard.ChessPiece(position); return(cp == null || cp.Color != Color); }
public override bool[,] PossibleMovements() { bool[,] match = new bool[Chessboard.Rows, Chessboard.Columns]; Position position = new Position(0, 0); if (Color == Color.White) { position.SetValues(Position.Row - 1, Position.Column); if (Chessboard.ValidPosition(position) && Free(position)) { match[position.Row, position.Column] = true; } position.SetValues(Position.Row - 2, Position.Column); if (Chessboard.ValidPosition(position) && Free(position) && QuantityMovements == 0) { match[position.Row, position.Column] = true; } position.SetValues(Position.Row - 1, Position.Column - 1); if (Chessboard.ValidPosition(position) && IsThereEnemy(position)) { match[position.Row, position.Column] = true; } position.SetValues(Position.Row - 1, Position.Column + 1); if (Chessboard.ValidPosition(position) && IsThereEnemy(position)) { match[position.Row, position.Column] = true; } // #SpecialMove En Passant if (Position.Row == 3) { Position left = new Position(Position.Row, Position.Column - 1); if (Chessboard.ValidPosition(left) && IsThereEnemy(left) && Chessboard.ChessPiece(left) == ChessMatch.VulnerableEnPassant) { match[left.Row - 1, left.Column] = true; } Position right = new Position(Position.Row, Position.Column + 1); if (Chessboard.ValidPosition(right) && IsThereEnemy(right) && Chessboard.ChessPiece(right) == ChessMatch.VulnerableEnPassant) { match[right.Row - 1, right.Column] = true; } } } else { position.SetValues(Position.Row + 1, Position.Column); if (Chessboard.ValidPosition(position) && Free(position)) { match[position.Row, position.Column] = true; } position.SetValues(Position.Row + 2, Position.Column); if (Chessboard.ValidPosition(position) && Free(position) && QuantityMovements == 0) { match[position.Row, position.Column] = true; } position.SetValues(Position.Row + 1, Position.Column - 1); if (Chessboard.ValidPosition(position) && IsThereEnemy(position)) { match[position.Row, position.Column] = true; } position.SetValues(Position.Row + 1, Position.Column + 1); if (Chessboard.ValidPosition(position) && IsThereEnemy(position)) { match[position.Row, position.Column] = true; } // #SpecialMove En Passant if (Position.Row == 4) { Position left = new Position(Position.Row, Position.Column - 1); if (Chessboard.ValidPosition(left) && IsThereEnemy(left) && Chessboard.ChessPiece(left) == ChessMatch.VulnerableEnPassant) { match[left.Row + 1, left.Column] = true; } Position right = new Position(Position.Row, Position.Column + 1); if (Chessboard.ValidPosition(right) && IsThereEnemy(right) && Chessboard.ChessPiece(right) == ChessMatch.VulnerableEnPassant) { match[right.Row + 1, right.Column] = true; } } } return(match); }
private bool Free(Position position) { return(Chessboard.ChessPiece(position) == null); }
private bool IsThereEnemy(Position position) { ChessPiece cp = Chessboard.ChessPiece(position); return(cp != null && cp.Color != Color); }