-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoState.py
More file actions
67 lines (53 loc) · 2.18 KB
/
GoState.py
File metadata and controls
67 lines (53 loc) · 2.18 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
from GoBoard import Board
from GoMove import Move
from GoPoint import Point
from copy import deepcopy
EMPTY, BLACK, WHITE = range(0, 3)
class State:
def __init__(self, board, p_state, next_player, move):
self.board: Board = board
self.previous_state: State = p_state
self.next_player = next_player
self.other_player = 3 - next_player
self.last_move: Move = move
# def is_move_valid(self, move):
# if move.is_pass:
# return True
# if not self.board.valid_point_check(move):
# return False
# temp_board = deepcopy(self.board)
# temp_board.place_stone(move, self.next_player)
# if not temp_board.has_liberty(move.point):
# return False
# if temp_board == self.previous_state.board:
# return False
# return True
def apply_move(self, move: Move):
next_board = deepcopy(self.board)
if move.is_play:
if not self.board.valid_move_check(move): # Basic Move Validity Checks
return None
next_board.place_stone(move, self.next_player)
point: Point = move.point
if not next_board.has_liberty(point): # Suicidal Move Check
return None
if next_board == self.previous_state.board: # Ko Violation check
return None
return State(next_board, self, self.other_player, move)
def stone_diff(self):
black_stones = 0
white_stones = 0
for i in range(self.board.board_size):
for j in range(self.board.board_size):
point = Point(i, j)
if self.board.get_point_color(point) == BLACK:
black_stones += 1
elif self.board.get_point_color(point) == WHITE:
white_stones += 1
return black_stones - white_stones - self.board.board_size/2.0
def is_terminal(self):
last_move: Move = self.last_move
last_to_last_move: Move = self.previous_state.last_move
if last_move is not None and last_to_last_move is not None:
return last_move.is_pass and last_to_last_move.is_pass
return False