stellar

Stellar - UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE

movelist.hpp (1166B)


      1 #ifndef STELLAR_MOVE_LIST_H
      2 #define STELLAR_MOVE_LIST_H
      3 
      4 #include "board.hpp"
      5 #include "move.hpp"
      6 #include "utils.hpp"
      7 
      8 #include <iostream>
      9 #include <numeric>
     10 #include <vector>
     11 
     12 class MoveList {
     13   private:
     14     using list_t = std::vector<Move>;
     15 
     16   public:
     17     MoveList() : list(){};
     18     MoveList(const Board &board, bool attacks_only = false) : list() {
     19         list.reserve(256);
     20         generate(board, attacks_only);
     21     }
     22 
     23     MoveList(const Board &board, bool attacks_only, bool legal) : MoveList(board, attacks_only) {
     24         int size = 0;
     25         for (const auto &move : list) {
     26             Board copy = board;
     27             if (move.make(copy)) list[size++] = move;
     28         }
     29         list.resize(size);
     30     }
     31 
     32     void clear() { list.clear(); }
     33     [[nodiscard]] int size() const { return list.size(); }
     34 
     35     const Move operator[](size_t idx) const { return list[idx]; }
     36     Move &operator[](size_t idx) { return list[idx]; }
     37     void push(const Move move) { list.push_back(move); }
     38 
     39     friend std::ostream &operator<<(std::ostream &os, const MoveList &list);
     40 
     41   private:
     42     void generate(const Board &board, bool attacks_only);
     43 
     44     list_t list;
     45 };
     46 
     47 #endif