diff --git a/Userland/Applications/GamesSettings/ChessSettingsWidget.cpp b/Userland/Applications/GamesSettings/ChessSettingsWidget.cpp index cddfc7f8a0..645a32d8bf 100644 --- a/Userland/Applications/GamesSettings/ChessSettingsWidget.cpp +++ b/Userland/Applications/GamesSettings/ChessSettingsWidget.cpp @@ -180,15 +180,18 @@ private: painter.fill_rect(square_rect, square.is_light() ? m_light_square_color : m_dark_square_color); if (m_show_coordinates) { - auto coord = square.to_algebraic(); auto text_color = square.is_light() ? m_dark_square_color : m_light_square_color; auto shrunken_rect = square_rect.shrunken(4, 4); - if (square.rank == 0) - painter.draw_text(shrunken_rect, coord.substring_view(0, 1), coordinate_font, Gfx::TextAlignment::BottomRight, text_color); + if (square.rank == 0) { + auto file_char = square.file_char(); + painter.draw_text(shrunken_rect, { &file_char, 1 }, coordinate_font, Gfx::TextAlignment::BottomRight, text_color); + } - if (square.file == 0) - painter.draw_text(shrunken_rect, coord.substring_view(1, 1), coordinate_font, Gfx::TextAlignment::TopLeft, text_color); + if (square.file == 0) { + auto rank_char = square.rank_char(); + painter.draw_text(shrunken_rect, { &rank_char, 1 }, coordinate_font, Gfx::TextAlignment::TopLeft, text_color); + } } } } diff --git a/Userland/Games/Chess/ChessWidget.cpp b/Userland/Games/Chess/ChessWidget.cpp index bb2bd05900..d736c5fbfe 100644 --- a/Userland/Games/Chess/ChessWidget.cpp +++ b/Userland/Games/Chess/ChessWidget.cpp @@ -66,16 +66,20 @@ void ChessWidget::paint_event(GUI::PaintEvent& event) } if (m_coordinates) { - auto coord = sq.to_algebraic(); auto text_color = (sq.is_light()) ? board_theme().dark_square_color : board_theme().light_square_color; auto shrunken_rect = tile_rect; shrunken_rect.shrink(4, 4); - if (sq.rank == coord_rank_file) - painter.draw_text(shrunken_rect, coord.substring_view(0, 1), coordinate_font, Gfx::TextAlignment::BottomRight, text_color); - if (sq.file == coord_rank_file) - painter.draw_text(shrunken_rect, coord.substring_view(1, 1), coordinate_font, Gfx::TextAlignment::TopLeft, text_color); + if (sq.rank == coord_rank_file) { + auto file_char = sq.file_char(); + painter.draw_text(shrunken_rect, { &file_char, 1 }, coordinate_font, Gfx::TextAlignment::BottomRight, text_color); + } + + if (sq.file == coord_rank_file) { + auto rank_char = sq.rank_char(); + painter.draw_text(shrunken_rect, { &rank_char, 1 }, coordinate_font, Gfx::TextAlignment::TopLeft, text_color); + } } for (auto& m : m_board_markings) { diff --git a/Userland/Libraries/LibChess/Chess.cpp b/Userland/Libraries/LibChess/Chess.cpp index 74ee1c0974..cf8d24ee05 100644 --- a/Userland/Libraries/LibChess/Chess.cpp +++ b/Userland/Libraries/LibChess/Chess.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -83,6 +84,16 @@ Square::Square(StringView name) } } +char Square::file_char() const +{ + return file + 'a'; +} + +char Square::rank_char() const +{ + return rank + '1'; +} + DeprecatedString Square::to_algebraic() const { StringBuilder builder; @@ -200,16 +211,16 @@ DeprecatedString Move::to_algebraic() const if (is_ambiguous) { if (from.file != ambiguous.file) - builder.append(from.to_algebraic().substring(0, 1)); + builder.append(from.file_char()); else if (from.rank != ambiguous.rank) - builder.append(from.to_algebraic().substring(1, 1)); + builder.append(from.rank_char()); else builder.append(from.to_algebraic()); } if (is_capture) { if (piece.type == Type::Pawn && !is_ambiguous) - builder.append(from.to_algebraic().substring(0, 1)); + builder.append(from.file_char()); builder.append('x'); } diff --git a/Userland/Libraries/LibChess/Chess.h b/Userland/Libraries/LibChess/Chess.h index e2dfb55aa4..554eda0fe3 100644 --- a/Userland/Libraries/LibChess/Chess.h +++ b/Userland/Libraries/LibChess/Chess.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2020, the SerenityOS developers. + * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -89,6 +90,9 @@ struct Square { bool in_bounds() const { return rank >= 0 && file >= 0 && rank < 8 && file < 8; } bool is_light() const { return (rank % 2) != (file % 2); } + + char file_char() const; + char rank_char() const; DeprecatedString to_algebraic() const; };