Skip to content

Commit

Permalink
Merge pull request #72 from Puckoland/gridSetter
Browse files Browse the repository at this point in the history
Grid setter
  • Loading branch information
strakam authored Oct 2, 2024
2 parents f4dd9f3 + c6efed0 commit 31b122c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
17 changes: 11 additions & 6 deletions generals/core/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ class Grid:
def __init__(self, grid: str | np.ndarray):
self.grid = grid

def __eq__(self, other):
return np.array_equal(self.grid, other.grid)

@property
def grid(self):
return self._grid

@grid.setter
def grid(self, grid: str | np.ndarray):
assert isinstance(
grid, (str, np.ndarray)
), "Grid must be encoded as a string or a numpy array."
if isinstance(grid, str):
grid = grid.strip()
grid = Grid.numpify_grid(grid)
match grid:
case str(grid):
grid = grid.strip()
grid = Grid.numpify_grid(grid)
case np.ndarray():
pass
case _:
raise ValueError("Grid must be encoded as a string or a numpy array.")
if not Grid.verify_grid(grid):
raise ValueError("Invalid grid layout - generals cannot reach each other.")
self._grid = grid
Expand Down
14 changes: 14 additions & 0 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
import numpy as np


def test_grid_creation():
map = """
.....
.A##2
...2.
..22.
...B.
"""
map_nd_array = np.ndarray((5, 5), buffer=np.array(list(map.replace("\n", ""))), dtype="U1")
grid_str = Grid(map)
grid_nd_array = Grid(map_nd_array)
assert grid_str == grid_nd_array


def test_verify_grid():
map = """
.....
Expand Down

0 comments on commit 31b122c

Please sign in to comment.