123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- #ifndef CHESSY_MOVEGENERATION_H
- #define CHESSY_MOVEGENERATION_H
- #include "BitBoard.h"
- #include <string>
- #include <vector>
- #include <array>
- namespace chessy
- {
- // forward declaration
- class ChessGame;
- using Leftright = int;
- const Leftright LEFT = -1;
- const Leftright RIGHT = 1;
- class PositionSet;
- template<Side side>
- class PawnPushGenerator;
- template<Side side>
- class PromotionGenerator;
- template<Side side>
- class PawnDoublePushGenerator;
- template<Side side, Leftright leftright>
- class PawnCaptureGenerator;
- class KnightMoveGenerator;
- class PrimitiveQueenMoveGenerator;
- class PrimitiveRookMoveGenerator;
- class PrimitiveBishopMoveGenerator;
- class KingMoveGenerator;
- template<Side side>
- class CastlingGenerator;
- template<Side side>
- void generatePawnPushes(const ChessGame& cg, std::vector<Move>& moves);
- template<Side side>
- void generatePawnDoublePushes(const ChessGame& cg, std::vector<Move>& moves);
- template<Side side>
- void generatePawnCaptures(const ChessGame& cg, std::vector<Move>& moves);
- template<Side side>
- void generatePawnPromotions(const ChessGame& cg, std::vector<Move>& moves);
- template<Side side>
- void generateKnightMoves(const ChessGame& cg, std::vector<Move>& moves);
- template<Side side>
- void generateQueenMoves(const ChessGame& cg, std::vector<Move>& moves);
- template<Side side>
- void generateRookMoves(const ChessGame& cg, std::vector<Move>& moves);
- template<Side side>
- void generateBishopMoves(const ChessGame& cg, std::vector<Move>& moves);
- template<Side side>
- void generateKingMoves(const ChessGame& cg, std::vector<Move>& moves);
- template<Side side>
- void generateCastling(const ChessGame& cg, std::vector<Move>& moves);
- template<Side side>
- void generateAllMoves(const ChessGame& cg, std::vector<Move>& moves);
- }
- class chessy::PositionSet
- {
- Bitboard bitboard;
- public:
- inline PositionSet(Bitboard b) : bitboard{ b } {}
- inline void setBitboard(Bitboard b) { bitboard = b; }
- inline Bitboard getBitboard(void) const { return bitboard; }
- struct PositionSetIterator
- {
- Bitboard bitboard;
- inline Index operator *(void) const
- {
- return Index{ int8_t(trailingZeroes(bitboard.bits)) };
- }
- inline void operator ++(void)
- {
- bitboard.bits &= bitboard.bits - 1ULL; // remove least significant one bit
- }
- inline bool operator !=(const PositionSetIterator& psi) const
- {
- return bitboard != psi.bitboard;
- }
- };
- inline PositionSetIterator begin(void) const
- {
- return PositionSetIterator{ bitboard };
- }
- inline static PositionSetIterator end(void)
- {
- return PositionSetIterator{ 0 };
- }
- };
- template<chessy::Side side>
- class chessy::PawnPushGenerator
- {
- const ChessGame& chessGame;
- struct MoveIterator
- {
- PositionSet::PositionSetIterator pawnPushes;
- inline Move operator *(void) const
- {
- Index pp = *pawnPushes;
- return Move{ int8_t(pp + (side != WHITE_SIDE ? 8 : -8)), pp };
- }
- inline void operator ++(void)
- {
- ++pawnPushes;
- }
- inline bool operator !=(const MoveIterator& psi) const
- {
- return pawnPushes != psi.pawnPushes;
- }
- };
- public:
- inline PawnPushGenerator(const ChessGame& cg) : chessGame{ cg } {}
- MoveIterator begin(void) const;
- MoveIterator end(void) const;
- };
- // TODO: rewrite better
- template<chessy::Side side>
- class chessy::PromotionGenerator
- {
- const ChessGame& chessGame;
- struct MoveIterator
- {
- const ChessGame& chessGame;
- PositionSet::PositionSetIterator pawns;
- PieceType promotionType;
- char direction;
- inline Move operator *(void) const
- {
- Index pp = *pawns;
- return Move{ pp, int8_t(pp + (side == WHITE_SIDE ? 8 : -8) +
- direction), promotionType };
- }
- private:
- void next(void);
- bool valid(void) const;
- public:
- inline void operator ++(void)
- {
- do {
- next();
- } while(!valid());
- }
- inline bool operator !=(const MoveIterator& psi) const
- {
- return pawns != psi.pawns;
- }
- };
- public:
- inline PromotionGenerator(const ChessGame& cg) : chessGame{ cg } {}
- MoveIterator begin(void) const;
- MoveIterator end(void) const;
- };
- template<chessy::Side side>
- class chessy::PawnDoublePushGenerator
- {
- const ChessGame& chessGame;
- struct MoveIterator
- {
- PositionSet::PositionSetIterator pawnPushes;
- inline Move operator *(void) const
- {
- Index pp = *pawnPushes;
- return Move{ int8_t(pp + (side != WHITE_SIDE ? 16 : -16)), pp };
- }
- inline void operator ++(void)
- {
- ++pawnPushes;
- }
- inline bool operator !=(const MoveIterator& psi) const
- {
- return pawnPushes != psi.pawnPushes;
- }
- };
- public:
- inline PawnDoublePushGenerator(const ChessGame& cg) : chessGame{ cg } {}
- MoveIterator begin(void) const;
- MoveIterator end(void) const;
- };
- template<chessy::Side side, chessy::Leftright leftright>
- class chessy::PawnCaptureGenerator
- {
- const ChessGame& chessGame;
- struct MoveIterator
- {
- PositionSet::PositionSetIterator pawnPushes;
- inline Move operator *(void) const
- {
- Index pp = *pawnPushes;
- return Move{
- int8_t(pp + (side != WHITE_SIDE ? 8 : -8) - leftright), pp
- };
- }
- inline void operator ++(void)
- {
- ++pawnPushes;
- }
- inline bool operator !=(const MoveIterator& psi) const
- {
- return pawnPushes != psi.pawnPushes;
- }
- };
- public:
- inline PawnCaptureGenerator(const ChessGame& cg) : chessGame{ cg } {}
- MoveIterator begin(void) const;
- MoveIterator end(void) const;
- };
- /*!
- * extends \link PositionSet \endlink so that all possible destinations for
- * a knight can be iterated over.
- */
- class chessy::KnightMoveGenerator :
- public PositionSet
- {
- static const std::array<Bitboard, 64> moveSets;
- public:
- inline KnightMoveGenerator(Index knight, Bitboard occupied) :
- PositionSet{ moveSets[knight] & ~occupied } {}
- private:
- static Bitboard generateFromIndex(Index i);
- static std::array<Bitboard, 64> generateKnightMoves(void);
- };
- /*!
- * uses a primitive algorithm to calculate all possible move destinations
- * for a queen.
- */
- class chessy::PrimitiveQueenMoveGenerator :
- public PositionSet
- {
- protected:
- inline PrimitiveQueenMoveGenerator(Index i, Bitboard enemies, Bitboard friendly,
- bool straight, bool diagonal) :
- PositionSet{ generateAttackSet(i, enemies, friendly, straight, diagonal) } {}
- public:
- inline PrimitiveQueenMoveGenerator(Index i, Bitboard enemies,
- Bitboard friendly) :
- PrimitiveQueenMoveGenerator{ i, enemies, friendly, true, true } {}
- private:
- Bitboard generateAttackSet(Index i, Bitboard enemies, Bitboard friendly,
- bool straight, bool diagonal);
- };
- class chessy::PrimitiveBishopMoveGenerator :
- public PrimitiveQueenMoveGenerator
- {
- public:
- inline PrimitiveBishopMoveGenerator(Index i, Bitboard enemies,
- Bitboard friendly) :
- // bishop moves are actually just diagonal queen moves
- PrimitiveQueenMoveGenerator{ i, enemies, friendly, false, true } {}
- };
- class chessy::PrimitiveRookMoveGenerator :
- public PrimitiveQueenMoveGenerator
- {
- public:
- inline PrimitiveRookMoveGenerator(Index i, Bitboard enemies,
- Bitboard friendly) :
- // rook moves are actually just straight queen moves
- PrimitiveQueenMoveGenerator{ i, enemies, friendly, true, false } {}
- };
- class chessy::KingMoveGenerator :
- public PositionSet
- {
- public:
- inline KingMoveGenerator(Bitboard king, Bitboard friendly) :
- PositionSet{ generateKingMoves(king) & ~friendly } {}
- private:
- Bitboard generateKingMoves(Bitboard king);
- };
- template<chessy::Side side>
- class chessy::CastlingGenerator :
- public PositionSet
- {
- public:
- CastlingGenerator(const ChessGame& game);
- };
- #endif // CHESSY_MOVEGENERATION_H
|