Get a tile from its representation
This commit is contained in:
parent
413494ca8b
commit
c0e5fe4400
|
@ -1,5 +1,5 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from enum import Enum, auto
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
class Map:
|
class Map:
|
||||||
|
@ -29,17 +29,21 @@ class Map:
|
||||||
return Map(width, height, tiles)
|
return Map(width, height, tiles)
|
||||||
|
|
||||||
def draw_string(self) -> str:
|
def draw_string(self) -> str:
|
||||||
return "\n".join("".join(tile.char for tile in line) for line in self.tiles)
|
return "\n".join("".join(tile.value for tile in line)
|
||||||
|
for line in self.tiles)
|
||||||
|
|
||||||
|
|
||||||
class Tile(Enum):
|
class Tile(Enum):
|
||||||
EMPTY = auto()
|
EMPTY = ' '
|
||||||
WALL = auto()
|
WALL = '#'
|
||||||
FLOOR = auto()
|
FLOOR = '.'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_char(c: str):
|
def from_char(c: str):
|
||||||
return {'#': Tile.WALL, '.': Tile.FLOOR, ' ': Tile.EMPTY}[c]
|
for t in Tile:
|
||||||
|
if t.value == c:
|
||||||
|
return t
|
||||||
|
raise ValueError(f"Tile not found : {c}")
|
||||||
|
|
||||||
|
|
||||||
class Entity:
|
class Entity:
|
||||||
|
|
|
@ -5,7 +5,7 @@ from dungeonbattle.interfaces import Map
|
||||||
|
|
||||||
class TestInterfaces(unittest.TestCase):
|
class TestInterfaces(unittest.TestCase):
|
||||||
def test_map(self) -> None:
|
def test_map(self) -> None:
|
||||||
m = Map.load_from_string("ab\ncd\n")
|
m = Map.load_from_string(".#\n#.\n")
|
||||||
self.assertEqual(m.width, 2)
|
self.assertEqual(m.width, 2)
|
||||||
self.assertEqual(m.height, 2)
|
self.assertEqual(m.height, 2)
|
||||||
self.assertEqual(m.draw_string(), "ab\ncd")
|
self.assertEqual(m.draw_string(), ".#\n#.")
|
||||||
|
|
Loading…
Reference in New Issue