2020-12-11 17:59:07 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from squirrelbattle.interfaces import Map, Tile
|
2021-01-08 15:11:17 +00:00
|
|
|
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]))
|
|
|
|
|
|
|
|
|
2020-12-11 17:59:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestRandomWalk(unittest.TestCase):
|
|
|
|
def setUp(self) -> None:
|
2020-12-11 18:14:28 +00:00
|
|
|
#we set no_lone_walls to true for 100% coverage
|
|
|
|
params = randomwalk.DEFAULT_PARAMS
|
|
|
|
params["no_lone_walls"] = True
|
|
|
|
self.generator = randomwalk.Generator(params = params)
|
2020-12-11 17:59:07 +00:00
|
|
|
|
|
|
|
def test_starting(self) -> None:
|
|
|
|
"""
|
|
|
|
Create a map and check that the whole map is accessible from the starting position using a
|
|
|
|
depth-first search
|
|
|
|
"""
|
|
|
|
m = self.generator.run()
|
|
|
|
self.assertTrue(m.tiles[m.start_y][m.start_x].can_walk())
|
|
|
|
|
2021-01-08 15:11:17 +00:00
|
|
|
def test_connexity(self) -> None:
|
|
|
|
m = self.generator.run()
|
|
|
|
self.assertTrue(is_connex(m.tiles))
|
2021-01-08 14:55:26 +00:00
|
|
|
|
2021-01-08 15:11:17 +00:00
|
|
|
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))
|