1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 21:27:34 +00:00

LibChess: Make piece_for_char_promotion() more generally useful

- Rename to make it clear it's not just for promotion
- Understand 'p' for pawns
- Take a char parameter instead of StringView since it's always 1 char
This commit is contained in:
Sam Atkins 2023-04-24 12:12:14 +01:00 committed by Andreas Kling
parent b111782f18
commit c73c697f94
2 changed files with 16 additions and 14 deletions

View file

@ -37,21 +37,22 @@ Optional<char> char_for_piece(Type type, Notation notation)
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
Chess::Type piece_for_char_promotion(StringView str) Type piece_from_char(char c)
{ {
DeprecatedString string = DeprecatedString(str).to_lowercase(); switch (to_ascii_lowercase(c)) {
if (string == "") case 'n':
return Type::None;
if (string == "n")
return Type::Knight; return Type::Knight;
if (string == "b") case 'b':
return Type::Bishop; return Type::Bishop;
if (string == "r") case 'r':
return Type::Rook; return Type::Rook;
if (string == "q") case 'q':
return Type::Queen; return Type::Queen;
if (string == "k") case 'k':
return Type::King; return Type::King;
case 'p':
return Type::Pawn;
}
return Type::None; return Type::None;
} }
@ -93,7 +94,7 @@ DeprecatedString Square::to_algebraic() const
Move::Move(StringView long_algebraic) Move::Move(StringView long_algebraic)
: from(long_algebraic.substring_view(0, 2)) : from(long_algebraic.substring_view(0, 2))
, to(long_algebraic.substring_view(2, 2)) , to(long_algebraic.substring_view(2, 2))
, promote_to(piece_for_char_promotion((long_algebraic.length() >= 5) ? long_algebraic.substring_view(4, 1) : ""sv)) , promote_to((long_algebraic.length() >= 5) ? piece_from_char(long_algebraic[4]) : Type::None)
{ {
} }
@ -130,8 +131,9 @@ Move Move::from_algebraic(StringView algebraic, const Color turn, Board const& b
} }
if (algebraic.contains('=')) { if (algebraic.contains('=')) {
move.promote_to = piece_for_char_promotion(move_string.split('=').at(1).substring(0, 1)); auto parts = move_string.split_view('=');
move_string = move_string.split('=').at(0); move.promote_to = piece_from_char(parts[1][0]);
move_string = parts[0];
} }
move.to = Square(move_string.substring(move_string.length() - 2, 2)); move.to = Square(move_string.substring(move_string.length() - 2, 2));
@ -145,7 +147,7 @@ Move Move::from_algebraic(StringView algebraic, const Color turn, Board const& b
if (move_string.is_empty() || move_string.characters()[0] >= 'a') { if (move_string.is_empty() || move_string.characters()[0] >= 'a') {
move.piece = Piece(turn, Type::Pawn); move.piece = Piece(turn, Type::Pawn);
} else { } else {
move.piece = Piece(turn, piece_for_char_promotion(move_string.substring(0, 1))); move.piece = Piece(turn, piece_from_char(move_string[0]));
move_string = move_string.substring(1, move_string.length() - 1); move_string = move_string.substring(1, move_string.length() - 1);
} }

View file

@ -30,7 +30,7 @@ enum class Notation {
FEN, FEN,
}; };
Optional<char> char_for_piece(Type, Notation); Optional<char> char_for_piece(Type, Notation);
Chess::Type piece_for_char_promotion(StringView str); Type piece_from_char(char);
enum class Color : u8 { enum class Color : u8 {
White, White,