Skip to content

Commit d71f688

Browse files
authored
Change Hexgrid._connect_cells_2d to use x,y coordinates (#2632)
1 parent 9e64c1b commit d71f688

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

mesa/experimental/cell_space/grid.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,20 +273,20 @@ class HexGrid(Grid[T]):
273273
def _connect_cells_2d(self) -> None:
274274
# fmt: off
275275
even_offsets = [
276-
(-1, -1), (-1, 0),
277-
( 0, -1), ( 0, 1),
278-
( 1, -1), ( 1, 0),
276+
(-1, -1), (0, -1),
277+
( -1, 0), ( 1, 0),
278+
( -1, 1), (0, 1),
279279
]
280280
odd_offsets = [
281-
(-1, 0), (-1, 1),
282-
( 0, -1), ( 0, 1),
283-
( 1, 0), ( 1, 1),
281+
(0, -1), (1, -1),
282+
( -1, 0), ( 1, 0),
283+
( 0, 1), ( 1, 1),
284284
]
285285
# fmt: on
286286

287287
for cell in self.all_cells:
288-
i = cell.coordinate[0]
289-
offsets = even_offsets if i % 2 == 0 else odd_offsets
288+
i = cell.coordinate[1]
289+
offsets = even_offsets if i % 2 else odd_offsets
290290
self._connect_single_cell_2d(cell, offsets=offsets)
291291

292292
def _connect_cells_nd(self) -> None:

tests/test_cell_space.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def test_cell_neighborhood():
354354
grid = HexGrid(
355355
(width, height), torus=False, capacity=None, random=random.Random(42)
356356
)
357-
for radius, n in zip(range(1, 4), [2, 6, 11]):
357+
for radius, n in zip(range(1, 4), [3, 7, 13]):
358358
if radius == 1:
359359
neighborhood = grid._cells[(0, 0)].neighborhood
360360
else:
@@ -366,7 +366,7 @@ def test_cell_neighborhood():
366366
grid = HexGrid(
367367
(width, height), torus=False, capacity=None, random=random.Random(42)
368368
)
369-
for radius, n in zip(range(1, 4), [5, 10, 17]):
369+
for radius, n in zip(range(1, 4), [4, 10, 17]):
370370
if radius == 1:
371371
neighborhood = grid._cells[(1, 0)].neighborhood
372372
else:
@@ -385,35 +385,35 @@ def test_hexgrid():
385385
assert len(grid._cells) == width * height
386386

387387
# first row
388-
assert len(grid._cells[(0, 0)].connections.values()) == 2
388+
assert len(grid._cells[(0, 0)].connections.values()) == 3
389389
for connection in grid._cells[(0, 0)].connections.values():
390-
assert connection.coordinate in {(0, 1), (1, 0)}
390+
assert connection.coordinate in {(0, 1), (1, 0), (1, 1)}
391391

392392
# second row
393-
assert len(grid._cells[(1, 0)].connections.values()) == 5
393+
assert len(grid._cells[(1, 0)].connections.values()) == 4
394394
for connection in grid._cells[(1, 0)].connections.values():
395395
# fmt: off
396-
assert connection.coordinate in {(0, 0), (0, 1),
397-
(1, 1),
398-
(2, 0), (2, 1)}
396+
assert connection.coordinate in { (1, 1), (2, 1),
397+
(0, 0), (2, 0),}
398+
# fmt: on
399399

400400
# middle odd row
401401
assert len(grid._cells[(5, 5)].connections.values()) == 6
402402
for connection in grid._cells[(5, 5)].connections.values():
403403
# fmt: off
404-
assert connection.coordinate in {(4, 5), (4, 6),
405-
(5, 4), (5, 6),
406-
(6, 5), (6, 6)}
404+
assert connection.coordinate in { (4, 4), (5, 4),
405+
(4, 5), (6, 5),
406+
(4, 6), (5, 6)}
407407

408408
# fmt: on
409409

410410
# middle even row
411411
assert len(grid._cells[(4, 4)].connections.values()) == 6
412412
for connection in grid._cells[(4, 4)].connections.values():
413413
# fmt: off
414-
assert connection.coordinate in {(3, 3), (3, 4),
415-
(4, 3), (4, 5),
416-
(5, 3), (5, 4)}
414+
assert connection.coordinate in {(4, 3), (5, 3),
415+
(3, 4), (5, 4),
416+
(4, 5), (5, 5)}
417417

418418
# fmt: on
419419

@@ -424,9 +424,9 @@ def test_hexgrid():
424424
assert len(grid._cells[(0, 0)].connections.values()) == 6
425425
for connection in grid._cells[(0, 0)].connections.values():
426426
# fmt: off
427-
assert connection.coordinate in {(9, 9), (9, 0),
428-
(0, 9), (0, 1),
429-
(1, 9), (1, 0)}
427+
assert connection.coordinate in {(0, 9), (1, 9),
428+
(9, 0), (1, 0),
429+
(0, 1), (1, 1)}
430430

431431
# fmt: on
432432

0 commit comments

Comments
 (0)