diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2023-04-24 12:12:14 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-04-24 20:58:35 +0200 |
commit | c73c697f9425cba1e530fc49a5d0502fad9b77c0 (patch) | |
tree | fac8a4857052ebd3da19aac6ae23ac7fd8f06cad | |
parent | b111782f18aa13306d40dd380a54acd138025bd9 (diff) | |
download | serenity-c73c697f9425cba1e530fc49a5d0502fad9b77c0.zip |
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
-rw-r--r-- | Userland/Libraries/LibChess/Chess.cpp | 28 | ||||
-rw-r--r-- | Userland/Libraries/LibChess/Chess.h | 2 |
2 files changed, 16 insertions, 14 deletions
diff --git a/Userland/Libraries/LibChess/Chess.cpp b/Userland/Libraries/LibChess/Chess.cpp index 529ef023a0..74ee1c0974 100644 --- a/Userland/Libraries/LibChess/Chess.cpp +++ b/Userland/Libraries/LibChess/Chess.cpp @@ -37,21 +37,22 @@ Optional<char> char_for_piece(Type type, Notation notation) VERIFY_NOT_REACHED(); } -Chess::Type piece_for_char_promotion(StringView str) +Type piece_from_char(char c) { - DeprecatedString string = DeprecatedString(str).to_lowercase(); - if (string == "") - return Type::None; - if (string == "n") + switch (to_ascii_lowercase(c)) { + case 'n': return Type::Knight; - if (string == "b") + case 'b': return Type::Bishop; - if (string == "r") + case 'r': return Type::Rook; - if (string == "q") + case 'q': return Type::Queen; - if (string == "k") + case 'k': return Type::King; + case 'p': + return Type::Pawn; + } return Type::None; } @@ -93,7 +94,7 @@ DeprecatedString Square::to_algebraic() const Move::Move(StringView long_algebraic) : from(long_algebraic.substring_view(0, 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('=')) { - move.promote_to = piece_for_char_promotion(move_string.split('=').at(1).substring(0, 1)); - move_string = move_string.split('=').at(0); + auto parts = move_string.split_view('='); + move.promote_to = piece_from_char(parts[1][0]); + move_string = parts[0]; } 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') { move.piece = Piece(turn, Type::Pawn); } 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); } diff --git a/Userland/Libraries/LibChess/Chess.h b/Userland/Libraries/LibChess/Chess.h index 137ac3c717..e2dfb55aa4 100644 --- a/Userland/Libraries/LibChess/Chess.h +++ b/Userland/Libraries/LibChess/Chess.h @@ -30,7 +30,7 @@ enum class Notation { FEN, }; Optional<char> char_for_piece(Type, Notation); -Chess::Type piece_for_char_promotion(StringView str); +Type piece_from_char(char); enum class Color : u8 { White, |