Chess Engine
A Chess Engine project written in C++.
Loading...
Searching...
No Matches
movegen.hpp
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <iostream>
6#include <vector>
7
8#include "bitboard.hpp"
9#include "position.hpp"
10
11namespace Movegen {
12
13constexpr int MAX_MOVES = 256; // limit of possible number of moves that can be performed from any position
14
15enum GenerationType {
16 CAPTURE,
17 NON_CAPTURE,
18 EVASION,
19 NON_EVASION,
20 LEGAL
21};
22
23extern std::array<std::array<Bitboard, Square::NB>, PieceType::NB> pseudoAttacks;
24
25extern std::array<std::array<Bitboard, Square::NB>, Color::NB> pawnAttacks;
26extern std::array<std::array<Bitboard, Square::NB>, Color::NB> pawnSinglePushes;
27
28const int BISHOP_ATTACK_NB = 0x1480;
29const int ROOK_ATTACK_NB = 0x19000;
30
31extern std::vector<Bitboard> bishopAttacks;
32extern std::vector<Bitboard> rookAttacks;
33
34void init();
35
36template <GenerationType T>
37class MoveList {
38 private:
39 std::array<Move, MAX_MOVES> moveList;
40 size_t size;
41
42 public:
43 explicit MoveList(const Position& position) : size(generateMoves<T>(position, moveList)) {}
44};
45
46template <GenerationType>
47size_t generateMoves(const Position& position, std::array<Move, MAX_MOVES>& moveList);
48
49template <bool Root>
50int perft(Position& position, int /*depth*/, std::vector<Move>& moveStack) {
51 const size_t start = moveStack.size();
52 const size_t count = 0; // generateMoves(position, moveStack);
53
54 int nodes = 0;
55
56 for (size_t i = 0; i < count; ++i) {
57 const Move& move = moveStack[start + i];
58 position.doMove(move);
59 /*if (isLegal(position)) [[likely]] {
60 if (depth == 1) {
61 nodes++;
62 } else {
63 int subnodes = perft<false>(position, depth - 1, moveStack);
64 if constexpr (Root) {
65 std::cout << move.from() << move.to() << ": " << subnodes << '\n';
66 }
67 nodes += subnodes;
68 }
69 }*/
70 position.undoMove();
71 }
72 moveStack.resize(start);
73 return nodes;
74}
75
76int perft(Position& position, int depth, bool verbose = true) {
77 std::vector<Move> moveStack;
78 if (verbose) {
79 return perft<true>(position, depth, moveStack);
80 } else {
81 return perft<false>(position, depth, moveStack);
82 }
83}
84
85} // namespace Movegen
Definition position.hpp:16
Definition move.hpp:64