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
1 changed files with 26 additions and 9 deletions

View File

@ -4,7 +4,22 @@
import unittest import unittest
from squirrelbattle.interfaces import Map, Tile 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): class TestRandomWalk(unittest.TestCase):
@ -22,12 +37,14 @@ class TestRandomWalk(unittest.TestCase):
m = self.generator.run() m = self.generator.run()
self.assertTrue(m.tiles[m.start_y][m.start_x].can_walk()) self.assertTrue(m.tiles[m.start_y][m.start_x].can_walk())
#DFS def test_connexity(self) -> None:
queue = Map.neighbourhood(m.tiles, m.start_y, m.start_x) m = self.generator.run()
while queue != []: self.assertTrue(is_connex(m.tiles))
y, x = queue.pop()
if m.tiles[y][x].can_walk():
m.tiles[y][x] = Tile.WALL
queue += Map.neighbourhood(m.tiles, y, x)
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))