-
Notifications
You must be signed in to change notification settings - Fork 0
/
OthelloBoard.py
127 lines (105 loc) · 4.21 KB
/
OthelloBoard.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'''
Erich Kramer - April 2017
Apache License
If using this code please cite creator.
'''
from Board import *
class OthelloBoard(Board):
def __init__(self, rows, cols, p1, p2):
Board.__init__(self, rows, cols)
self.p1_symbol = p1
self.p2_symbol = p2
#PYTHON: this function is substitute for clone. call as New = Old.cloneOBoard()
def cloneOBoard(self):
tmp = OthelloBoard(self.cols, self.rows, self.p1_symbol, self.p2_symbol)
tmp.grid = copy.deepcopy(self.grid)
return tmp;
def initialize(self):
self.set_cell(self.cols //2 -1, self.rows //2 -1, self.p1_symbol)
self.set_cell(self.cols //2, self.rows //2, self.p1_symbol)
self.set_cell(self.cols //2 -1, self.rows //2, self.p2_symbol)
self.set_cell(self.cols //2, self.rows //2 -1, self.p2_symbol)
#PYTHON: Instead of having side effects this function now returns a TUPLE
def set_coords_in_direction(self, col, row, D):#D=direction
if(D.name == 'N'):
row += 1
elif(D.name == 'NE'):
col+=1
row+=1
elif(D.name == 'E'):
col+=1
elif(D.name == 'SE'):
col+=1
row-=1
elif(D.name == 'S'):
row-=1
elif(D.name == 'SW'):
col-=1
row-=1
elif(D.name == 'W'):
col-=1
elif(D.name == 'NW'):
col-=1
row+=1
else:
print("Invalid Direction.")
return (col, row)
#Recursively travel in a direction
def check_endpoint(self, col, row, symbol, d, match_symbol):#match is bool type
if not self.is_in_bounds(col, row) or self.is_cell_empty(col,row):
return False
else:
if(match_symbol):
if(self.get_cell(col, row) == symbol):
return True
else:
(next_col, next_row) = self.set_coords_in_direction(col, row, d)
return self.check_endpoint(next_col, next_row, symbol, d, match_symbol)
else:
if(self.get_cell(col, row) == symbol):
return False
else:
(next_col, next_row) = self.set_coords_in_direction(col, row, d)
return self.check_endpoint(next_col, next_row, symbol, d, not match_symbol)
def is_legal_move(self, col, row, symbol):
result = False
if(not self.is_in_bounds(col, row) or not self.is_cell_empty(col, row)):
return False
for d in Direction: #enum from board.py
(next_col, next_row) = self.set_coords_in_direction(col, row, d)
if(self.check_endpoint(next_col, next_row, symbol, d, False)):
return True
return False
def flip_pieces_helper(self, col, row, symbol, d):
if(self.get_cell(col, row) == symbol):
return 0;
else:
self.set_cell(col,row, symbol)
(next_col, next_row) = self.set_coords_in_direction(col, row, d)
return 1+ self.flip_pieces_helper(next_col, next_row, symbol, d)
def flip_pieces(self, col, row, symbol):
pieces_flipped = 0
if(not self.is_in_bounds(col, row)):
print("Flip Pieces bad params.")
exit();
for d in Direction:
(next_col, next_row) = self.set_coords_in_direction(col,row,d)
if(self.check_endpoint(next_col, next_row, symbol, d, False)):
pieces_flipped += self.flip_pieces_helper(next_col, next_row, symbol, d);
return pieces_flipped
def has_legal_moves_remaining(self, symbol):
for c in range (0, self.cols):
for r in range (0, self.rows):
if self.is_cell_empty(c, r) and self.is_legal_move(c, r, symbol):
return True
return False;
def count_score(self, symbol):
score = 0
for c in range (0, self.cols):
for r in range (0, self.rows):
if self.grid[c][r] == symbol:
score+=1
return score
def play_move(self, col, row, symbol):
self.set_cell(col, row, symbol)
self.flip_pieces(col, row, symbol)