/
utils.py
53 lines (37 loc) · 1.2 KB
/
utils.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
from board import Board
from colors import Color
from exceptions import IllegalGoPieceColorException
def piece_str_to_color(color_str):
"""
Maps single letters to piece Color enum
:param color_str: str
:return: Color enum
"""
color_str = color_str.lower()
COLORS_MAP = {
'w': Color.white,
'b': Color.black
}
if color_str not in COLORS_MAP:
raise IllegalGoPieceColorException(
"Invalid color string: {}".format(color_str)
)
return COLORS_MAP[color_str]
def create_board_from_str(board_str, empty_location_str='-'):
"""
:param board_str: str representing desired board configuration
:return: Board
"""
# Remove spaces
board_str = board_str.replace(' ', '')
# Split by lines
board_rows = board_str.split('\n')
# Remove empty lines
board_rows = [row for row in board_rows if row]
board = Board(len(board_rows))
for r, row in enumerate(board_rows):
for c, piece_color_str in enumerate(row):
if piece_color_str == empty_location_str: continue
color = piece_str_to_color(piece_color_str)
board.add(color=color, position=(r,c))
return board