Uploaded my most recent versions of the amazons work.

This commit is contained in:
Finn Goehrig 2023-06-28 14:22:44 -04:00
parent 86130ecc8b
commit c67ab96a52
8 changed files with 126 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,83 @@
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))

View File

@ -0,0 +1,3 @@
import board
possible_moves()

0
amazons/test/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,39 @@
from unittest import TestCase
from board import Board
class TestBoard(TestCase):
def setUp(self):
self.board = Board()
self.tiny = Board()
self.tiny.state = [[3,0], [0,2]]
def test_board_size_is_correct(self):
self.assertEqual(len(self.board.state), 4)
def test_in_bounds_works(self):
bad = [
(-1, 1), (5, 1)
]
good = [
(2, 2), (0, 0), (3, 3)
]
for square in bad:
self.assertFalse(self.board.in_bounds(square))
for square in good:
self.assertTrue(self.board.in_bounds(square))
def test_get_initial_state(self):
def test_get_active_player_code(self):
def test_get_active_amazons_positions(self):
def test_possible_moves(self):
def test_get_successor_state(self, move_and_burn):
def test_get_possible_successor_states(self):
def test_is_empty(self, square):
def test_get_reachable_squares(self, amazon):

View File

@ -0,0 +1 @@
import board