diff --git a/Board.cpp b/Board.cpp index 6aa3325..d084664 100644 --- a/Board.cpp +++ b/Board.cpp @@ -24,7 +24,7 @@ Piece::Optional Board::piece(const Square &square) const { BitBoard mask = indexToBitBoard(square.index()); for (const auto &kPieceBb : mPieceBBs) { if (kPieceBb & mask) { - return Piece(indexToColor(i), indexToType(i)); + return Piece::fromValue(i); } i++; diff --git a/Board.hpp b/Board.hpp index 94d81d8..d167aca 100644 --- a/Board.hpp +++ b/Board.hpp @@ -52,14 +52,6 @@ private: static inline BitBoard indexToBitBoard(unsigned i) { return (1ULL << i); } - - static inline PieceColor indexToColor(unsigned i) { - return static_cast(i % 2); - } - - static inline PieceType indexToType(unsigned i) { - return static_cast(i - (i % 2)); - } }; std::ostream &operator<<(std::ostream &os, const Board &board); diff --git a/Piece.cpp b/Piece.cpp index 276b7ad..40d1ef7 100644 --- a/Piece.cpp +++ b/Piece.cpp @@ -42,6 +42,9 @@ std::optional Piece::pieceTypeFromSymbol(char symbol) { default: return std::nullopt; } } +Piece::Optional Piece::fromValue(unsigned int value) { + return Piece(valToColor(value), valToType(value)); +} bool operator==(const Piece &lhs, const Piece &rhs) { return lhs.color() == rhs.color() && lhs.type() == rhs.type(); diff --git a/Piece.hpp b/Piece.hpp index 8565ae4..8e936c3 100644 --- a/Piece.hpp +++ b/Piece.hpp @@ -27,6 +27,7 @@ public: Piece(PieceColor color, PieceType type); static Optional fromSymbol(char symbol); + static Optional fromValue(unsigned value); static std::optional pieceTypeFromSymbol(char symbol); @@ -38,6 +39,14 @@ public: private: const PieceColor mColor; const PieceType mType; + + static inline PieceColor valToColor(unsigned v) { + return static_cast(v % 2); + } + + static inline PieceType valToType(unsigned v) { + return static_cast(v - (v % 2)); + } }; bool operator==(const Piece &lhs, const Piece &rhs);