From c0e5fe4400be4befdd704a8776ac2c3fc1702a31 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 16 Oct 2020 16:41:38 +0200 Subject: [PATCH] Get a tile from its representation --- dungeonbattle/interfaces.py | 16 ++++++++++------ dungeonbattle/interfaces_test.py | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/dungeonbattle/interfaces.py b/dungeonbattle/interfaces.py index 537b256..b7229a8 100644 --- a/dungeonbattle/interfaces.py +++ b/dungeonbattle/interfaces.py @@ -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: diff --git a/dungeonbattle/interfaces_test.py b/dungeonbattle/interfaces_test.py index 479a9ac..dc8932a 100644 --- a/dungeonbattle/interfaces_test.py +++ b/dungeonbattle/interfaces_test.py @@ -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#.")