def test_get_empty_random_position(self, mocked_randint): map = Map() map.generate(20, 20, EMPTY) random_value = randint(1, 10) mocked_randint.return_value = random_value self.assertEqual(map.get_empty_random_position(), (random_value, random_value)) map.generate(20, 20, TREE) map.empty_char = EMPTY self.assertEqual(map.get_empty_random_position(), (-1, -1))
class Game: def __init__(self): self.messages = [] self.camera = Camera() self.map = Map() self.user = User('Name') self.monsters = [Monster(f'Ork{i}') for i in range(MAX_MONSTERS)] EventManager.getInstance().bind(MessageEvent.name, self, 'new_message') def generate_map(self, width, height, empty_char): self.map.generate(width, height, empty_char) def place_on_map(self, quantity, char): self.map.place(quantity, char) def run(self): self.generate_map(MAP_WIDTH, MAP_HEIGHT, EMPTY) self.place_on_map(MAX_TREES, TREE) self.place_on_map(MAX_STONES, STONE) self.place_on_map(MAX_LETTERS, LETTER) self.place_on_map(MAX_TREASURES, TREASURE) self.user.place_on(self.map) for monster in self.monsters: monster.place_on(self.map) while True: self.turn() def turn(self): self.camera.show(self.map, self.user, SMOG_RADIUS) self.show_messages() self.user.turn(self.map) if self.is_over(): raise GameOverException() for monster in self.monsters: if monster.is_dead(): monster.killed(self.map) self.monsters.remove(monster) else: monster.turn(self.map) def is_over(self): return self.user.is_dead() or self.user.has(MAX_TREASURES, TREASURE) def new_message(self, event): self.messages += event.data def show_messages(self): if len(self.messages) > 0: for message in self.messages: print(message) self.messages = []
def test_move_object(self): width, height = 20, 20 start_position = [randint(0, width - 1), randint(0, height - 1)] new_position = [randint(0, width - 1), randint(0, height - 1)] map = Map() map.generate(width, height, EMPTY) map.put(start_position[0], start_position[1], USER) self.assertEqual(map.get(start_position[0], start_position[1]), USER) self.assertEqual(map.get(new_position[0], new_position[1]), EMPTY) map.move(USER, start_position, new_position) self.assertEqual(map.get(start_position[0], start_position[1]), EMPTY) self.assertEqual(map.get(new_position[0], new_position[1]), USER) map.move(USER, new_position, [-1, -1]) self.assertEqual(map.get(new_position[0], new_position[1]), USER)
def test_apply_smog(self): map = Map() width, height = 5, 5 map.generate(width, height, EMPTY) position = [2, 2] map.map = [ [EMPTY for x in range(width)], [TREASURE for x in range(width)], [LETTER for x in range(width)], [TREE for x in range(width)], [EMPTY for x in range(width)], ] position = [2, 2] expected = [ list('#####'), list('#$$$#'), list('#@@@#'), list('#TTT#'), list('#####'), ] self.assertEqual(self.camera.apply_smog(map, position, 1), expected) position = [1, 2] expected = [ list('#####'), list('$$$##'), list('@@@##'), list('TTT##'), list('#####'), ] self.assertEqual(self.camera.apply_smog(map, position, 1), expected) position = [1, 1] expected = [ list(' ##'), list('$$$##'), list('@@@##'), list('#####'), list('#####'), ] self.assertEqual(self.camera.apply_smog(map, position, 1), expected)
def test_init_map(self): width = 20 height = 10 map = Map() map.generate(width, height, EMPTY) self.assertEqual(height, len(map.map)) self.assertEqual(width, len(choice(map.map))) self.assertEqual( EMPTY, map.get(randint(0, width - 1), randint(0, height - 1))) self.assertEqual(map.width, width) self.assertEqual(map.height, height) self.assertEqual(map.empty_char, EMPTY) width = 2 height = 5 map.generate(width, height, TREE) self.assertEqual( TREE, map.get(randint(0, width - 1), randint(0, height - 1))) self.assertEqual(map.width, width) self.assertEqual(map.height, height) self.assertEqual(map.empty_char, TREE)
def test_place_items_on_map(self): map = Map() map.generate(20, 10, EMPTY) map.place(20, TREE) self.assertEqual(map.count(TREE), 20)
def test_is_valid_pos(self): map = Map() map.generate(20, 10, EMPTY) self.assertFalse(map.is_valid_pos(-1, -1)) self.assertTrue(map.is_valid_pos(1, 1))
def test_get_item_by_xy(self): map = Map() map.generate(20, 10, EMPTY) self.assertEqual(map.get(-1, -1), STONE) self.assertEqual(map.get(1, 1), EMPTY)
def test_get_map_row(self): width, height = randint(1, 100), randint(1, 100) map = Map() map.generate(width, height, EMPTY) row = randint(0, height - 1) self.assertEqual(map.row(row), map.map[row])
def test_get_map_size(self): width, height = randint(1, 100), randint(1, 100) map = Map() map.generate(width, height, EMPTY) self.assertEqual(map.size(), [width, height])