forked from Ttl/leela-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoState.cpp
110 lines (81 loc) · 2.64 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
This file is part of Leela Zero.
Copyright (C) 2017 Gian-Carlo Pascutto
Leela Zero is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Leela Zero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Leela Zero. If not, see <http://www.gnu.org/licenses/>.
*/
#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) {
auto first = crbegin(ko_hash_history);
auto last = crend(ko_hash_history);
auto res = std::find(++first, last, board.ko_hash);
return (res != last);
}
bool KoState::superko(uint64 newhash) {
auto first = crbegin(ko_hash_history);
auto last = crend(ko_hash_history);
auto 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();
}
}