84 lines
2.8 KiB
Python
84 lines
2.8 KiB
Python
class Board:
|
|
def __init__(self,):
|
|
self.state = self.get_initial_state()
|
|
|
|
def get_initial_state(self):
|
|
return [[0,0,0,0],[0,2,3,0],[0,3,2,0],[0,0,0,0]]
|
|
|
|
def get_active_player_code(self):
|
|
free_spaces = 0
|
|
for row in self.state:
|
|
for box in row:
|
|
if box == 0:
|
|
free_spaces += 1
|
|
if len(self.state[0])%2 == 0:
|
|
return (free_spaces%2+2)
|
|
else:
|
|
return (free_spaces%2+3)
|
|
|
|
def get_active_amazons_positions(self):
|
|
code = self.get_active_player_code()
|
|
positions=[]
|
|
for y, row in enumerate(self.state):
|
|
for x, box in enumerate(row):
|
|
if box == code:
|
|
positions.append((x, y))
|
|
return positions
|
|
|
|
def possible_moves(self):
|
|
directions = [(-1,1),(0,1),(1,1),(-1,0),(1,0),(-1,-1),(0,-1),(1,-1)]
|
|
move_options=[]
|
|
for amazon in self.get_active_amazons_positions():
|
|
amazon_move_options = self.get_reachable_squares(amazon)
|
|
for move_option in amazon_move_options:
|
|
burn_options = self.get_reachable_squares(move_option)
|
|
for burn_option in burn_options:
|
|
move_options.append((amazon, move_option, burn_option))
|
|
return move_options
|
|
|
|
def get_successor_state(self, move_and_burn):
|
|
new_state = self.state.copy()
|
|
ai, aj = [move_and_burn[0][0]], [move_and_burn[0][1]]
|
|
mi, mj = [move_and_burn[1][0]], [move_and_burn[1][1]]
|
|
bi, bj = [move_and_burn[2][0]], [move_and_burn[2][1]]
|
|
new_state[aj][ai] = 0
|
|
new_state[mj][mi] = self.get_active_player_code()
|
|
new_state[bj][bi] = 1
|
|
next_game_states.append(new_state)
|
|
return next_game_states
|
|
|
|
def get_possible_successor_states(self):
|
|
return [self.get_successor_state(move) for move in self.possible_moves()]
|
|
|
|
def is_empty(self, square):
|
|
i, j = square
|
|
if self.state[j][i] == 0:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def in_bounds(self, square):
|
|
i, j = square
|
|
if (i < 0) or (j < 0) or (i > len(self.state[0])) or (j > len(self.state[0])):
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
def get_reachable_squares(self, amazon):
|
|
directions = [(-1,1),(0,1),(1,1),(-1,0),(1,0),(-1,-1),(0,-1),(1,-1)]
|
|
reachables = []
|
|
for direction in directions:
|
|
move_option = amazon
|
|
hit_something = False
|
|
while hit_something == False:
|
|
move_option += direction
|
|
if self.in_bounds(move_option) and self.is_empty(move_option):
|
|
reachables.append(move_option)
|
|
else:
|
|
hit_something = True
|
|
return reachables
|
|
|
|
board = Board()
|
|
|
|
print(Board.possible_moves(board))
|