-
Notifications
You must be signed in to change notification settings - Fork 2
/
kihime.cpp
63 lines (57 loc) · 1.52 KB
/
kihime.cpp
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
#include "kihime.hpp"
std::random_device Kihime::rnd;
std::mt19937 Kihime::mt = Kihime::mtInit();
std::mt19937 Kihime::mtInit(){
std::vector<std::uint_least32_t> v(10);
for(auto& e: v) e = rnd();
std::seed_seq seq(begin(v), end(v));
std::mt19937 mt(seq);
return mt;
}
Dir Kihime::decideDir(){
std::array<unsigned int, 4> cnt, depths;
cnt.fill(0);
depths.fill(0);
std::array<double, 4> aves;
aves.fill(0);
Dir max = Dir::Up;
double maxAve = 0.0;
bool flg = true;
label:;
for(int i(0); i < ITERATION; ++i){
Dir dir = allDirs[mt()%4];
while(! Board::movable(grid, dir)) dir = allDirs[mt()%4];
auto moved = Board::moved(grid, dir);
unsigned depth = toDead(moved, 0);
depths[dirToInt(dir)] += depth;
++cnt[dirToInt(dir)];
}
for(int i(0); i < 4; ++i){
if(cnt[i] == 0){
aves[i] = 0;
continue;
}
aves[i] = depths[i] / (cnt[i] * 1.0);
}
for(int i(0); i < 4; ++i){
if(aves[i] > maxAve){
max = allDirs[i];
maxAve = aves[i];
}
}
if(flg && maxAve < DANGER){
flg = false;
maxAve = 0;
goto label;
}
return max;
}
int Kihime::toDead(Board::Grid grid, int depth) {
if(Board::alive(grid)){
auto dir = allDirs[mt()%4];
while(! Board::movable(grid, dir)) dir = allDirs[mt()%4];
auto moved = Board::moveAndBirth(grid, dir).second;
return toDead(moved, depth + 1);
}
return depth;
}