-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuzero_interface.h
More file actions
135 lines (117 loc) · 3.49 KB
/
suzero_interface.h
File metadata and controls
135 lines (117 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef SUZERO_INTERFACE_H
#define SUZERO_INTERFACE_H
#include <cstdint>
#include <vector>
#include <array>
#include <tuple>
#include <string>
#include <memory>
#include <map>
inline void hash_combine(std::size_t& seed) { }
template <typename T, typename... Rest>
inline void hash_combine(std::size_t& seed, const T& v, Rest... rest) {
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
hash_combine(seed, rest...);
}
struct StateForAI
{
bool isWhiteTrun;
bool hasSelection;
std::vector<uint64_t> bitboards;
std::array<std::vector<int>, 2> shapeOfBoards;
int pieceCoords[4];
};
template<>
struct std::hash<StateForAI>
{
std::size_t operator()(const StateForAI& s) const noexcept
{
size_t seed = 0;
hash_combine(seed, s.isWhiteTrun, s.hasSelection);
hash_combine(seed, s.pieceCoords[0], s.pieceCoords[1], s.pieceCoords[2], s.pieceCoords[3]);
for(uint64_t x: s.bitboards)
{
x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
seed ^= x ^ (x >> 31) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
for(auto xs : s.shapeOfBoards)
{
for(auto x : xs)
{
seed ^= x + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
}
return seed;
}
};
struct AllLegalAction
{
bool canSwitchPlayer;
std::map<std::tuple<int, int>, std::vector<std::pair<int, int>>> legalPosition;
};
/**
* This is the interface. An implementation of this interface
* must override every pure virtual method in this class.
* Ideally, it also provides a static method `getNewTurnZeroAIMode`
* which returns a `shared_ptr<SuZeroState>`
* (See: SuZeroStateDummyImplementation below)
*/
class SuZeroState {
public:
virtual std::size_t getStateHash()
{
StateForAI s = getStateForAI();
std::hash<StateForAI> hasher;
return hasher(s);
}
virtual bool getPlayer() = 0;
virtual bool isGameOver() const = 0;
virtual int getReward() const = 0;
virtual StateForAI getStateForAI() const = 0;
virtual AllLegalAction getAllLegalAction() const = 0;
virtual std::array<float, 2> forceScoring() const = 0;
virtual std::shared_ptr<SuZeroState> switchActivePlayer() const = 0;
virtual std::shared_ptr<SuZeroState> selectMovePosition(std::array<int, 4>) = 0;
};
class SuZeroStateDummyImplementation : public SuZeroState {
public:
bool getPlayer() override
{
return true; // Dummy return value
}
bool isGameOver() const override
{
return false; // Dummy return value
}
int getReward() const override
{
return 0; // Dummy return value
}
StateForAI getStateForAI() const override
{
return StateForAI{}; // Return a default-constructed state
}
AllLegalAction getAllLegalAction() const override
{
return AllLegalAction(); // No legal actions
}
std::array<float, 2> forceScoring() const override
{
return {0.0f, 0.0f}; // Dummy scores
}
std::shared_ptr<SuZeroState> switchActivePlayer() const override
{
return nullptr;
}
std::shared_ptr<SuZeroState> selectMovePosition(std::array<int, 4>) override
{
return nullptr;
}
static std::shared_ptr<SuZeroState> getNewTurnZeroAIMode()
{
return std::make_shared<SuZeroStateDummyImplementation>();
}
};
#endif //SUZERO_INTERFACE_H