Skip to content

Commit

Permalink
Change Hexgrid._connect_cells_2d to use x,y coordinates (#2632)
Browse files Browse the repository at this point in the history
  • Loading branch information
quaquel authored Jan 20, 2025
1 parent 9e64c1b commit d71f688
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
16 changes: 8 additions & 8 deletions mesa/experimental/cell_space/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,20 +273,20 @@ class HexGrid(Grid[T]):
def _connect_cells_2d(self) -> None:
# fmt: off
even_offsets = [
(-1, -1), (-1, 0),
( 0, -1), ( 0, 1),
( 1, -1), ( 1, 0),
(-1, -1), (0, -1),
( -1, 0), ( 1, 0),
( -1, 1), (0, 1),
]
odd_offsets = [
(-1, 0), (-1, 1),
( 0, -1), ( 0, 1),
( 1, 0), ( 1, 1),
(0, -1), (1, -1),
( -1, 0), ( 1, 0),
( 0, 1), ( 1, 1),
]
# fmt: on

for cell in self.all_cells:
i = cell.coordinate[0]
offsets = even_offsets if i % 2 == 0 else odd_offsets
i = cell.coordinate[1]
offsets = even_offsets if i % 2 else odd_offsets
self._connect_single_cell_2d(cell, offsets=offsets)

def _connect_cells_nd(self) -> None:
Expand Down
34 changes: 17 additions & 17 deletions tests/test_cell_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def test_cell_neighborhood():
grid = HexGrid(
(width, height), torus=False, capacity=None, random=random.Random(42)
)
for radius, n in zip(range(1, 4), [2, 6, 11]):
for radius, n in zip(range(1, 4), [3, 7, 13]):
if radius == 1:
neighborhood = grid._cells[(0, 0)].neighborhood
else:
Expand All @@ -366,7 +366,7 @@ def test_cell_neighborhood():
grid = HexGrid(
(width, height), torus=False, capacity=None, random=random.Random(42)
)
for radius, n in zip(range(1, 4), [5, 10, 17]):
for radius, n in zip(range(1, 4), [4, 10, 17]):
if radius == 1:
neighborhood = grid._cells[(1, 0)].neighborhood
else:
Expand All @@ -385,35 +385,35 @@ def test_hexgrid():
assert len(grid._cells) == width * height

# first row
assert len(grid._cells[(0, 0)].connections.values()) == 2
assert len(grid._cells[(0, 0)].connections.values()) == 3
for connection in grid._cells[(0, 0)].connections.values():
assert connection.coordinate in {(0, 1), (1, 0)}
assert connection.coordinate in {(0, 1), (1, 0), (1, 1)}

# second row
assert len(grid._cells[(1, 0)].connections.values()) == 5
assert len(grid._cells[(1, 0)].connections.values()) == 4
for connection in grid._cells[(1, 0)].connections.values():
# fmt: off
assert connection.coordinate in {(0, 0), (0, 1),
(1, 1),
(2, 0), (2, 1)}
assert connection.coordinate in { (1, 1), (2, 1),
(0, 0), (2, 0),}
# fmt: on

# middle odd row
assert len(grid._cells[(5, 5)].connections.values()) == 6
for connection in grid._cells[(5, 5)].connections.values():
# fmt: off
assert connection.coordinate in {(4, 5), (4, 6),
(5, 4), (5, 6),
(6, 5), (6, 6)}
assert connection.coordinate in { (4, 4), (5, 4),
(4, 5), (6, 5),
(4, 6), (5, 6)}

# fmt: on

# middle even row
assert len(grid._cells[(4, 4)].connections.values()) == 6
for connection in grid._cells[(4, 4)].connections.values():
# fmt: off
assert connection.coordinate in {(3, 3), (3, 4),
(4, 3), (4, 5),
(5, 3), (5, 4)}
assert connection.coordinate in {(4, 3), (5, 3),
(3, 4), (5, 4),
(4, 5), (5, 5)}

# fmt: on

Expand All @@ -424,9 +424,9 @@ def test_hexgrid():
assert len(grid._cells[(0, 0)].connections.values()) == 6
for connection in grid._cells[(0, 0)].connections.values():
# fmt: off
assert connection.coordinate in {(9, 9), (9, 0),
(0, 9), (0, 1),
(1, 9), (1, 0)}
assert connection.coordinate in {(0, 9), (1, 9),
(9, 0), (1, 0),
(0, 1), (1, 1)}

# fmt: on

Expand Down

0 comments on commit d71f688

Please sign in to comment.