public static string GetString(ChessRank rank, ChessOwner player, ChessIconFormat format = ChessIconFormat.Text) { bool isBlack = player == ChessOwner.Black; bool isEmote = format == ChessIconFormat.Emote; string text = rank switch { ChessRank.Pawn when isEmote => isBlack ? "♙" : "♟", ChessRank.Pawn => "P", ChessRank.Knight when isEmote => isBlack ? "♘" : "♞", ChessRank.Knight => "N", ChessRank.Bishop when isEmote => isBlack ? "♗" : "♝", ChessRank.Bishop => "B", ChessRank.Rook when isEmote => isBlack ? "♖" : "♜", ChessRank.Rook => "R", ChessRank.Queen when isEmote => isBlack ? "♕" : "♛", ChessRank.Queen => "Q", ChessRank.King when isEmote => isBlack ? "♔" : "♚", ChessRank.King => "K", _ => throw new ArgumentException("Unknown rank") }; if (isBlack && !isEmote) { text = text.ToLower(); } return(text); }
public static bool TryParse(string input, out ChessRank piece) { piece = 0; if (input.EqualsAny(StringComparison.OrdinalIgnoreCase, "p", "pawn")) { piece = ChessRank.Pawn; return(true); } if (input.EqualsAny(StringComparison.OrdinalIgnoreCase, "n", "knight")) { piece = ChessRank.Knight; return(true); } if (input.EqualsAny(StringComparison.OrdinalIgnoreCase, "b", "bishop")) { piece = ChessRank.Bishop; return(true); } if (input.EqualsAny(StringComparison.OrdinalIgnoreCase, "r", "rook")) { piece = ChessRank.Rook; return(true); } if (input.EqualsAny(StringComparison.OrdinalIgnoreCase, "q", "queen")) { piece = ChessRank.Queen; return(true); } if (input.EqualsAny(StringComparison.OrdinalIgnoreCase, "k", "king")) { piece = ChessRank.King; return(true); } return(false); }
// 0 to 7 // file = y // rank = x private List <Coordinate> GetBaseMoves(ChessOwner owner, ChessRank piece, int rank, int file) { var moves = new List <Coordinate>(); switch (piece) { case ChessRank.King: moves.AddRange(new List <Coordinate> { new Coordinate(rank + 1, file), new Coordinate(rank - 1, file), new Coordinate(rank, file + 1), new Coordinate(rank, file - 1), new Coordinate(rank + 1, file + 1), new Coordinate(rank - 1, file + 1), new Coordinate(rank + 1, file - 1), new Coordinate(rank - 1, file - 1) }); break; case ChessRank.Knight: moves.AddRange(new List <Coordinate> { new Coordinate(rank + 1, file - 2), new Coordinate(rank - 1, file - 2), new Coordinate(rank + 1, file + 2), new Coordinate(rank - 1, file + 2), new Coordinate(rank + 2, file - 1), new Coordinate(rank - 2, file - 1), new Coordinate(rank + 2, file + 1), new Coordinate(rank - 2, file + 1) }); break; case ChessRank.Rook: MoveWest(ref moves, rank, file); MoveEast(ref moves, rank, file); MoveNorth(ref moves, rank, file); MoveSouth(ref moves, rank, file); break; case ChessRank.Bishop: MoveNorthWest(ref moves, rank, file); MoveNorthEast(ref moves, rank, file); MoveSouthWest(ref moves, rank, file); MoveSouthEast(ref moves, rank, file); break; case ChessRank.Queen: MoveWest(ref moves, rank, file); MoveEast(ref moves, rank, file); MoveNorth(ref moves, rank, file); MoveSouth(ref moves, rank, file); MoveNorthWest(ref moves, rank, file); MoveNorthEast(ref moves, rank, file); MoveSouthWest(ref moves, rank, file); MoveSouthEast(ref moves, rank, file); break; case ChessRank.Pawn: if (owner == ChessOwner.Black) { moves.Add(new Coordinate(rank, file + 1)); if (file == 1) { moves.Add(new Coordinate(rank, file + 2)); } } else { moves.Add(new Coordinate(rank, file - 1)); if (file == 6) { moves.Add(new Coordinate(rank, file - 2)); } } break; } return(moves.Where(Exclude).ToList()); }
/// <summary> /// Returns an instance of this class initialized with the coordinates of the square. /// </summary> /// <param name="file"></param> /// <param name="rank"></param> public ChessSquare(ChessFile file, ChessRank rank) { this.File = file; this.Rank = rank; }