diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2023-04-24 12:29:31 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-04-24 20:58:35 +0200 |
commit | 5f6dd87163f7c7dded9f936eb0b3886cad3a9573 (patch) | |
tree | f58fb491847e9cdc5a5df103625e82575df11f81 /Userland/Libraries/LibChess | |
parent | c73c697f9425cba1e530fc49a5d0502fad9b77c0 (diff) | |
download | serenity-5f6dd87163f7c7dded9f936eb0b3886cad3a9573.zip |
LibChess: Add and use Square::{file,rank}_char() methods
This saves us having to build and allocate a String, just to then use
one character of it.
Diffstat (limited to 'Userland/Libraries/LibChess')
-rw-r--r-- | Userland/Libraries/LibChess/Chess.cpp | 17 | ||||
-rw-r--r-- | Userland/Libraries/LibChess/Chess.h | 4 |
2 files changed, 18 insertions, 3 deletions
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 <atkinssj@serenityos.org> * * 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 <atkinssj@serenityos.org> * * 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; }; |