/
game_state.py
201 lines (157 loc) · 5.71 KB
/
game_state.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import label
import itertools
class Position(object):
"""Defines a position on the board."""
def __init__(self, row, col):
self.row = row
self.col = col
def __repr__(self):
return '(%d, %d)' % (self.row, self.col)
class Piece(object):
"""Defines a position on the board."""
def __init__(self, state, color, pos):
self.state = state
self.color = color
self.pos = pos
def get_moves(self):
""" Returns a list of all possible moves from this piece."""
row_offset = 1 if self.color == label.RED else -1
left_offset = -1
right_offset = 1
moves = []
# Try advance
if self.state.check_cell(self.pos.row + row_offset, self.pos.col) == label.BLANK:
moves.append(Move(self, Position(self.pos.row + row_offset, self.pos.col)))
# Left attack
if self.state.check_cell(self.pos.row + row_offset, self.pos.col + left_offset) == \
label.get_opposite(self.color):
moves.append(Move(self, Position(self.pos.row + row_offset, self.pos.col + left_offset)))
# Right attack
if self.state.check_cell(self.pos.row + row_offset, self.pos.col + right_offset) == \
label.get_opposite(self.color):
moves.append(Move(self, Position(self.pos.row + row_offset, self.pos.col + right_offset)))
return moves
@property
def row(self):
return self.pos.row
@property
def col(self):
return self.pos.col
def __repr__(self):
return "%s at %s" % (self.color, self.pos)
class Move(object):
def __init__(self, pawn, pos):
self.pawn = pawn
self.pos = pos
def __repr__(self):
return "%s %s" % (self.pawn, self.pos)
class HexapawnState(object):
"""The game of Hexapawn."""
def __init__(self, width):
self.width = width
self.board = []
self.board.append([label.RED] * self.width)
self.board.append([label.BLANK] * self.width)
self.board.append([label.BLUE] * self.width)
self.__turn = label.RED
self.__gameover = False
self.winner = None
def get_available_moves(self):
"""Returns the set of available moves."""
pawns = []
# Scan the playable pawns on the board
for i, j in itertools.product(range(3), range(self.width)):
if self.board[i][j] == self.turn:
pawns.append(Piece(self, self.turn, Position(i, j)))
moves = []
for pawn in pawns:
for move in pawn.get_moves():
moves.append(move)
return moves
def make_move(self, move):
"""Makes a move on the game board."""
self.board[move.pawn.row][move.pawn.col] = label.BLANK
self.board[move.pos.row][move.pos.col] = move.pawn.color
def alternate_turn(self):
"""Alternates the player turn on the board."""
self.turn = label.get_opposite(self.turn)
def check_gameover(self):
"""Checks if the gameover state has occurred."""
# Check no piece end
red = 0
blue = 0
for i, j in itertools.product(range(3), range(self.width)):
if self.board[i][j] == label.RED:
red += 1
elif self.board[i][j] == label.BLUE:
blue += 1
if red == 0:
self.winner = label.BLUE
return True
elif blue == 0:
self.winner = label.RED
return True
# Check RED end line
for i in range(self.width):
if self.board[2][i] == label.RED:
self.winner = label.RED
return True
# Check BLUE end line
for i in range(self.width):
if self.board[0][i] == label.BLUE:
self.winner = label.BLUE
return True
# No moves available
if len(self.get_available_moves()) == 0:
self.winner = label.get_opposite(self.turn)
return True
def get_pawn(self, row, col, color):
if self.check_cell(row, col) == color:
return Piece(self, self.turn, Position(row, col))
else:
return None
def check_cell(self, row, col):
return -1 if row < 0 or row > (self.width - 1) or col < 0 or col > (self.width - 1) else self.board[row][col]
def draw_board(self):
"""Prints the state of board formatted with edges.
Example:
For a standard 3x3 board, prints a row of column indices, a column of
row indices, and the state of the board.
| 0 | 1 | 2 |
---------------
0 | R | R | R |
---------------
1 | | | |
---------------
2 | B | B | B |
---------------
"""
header = (str(i) for i in range(self.width))
hrule = '-' * 15
print(' |', ' | '.join(header), '|')
print(hrule)
for index, row in enumerate(self.board):
print(index, '|', ' | '.join(cell for cell in row), '|')
print(hrule)
@property
def turn(self):
return self.__turn
@turn.setter
def turn(self, value):
self.__turn = value
@property
def gameover(self):
return self.__gameover
@gameover.setter
def gameover(self, value):
self.__gameover = value
def __cmp__(self, other):
if self.board < other:
return -1
elif self.board > other:
return 1
return 0
def __str__(self):
return '\n'.join(x for x in map(lambda x: str(x), self.board))
def __repr__(self):
return '\n'.join(x for x in map(lambda x: str(x), self.board))