Refactoring in tests to allow for easy connexity verification

This commit is contained in:
Charles Peyrat 2021-01-08 16:11:17 +01:00
parent 785ac403e3
commit 0aa4eb9c0b

View File

@ -4,7 +4,22 @@
import unittest
from squirrelbattle.interfaces import Map, Tile
from squirrelbattle.mapgeneration import randomwalk
from squirrelbattle.mapgeneration import randomwalk, broguelike
def is_connex(grid):
h, w = len(grid), len(grid[0])
y, x = randint(0, h-1), randint(0, w-1)
while not(grid[y][x].can_walk()):
y, x = randint(0, h-1), randint(0, w-1)
queue = Map.neighbourhood(grid, y, x)
while queue != []:
y, x = queue.pop()
if m.tiles[y][x].can_walk():
m.tiles[y][x] = Tile.WALL
queue += Map.neighbourhood(grid, y, x)
return not(any([any([t.can_walk() for t in l]) for l in m.tiles]))
class TestRandomWalk(unittest.TestCase):
@ -22,12 +37,14 @@ class TestRandomWalk(unittest.TestCase):
m = self.generator.run()
self.assertTrue(m.tiles[m.start_y][m.start_x].can_walk())
#DFS
queue = Map.neighbourhood(m.tiles, m.start_y, m.start_x)
while queue != []:
y, x = queue.pop()
if m.tiles[y][x].can_walk():
m.tiles[y][x] = Tile.WALL
queue += Map.neighbourhood(m.tiles, y, x)
def test_connexity(self) -> None:
m = self.generator.run()
self.assertTrue(is_connex(m.tiles))
self.assertFalse(any([any([t.can_walk() for t in l]) for l in m.tiles]))
class TestBroguelike(unittest.TestCase):
def setUp(self) -> None:
self.generator = broguelike.Generator()
def test_connexity(self) -> None:
m = self.generator.run()
self.assertTrue(is_connex(m))