forked from paramhooda/cmput455-a2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard_util.py
95 lines (82 loc) · 2.93 KB
/
board_util.py
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
"""
board_util.py
Utility functions for Go board.
"""
import numpy as np
import random
from typing import List
from board_base import GO_COLOR, GO_POINT
from board import GoBoard
class GoBoardUtil(object):
@staticmethod
def generate_legal_moves(board: GoBoard, color: GO_COLOR) -> List:
"""
generate a list of all legal moves on the board.
Does not include the Pass move.
Arguments
---------
board:
a GoBoard
color:
the color to generate the move for.
"""
moves: np.ndarray[GO_POINT] = board.get_empty_points()
legal_moves: List[GO_POINT] = []
for move in moves:
if board.is_legal(move, color):
legal_moves.append(move)
return legal_moves
@staticmethod
def generate_random_move(board: GoBoard, color: GO_COLOR,
use_eye_filter: bool) -> GO_POINT:
"""
Generate a random move.
Arguments
---------
board : np.array
a 1-d array representing the board
color : BLACK, WHITE
the color to generate the move for.
"""
moves: np.ndarray[GO_POINT] = board.get_empty_points()
np.random.shuffle(moves)
for move in moves:
legal: bool = not (
use_eye_filter and board.is_eye(move, color)
) and board.is_legal(move, color)
if legal:
return move
@staticmethod
def generate_random_moves(board: GoBoard, use_eye_filter: bool) -> List:
"""
Return a list of random (legal) moves with eye-filtering.
"""
empty_points: np.ndarray[GO_POINT] = board.get_empty_points()
color: GO_COLOR = board.current_player
moves: List[GO_POINT] = []
for move in empty_points:
legal: bool = \
not (
use_eye_filter and board.is_eye(move, color)
) and board.is_legal(move, color)
if legal:
moves.append(move)
return moves
@staticmethod
def get_twoD_board(go_board: GoBoard) -> np.ndarray:
"""
Return: numpy array
a two dimensional numpy array with the goboard.
Shows stones and empty points as encoded in board_base.py.
Result is not padded with BORDER points.
Rows 1..size of goboard are copied into rows 0..size - 1 of board2d
Then the board is flipped up-down to be consistent with the
coordinate system in GoGui (row 1 at the bottom).
"""
size: int = go_board.size
board2d: np.ndarray[GO_POINT] = np.zeros((size, size), dtype=GO_POINT)
for row in range(size):
start: int = go_board.row_start(row + 1)
board2d[row, :] = go_board.board[start : start + size]
board2d = np.flipud(board2d)
return board2d