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
|
2021-01-08 15:21:16 +00:00
|
|
|
from random import randint
|
2021-01-08 15:55:02 +00:00
|
|
|
from typing import List
|
2020-12-11 17:59:07 +00:00
|
|
|
|
|
|
|
from squirrelbattle.interfaces import Map, Tile
|
2021-01-08 15:16:42 +00:00
|
|
|
from squirrelbattle.mapgeneration import broguelike
|
2021-01-10 20:30:18 +00:00
|
|
|
from squirrelbattle.display.texturepack import TexturePack
|
2021-01-08 15:11:17 +00:00
|
|
|
|
2020-12-11 17:59:07 +00:00
|
|
|
|
2021-01-08 15:11:17 +00:00
|
|
|
class TestBroguelike(unittest.TestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
self.generator = broguelike.Generator()
|
2021-01-10 20:30:18 +00:00
|
|
|
self.stom = lambda x : Map.load_from_string("0 0\n" + x)
|
|
|
|
self.mtos = lambda x : x.draw_string(TexturePack.ASCII_PACK)
|
|
|
|
|
|
|
|
def test_dist(self):
|
|
|
|
m = self.stom(".. ..\n ... ")
|
|
|
|
distance = broguelike.dist(m.tiles, 0, 0, 0, 4)
|
|
|
|
self.assertEqual(distance, 6)
|
2021-01-10 20:32:58 +00:00
|
|
|
m = self.stom(". .")
|
|
|
|
distance = broguelike.dist(m.tiles, 0, 0, 0, 2)
|
|
|
|
self.assertEqual(distance, -1)
|
2021-01-08 15:16:42 +00:00
|
|
|
|
2021-01-08 15:55:02 +00:00
|
|
|
def is_connex(self, grid: List[List[Tile]]) -> bool:
|
|
|
|
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 grid[y][x].can_walk():
|
|
|
|
grid[y][x] = Tile.WALL
|
|
|
|
queue += Map.neighbourhood(grid, y, x)
|
|
|
|
return not any([t.can_walk() for row in grid for t in row])
|
|
|
|
|
2021-01-10 20:48:12 +00:00
|
|
|
def test_build_doors(self):
|
|
|
|
m = self.stom(". .\n. .\n. .\n")
|
|
|
|
self.assertFalse(self.generator.build_door(m.tiles, 1, 1, 0, 1, 2))
|
|
|
|
|
2021-01-08 15:11:17 +00:00
|
|
|
def test_connexity(self) -> None:
|
|
|
|
m = self.generator.run()
|
2021-01-08 15:55:02 +00:00
|
|
|
self.assertTrue(self.is_connex(m.tiles))
|
2021-01-10 20:30:18 +00:00
|
|
|
|
|
|
|
def test_loops(self) -> None:
|
|
|
|
m = self.stom(3*".. ..\n")
|
|
|
|
self.generator.add_loop(m.tiles, 1, 3)
|
|
|
|
s = self.mtos(m)
|
|
|
|
self.assertEqual(s, ".. ..\n.......\n.. ..")
|
|
|
|
self.assertFalse(self.generator.add_loop(m.tiles, 0, 0))
|
|
|
|
m = self.stom("...\n. .\n...")
|
|
|
|
self.assertFalse(self.generator.add_loop(m.tiles, 1, 1))
|
|
|
|
|