mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 17:10:23 +00:00
Chess+LibChess: Avoid using DeprecatedString
whenever possible
This commit is contained in:
parent
e7377c6d60
commit
55c5639004
Notes:
sideshowbarker
2024-07-17 11:33:34 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/55c5639004 Pull-request: https://github.com/SerenityOS/serenity/pull/17333 Reviewed-by: https://github.com/AtkinsSJ ✅
4 changed files with 48 additions and 48 deletions
|
@ -376,7 +376,7 @@ void ChessWidget::keydown_event(GUI::KeyEvent& event)
|
|||
update();
|
||||
}
|
||||
|
||||
static DeprecatedString set_path = DeprecatedString("/res/icons/chess/sets/");
|
||||
static constexpr StringView set_path = "/res/icons/chess/sets/"sv;
|
||||
|
||||
static RefPtr<Gfx::Bitmap> get_piece(StringView set, StringView image)
|
||||
{
|
||||
|
@ -444,11 +444,11 @@ void ChessWidget::set_board_theme(StringView name)
|
|||
// FIXME: Add some kind of themes.json
|
||||
// The following Colors have been taken from lichess.org, but i'm pretty sure they took them from chess.com.
|
||||
if (name == "Beige") {
|
||||
m_board_theme = { "Beige", Color::from_rgb(0xb58863), Color::from_rgb(0xf0d9b5) };
|
||||
m_board_theme = { "Beige"sv, Color::from_rgb(0xb58863), Color::from_rgb(0xf0d9b5) };
|
||||
} else if (name == "Green") {
|
||||
m_board_theme = { "Green", Color::from_rgb(0x86a666), Color::from_rgb(0xffffdd) };
|
||||
m_board_theme = { "Green"sv, Color::from_rgb(0x86a666), Color::from_rgb(0xffffdd) };
|
||||
} else if (name == "Blue") {
|
||||
m_board_theme = { "Blue", Color::from_rgb(0x8ca2ad), Color::from_rgb(0xdee3e6) };
|
||||
m_board_theme = { "Blue"sv, Color::from_rgb(0x8ca2ad), Color::from_rgb(0xdee3e6) };
|
||||
} else {
|
||||
set_board_theme("Beige"sv);
|
||||
}
|
||||
|
@ -643,7 +643,7 @@ ErrorOr<void> ChessWidget::export_pgn(Core::Stream::File& file) const
|
|||
TRY(file.write(DeprecatedString::formatted("[White \"{}\"]\n", m_side == Chess::Color::White ? player1 : player2).bytes()));
|
||||
TRY(file.write(DeprecatedString::formatted("[Black \"{}\"]\n", m_side == Chess::Color::Black ? player1 : player2).bytes()));
|
||||
|
||||
TRY(file.write(DeprecatedString::formatted("[Result \"{}\"]\n", Chess::Board::result_to_points_deprecated_string(m_board.game_result(), m_board.turn())).bytes()));
|
||||
TRY(file.write(DeprecatedString::formatted("[Result \"{}\"]\n", Chess::Board::result_to_points_string(m_board.game_result(), m_board.turn())).bytes()));
|
||||
TRY(file.write("[WhiteElo \"?\"]\n"sv.bytes()));
|
||||
TRY(file.write("[BlackElo \"?\"]\n"sv.bytes()));
|
||||
TRY(file.write("[Variant \"Standard\"]\n"sv.bytes()));
|
||||
|
@ -664,9 +664,9 @@ ErrorOr<void> ChessWidget::export_pgn(Core::Stream::File& file) const
|
|||
}
|
||||
|
||||
TRY(file.write("{ "sv.bytes()));
|
||||
TRY(file.write(Chess::Board::result_to_deprecated_string(m_board.game_result(), m_board.turn()).bytes()));
|
||||
TRY(file.write(Chess::Board::result_to_string(m_board.game_result(), m_board.turn()).bytes()));
|
||||
TRY(file.write(" } "sv.bytes()));
|
||||
TRY(file.write(Chess::Board::result_to_points_deprecated_string(m_board.game_result(), m_board.turn()).bytes()));
|
||||
TRY(file.write(Chess::Board::result_to_points_string(m_board.game_result(), m_board.turn()).bytes()));
|
||||
TRY(file.write("\n"sv.bytes()));
|
||||
|
||||
return {};
|
||||
|
@ -698,7 +698,7 @@ int ChessWidget::resign()
|
|||
|
||||
set_drag_enabled(false);
|
||||
update();
|
||||
const DeprecatedString msg = Chess::Board::result_to_deprecated_string(m_board.game_result(), m_board.turn());
|
||||
auto const msg = Chess::Board::result_to_string(m_board.game_result(), m_board.turn());
|
||||
GUI::MessageBox::show(window(), msg, "Game Over"sv, GUI::MessageBox::Type::Information);
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -62,7 +62,7 @@ public:
|
|||
void reset();
|
||||
|
||||
struct BoardTheme {
|
||||
DeprecatedString name;
|
||||
StringView name;
|
||||
Color dark_square_color;
|
||||
Color light_square_color;
|
||||
};
|
||||
|
@ -122,7 +122,7 @@ private:
|
|||
size_t m_playback_move_number { 0 };
|
||||
BoardMarking m_current_marking;
|
||||
Vector<BoardMarking> m_board_markings;
|
||||
BoardTheme m_board_theme { "Beige", Color::from_rgb(0xb58863), Color::from_rgb(0xf0d9b5) };
|
||||
BoardTheme m_board_theme { "Beige"sv, Color::from_rgb(0xb58863), Color::from_rgb(0xf0d9b5) };
|
||||
Color m_move_highlight_color { Color::from_argb(0x66ccee00) };
|
||||
Color m_marking_primary_color { Color::from_argb(0x66ff0000) };
|
||||
Color m_marking_alternate_color { Color::from_argb(0x66ffaa00) };
|
||||
|
|
|
@ -13,22 +13,22 @@
|
|||
|
||||
namespace Chess {
|
||||
|
||||
DeprecatedString char_for_piece(Chess::Type type)
|
||||
StringView char_for_piece(Chess::Type type)
|
||||
{
|
||||
switch (type) {
|
||||
case Type::Knight:
|
||||
return "N";
|
||||
return "N"sv;
|
||||
case Type::Bishop:
|
||||
return "B";
|
||||
return "B"sv;
|
||||
case Type::Rook:
|
||||
return "R";
|
||||
return "R"sv;
|
||||
case Type::Queen:
|
||||
return "Q";
|
||||
return "Q"sv;
|
||||
case Type::King:
|
||||
return "K";
|
||||
return "K"sv;
|
||||
case Type::Pawn:
|
||||
default:
|
||||
return "";
|
||||
return ""sv;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,7 +97,7 @@ DeprecatedString Move::to_long_algebraic() const
|
|||
StringBuilder builder;
|
||||
builder.append(from.to_algebraic());
|
||||
builder.append(to.to_algebraic());
|
||||
builder.append(char_for_piece(promote_to).to_lowercase());
|
||||
builder.append(DeprecatedString(char_for_piece(promote_to)).to_lowercase());
|
||||
return builder.to_deprecated_string();
|
||||
}
|
||||
|
||||
|
@ -286,11 +286,11 @@ DeprecatedString Board::to_fen() const
|
|||
builder.append(DeprecatedString::number(empty));
|
||||
empty = 0;
|
||||
}
|
||||
DeprecatedString piece = char_for_piece(p.type);
|
||||
if (piece == "")
|
||||
piece = "P";
|
||||
|
||||
builder.append(p.color == Color::Black ? piece.to_lowercase() : piece);
|
||||
auto const piece = char_for_piece(p.type);
|
||||
if (p.color == Color::Black)
|
||||
builder.append(DeprecatedString(piece).to_lowercase());
|
||||
else
|
||||
builder.append(piece);
|
||||
}
|
||||
if (empty > 0) {
|
||||
builder.append(DeprecatedString::number(empty));
|
||||
|
@ -886,59 +886,59 @@ void Board::set_resigned(Chess::Color c)
|
|||
m_resigned = c;
|
||||
}
|
||||
|
||||
DeprecatedString Board::result_to_deprecated_string(Result result, Color turn)
|
||||
StringView Board::result_to_string(Result result, Color turn)
|
||||
{
|
||||
switch (result) {
|
||||
case Result::CheckMate:
|
||||
VERIFY(turn != Chess::Color::None);
|
||||
return turn == Chess::Color::White ? "Black wins by Checkmate" : "White wins by Checkmate";
|
||||
return turn == Chess::Color::White ? "Black wins by Checkmate"sv : "White wins by Checkmate"sv;
|
||||
case Result::WhiteResign:
|
||||
return "Black wins by Resignation";
|
||||
return "Black wins by Resignation"sv;
|
||||
case Result::BlackResign:
|
||||
return "White wins by Resignation";
|
||||
return "White wins by Resignation"sv;
|
||||
case Result::StaleMate:
|
||||
return "Draw by Stalemate";
|
||||
return "Draw by Stalemate"sv;
|
||||
case Chess::Board::Result::FiftyMoveRule:
|
||||
return "Draw by 50 move rule";
|
||||
return "Draw by 50 move rule"sv;
|
||||
case Chess::Board::Result::SeventyFiveMoveRule:
|
||||
return "Draw by 75 move rule";
|
||||
return "Draw by 75 move rule"sv;
|
||||
case Chess::Board::Result::ThreeFoldRepetition:
|
||||
return "Draw by threefold repetition";
|
||||
return "Draw by threefold repetition"sv;
|
||||
case Chess::Board::Result::FiveFoldRepetition:
|
||||
return "Draw by fivefold repetition";
|
||||
return "Draw by fivefold repetition"sv;
|
||||
case Chess::Board::Result::InsufficientMaterial:
|
||||
return "Draw by insufficient material";
|
||||
return "Draw by insufficient material"sv;
|
||||
case Chess::Board::Result::NotFinished:
|
||||
return "Game not finished";
|
||||
return "Game not finished"sv;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
DeprecatedString Board::result_to_points_deprecated_string(Result result, Color turn)
|
||||
StringView Board::result_to_points_string(Result result, Color turn)
|
||||
{
|
||||
switch (result) {
|
||||
case Result::CheckMate:
|
||||
VERIFY(turn != Chess::Color::None);
|
||||
return turn == Chess::Color::White ? "0-1" : "1-0";
|
||||
return turn == Chess::Color::White ? "0-1"sv : "1-0"sv;
|
||||
case Result::WhiteResign:
|
||||
return "0-1";
|
||||
return "0-1"sv;
|
||||
case Result::BlackResign:
|
||||
return "1-0";
|
||||
return "1-0"sv;
|
||||
case Result::StaleMate:
|
||||
return "1/2-1/2";
|
||||
return "1/2-1/2"sv;
|
||||
case Chess::Board::Result::FiftyMoveRule:
|
||||
return "1/2-1/2";
|
||||
return "1/2-1/2"sv;
|
||||
case Chess::Board::Result::SeventyFiveMoveRule:
|
||||
return "1/2-1/2";
|
||||
return "1/2-1/2"sv;
|
||||
case Chess::Board::Result::ThreeFoldRepetition:
|
||||
return "1/2-1/2";
|
||||
return "1/2-1/2"sv;
|
||||
case Chess::Board::Result::FiveFoldRepetition:
|
||||
return "1/2-1/2";
|
||||
return "1/2-1/2"sv;
|
||||
case Chess::Board::Result::InsufficientMaterial:
|
||||
return "1/2-1/2";
|
||||
return "1/2-1/2"sv;
|
||||
case Chess::Board::Result::NotFinished:
|
||||
return "*";
|
||||
return "*"sv;
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ enum class Type : u8 {
|
|||
None,
|
||||
};
|
||||
|
||||
DeprecatedString char_for_piece(Type type);
|
||||
StringView char_for_piece(Type type);
|
||||
Chess::Type piece_for_char_promotion(StringView str);
|
||||
|
||||
enum class Color : u8 {
|
||||
|
@ -145,8 +145,8 @@ public:
|
|||
NotFinished,
|
||||
};
|
||||
|
||||
static DeprecatedString result_to_deprecated_string(Result, Color turn);
|
||||
static DeprecatedString result_to_points_deprecated_string(Result, Color turn);
|
||||
static StringView result_to_string(Result, Color turn);
|
||||
static StringView result_to_points_string(Result, Color turn);
|
||||
|
||||
template<typename Callback>
|
||||
void generate_moves(Callback callback, Color color = Color::None) const;
|
||||
|
|
Loading…
Reference in a new issue