Get a tile from its representation

This commit is contained in:
Yohann D'ANELLO 2020-10-16 16:41:38 +02:00
parent 413494ca8b
commit c0e5fe4400
2 changed files with 12 additions and 8 deletions

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python
from enum import Enum, auto
from enum import Enum
class Map:
@ -29,17 +29,21 @@ class Map:
return Map(width, height, tiles)
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):
EMPTY = auto()
WALL = auto()
FLOOR = auto()
EMPTY = ' '
WALL = '#'
FLOOR = '.'
@staticmethod
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:

View File

@ -5,7 +5,7 @@ from dungeonbattle.interfaces import Map
class TestInterfaces(unittest.TestCase):
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.height, 2)
self.assertEqual(m.draw_string(), "ab\ncd")
self.assertEqual(m.draw_string(), ".#\n#.")