diff --git a/amazons/__pycache__/board.cpython-310.pyc b/amazons/__pycache__/board.cpython-310.pyc new file mode 100644 index 0000000..486e1ba Binary files /dev/null and b/amazons/__pycache__/board.cpython-310.pyc differ diff --git a/amazons/board.py b/amazons/board.py index e69de29..c7ac35b 100644 --- a/amazons/board.py +++ b/amazons/board.py @@ -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)) diff --git a/amazons/game.py b/amazons/game.py index e69de29..2a674a4 100644 --- a/amazons/game.py +++ b/amazons/game.py @@ -0,0 +1,3 @@ +import board + +possible_moves() diff --git a/amazons/test/__init__.py b/amazons/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/amazons/test/__pycache__/__init__.cpython-310.pyc b/amazons/test/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..9a71fe2 Binary files /dev/null and b/amazons/test/__pycache__/__init__.cpython-310.pyc differ diff --git a/amazons/test/__pycache__/test_board.cpython-310.pyc b/amazons/test/__pycache__/test_board.cpython-310.pyc new file mode 100644 index 0000000..1fba40e Binary files /dev/null and b/amazons/test/__pycache__/test_board.cpython-310.pyc differ diff --git a/amazons/test/test_board.py b/amazons/test/test_board.py new file mode 100644 index 0000000..d49b589 --- /dev/null +++ b/amazons/test/test_board.py @@ -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): diff --git a/amazons/view.py b/amazons/view.py index e69de29..6af282f 100644 --- a/amazons/view.py +++ b/amazons/view.py @@ -0,0 +1 @@ +import board