-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.py
100 lines (73 loc) · 2.14 KB
/
Board.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
96
97
98
99
'''
Erich Kramer - April 2017
Apache License
If using this code please cite creator.
'''
import copy
#The following has been excluded as no application is apparent
from enum import Enum
#"Directsion enum for the Othello Board"
# a = Direction.S;
# a.name == 'NE'
#>true
class Direction(Enum):
N = 1
NE = 2
E = 3
SE = 4
S = 5
SW = 6
W = 7
NW = 8
#global empty state
EMPTY = '.'
class Board:
def __init__(self, cols, rows):
self.cols = cols
self.rows = rows
#Col x Row grid filled with "EMPTY"
self.grid = [[EMPTY for x in range(cols)] for y in range(rows)]
#PYTHON #Duplicate a board with B2 = B1.cloneBoard()
#THIS SERVES AS BOTH COPY CONSTRUCTOR AND ASSIGNMENT
def cloneBoard(self):
tmp = Board(self.cols, self.rows)
tmp.grid = copy.deepcopy(self.grid)
return tmp
#deepcopy is slow, consider replacing with list slice if performance demands
#empties grid. No other references will remain.
def delete_grid(self):
for x in range(len(self.grid)):
del self.grid[x][:]
del self.grid[:]
def get_num_cols(self):
return self.cols
def get_num_rows(self):
return self.rows
def get_cell(self, col, row):
if not self.is_in_bounds(col, row):
return None
else:
return self.grid[col][row]
def set_cell(self, col, row, val):
if not self.is_in_bounds(col, row):
return None
else:
self.grid[col][row] = val
def is_cell_empty(self, col, row):
if self.grid[col][row] == EMPTY:
return True
return False
def is_in_bounds(self, col, row):
if (0 <= col < self.cols) and (0<= row < self.rows):
return True
else:
return False
def display(self):
string2 = '--' * self.cols
print(string2)
for r in range(0,self.rows):
string = ''
for c in range(0, self.cols):
string += self.grid[c][r] + ' '
print(string)
print(string2)