-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathKoState.cpp
94 lines (69 loc) · 2.39 KB
/
KoState.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
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
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
#include <algorithm>
#include "config.h"
#include "FastState.h"
#include "FullBoard.h"
#include "KoState.h"
void KoState::init_game(int size, float komi) {
assert(size <= FastBoard::MAXBOARDSIZE);
FastState::init_game(size, komi);
ko_hash_history.clear();
hash_history.clear();
ko_hash_history.push_back(board.calc_ko_hash());
hash_history.push_back(board.calc_hash());
}
bool KoState::legal_move(int vertex) {
if (board.get_square(vertex) != FastBoard::EMPTY) {
return false;
}
if (board.is_suicide(vertex, board.get_to_move())) {
return false;
}
KoState tmp = *this;
tmp.play_move(vertex);
if (tmp.superko()) {
return false;
}
return true;
}
bool KoState::superko(void) {
std::vector<uint64>::const_reverse_iterator first = ko_hash_history.rbegin();
std::vector<uint64>::const_reverse_iterator last = ko_hash_history.rend();
std::vector<uint64>::const_reverse_iterator res;
res = std::find(++first, last, board.ko_hash);
return (res != last);
}
bool KoState::superko(uint64 newhash) {
std::vector<uint64>::const_reverse_iterator first = ko_hash_history.rbegin();
std::vector<uint64>::const_reverse_iterator last = ko_hash_history.rend();
std::vector<uint64>::const_reverse_iterator res;
res = std::find(first, last, newhash);
return (res != last);
}
void KoState::reset_game() {
FastState::reset_game();
ko_hash_history.clear();
hash_history.clear();
ko_hash_history.push_back(board.calc_ko_hash());
hash_history.push_back(board.calc_hash());
}
void KoState::play_pass(void) {
FastState::play_pass();
ko_hash_history.push_back(board.ko_hash);
hash_history.push_back(board.hash);
}
void KoState::play_move(int vertex) {
play_move(board.get_to_move(), vertex);
}
void KoState::play_move(int color, int vertex) {
if (vertex != FastBoard::PASS && vertex != FastBoard::RESIGN) {
FastState::play_move(color, vertex);
ko_hash_history.push_back(board.ko_hash);
hash_history.push_back(board.hash);
} else {
play_pass();
}
}