diff options
author | Peter Elliott <pelliott@ualberta.ca> | 2021-06-19 16:33:20 -0600 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-22 23:09:42 +0200 |
commit | 34433f5dc4a8cc241fe374c580623f9e12f64939 (patch) | |
tree | 1862e70eae45eb088a58e671b7ba8fac0d6b701f /Userland/Libraries | |
parent | 57bb4d1aec903b5336aa06d1b0de4d016a403043 (diff) | |
download | serenity-34433f5dc4a8cc241fe374c580623f9e12f64939.zip |
LibChess: Compact the Defenitions of various chess related types
before:
sizeof(Board)=344, sizeof(Move)=36, sizeof(Piece)=4, sizeof(Square)=8
after:
sizeof(Board)=108, sizeof(Move)=9, sizeof(Piece)=1, sizeof(Square)=2
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibChess/Chess.h | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/Userland/Libraries/LibChess/Chess.h b/Userland/Libraries/LibChess/Chess.h index 9ad01408ae..5a75a8c569 100644 --- a/Userland/Libraries/LibChess/Chess.h +++ b/Userland/Libraries/LibChess/Chess.h @@ -15,7 +15,7 @@ namespace Chess { -enum class Type { +enum class Type : u8 { Pawn, Knight, Bishop, @@ -28,7 +28,7 @@ enum class Type { String char_for_piece(Type type); Chess::Type piece_for_char_promotion(const StringView& str); -enum class Color { +enum class Color : u8 { White, Black, None, @@ -55,8 +55,8 @@ struct Piece { constexpr Piece EmptyPiece = { Color::None, Type::None }; struct Square { - int rank; // zero indexed; - int file; + i8 rank; // zero indexed; + i8 file; Square(const StringView& name); Square(const int& rank, const int& file) : rank(rank) @@ -88,10 +88,10 @@ struct Move { Square to; Type promote_to; Piece piece; - bool is_check = false; - bool is_mate = false; - bool is_capture = false; - bool is_ambiguous = false; + bool is_check : 1 = false; + bool is_mate : 1 = false; + bool is_capture : 1 = false; + bool is_ambiguous : 1 = false; Square ambiguous { 50, 50 }; Move(const StringView& long_algebraic); Move(const Square& from, const Square& to, const Type& promote_to = Type::None) @@ -161,16 +161,17 @@ private: bool apply_illegal_move(const Move&, Color color); Piece m_board[8][8]; - Color m_turn { Color::White }; - Color m_resigned { Color::None }; Optional<Move> m_last_move; - int m_moves_since_capture { 0 }; - int m_moves_since_pawn_advance { 0 }; + short m_moves_since_capture { 0 }; + short m_moves_since_pawn_advance { 0 }; - bool m_white_can_castle_kingside { true }; - bool m_white_can_castle_queenside { true }; - bool m_black_can_castle_kingside { true }; - bool m_black_can_castle_queenside { true }; + Color m_turn : 2 { Color::White }; + Color m_resigned : 2 { Color::None }; + + bool m_white_can_castle_kingside : 1 { true }; + bool m_white_can_castle_queenside : 1 { true }; + bool m_black_can_castle_kingside : 1 { true }; + bool m_black_can_castle_queenside : 1 { true }; // We trust that hash collisions will not happen to save lots of memory and time. HashMap<unsigned, int> m_previous_states; |