Пример #1
0
 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')
Пример #2
0
 def test_clear_position(self):
     width, height = 20, 20
     start_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)
     map.clear(start_position)
     self.assertEqual(map.get(start_position[0], start_position[1]),
                      map.empty_char)
Пример #3
0
 def test_check_if_position_contain_char(self):
     map = Map()
     map.generate(20, 10, TREE)
     map.put(10, 5, EMPTY)
     self.assertTrue(map.check(10, 5, EMPTY))
     self.assertFalse(map.check(10, 6, EMPTY))
     self.assertTrue(map.check(10, 6, TREE))
Пример #4
0
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 = []
Пример #5
0
    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))
Пример #6
0
    def test_put_item_on_map(self):
        x, y = randint(0, 20 - 1), randint(0, 10 - 1)
        map = Map()
        map.generate(20, 10, EMPTY)

        self.assertEqual(EMPTY, map.get(x, y))
        map.put(x, y, TREE)
        self.assertEqual(TREE, map.get(x, y))
Пример #7
0
 def test_user_do(self, MockTreasure):
     user = User('User')
     map = Map()
     user.action = PICK_UP
     MockTreasure.can_do.return_value = [PICK_UP]
     user.do(map, MockTreasure)
     MockTreasure.do.asssert_called_with(user, user.action, map)
Пример #8
0
 def test_make_map(self):
     map = Map()
     self.assertIsNotNone(map)
     self.assertIsNotNone(map.map)
     self.assertIsInstance(map.map, list)
     self.assertEqual(map.width, 0)
     self.assertEqual(map.height, 0)
     self.assertEqual(map.empty_char, '')
Пример #9
0
    def test_calculate_items_on_map(self):
        map = Map()
        map.generate(20, 10, EMPTY)
        count = 0
        for x, y in [[0, 0], [3, 4], [5, 6]]:
            count += 1
            map.put(x, y, TREE)

        self.assertEqual(map.count(TREE), count)
        self.assertEqual(map.count(EMPTY), 20 * 10 - count)
Пример #10
0
 def test_get_max_quantity_of_chars(self):
     width, height = 10, 10
     map = Map()
     map.generate(width, height, EMPTY)
     self.assertEqual(map.get_max_quantity(1), 1)
     self.assertEqual(map.get_max_quantity(width * height), width * height)
     self.assertEqual(map.get_max_quantity(width * height + 1),
                      width * height)
Пример #11
0
    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)
Пример #12
0
    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)
Пример #13
0
    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)
Пример #14
0
 def test_calculate_position_by_xy_and_direction(self):
     map = Map()
     self.assertEqual(map.calculate_position(1, 1, DIRECTION_UP), (1, 0))
     self.assertEqual(map.calculate_position(1, 1, DIRECTION_DOWN), (1, 2))
     self.assertEqual(map.calculate_position(1, 1, DIRECTION_LEFT), (0, 1))
     self.assertEqual(map.calculate_position(1, 1, DIRECTION_RIGHT), (2, 1))
Пример #15
0
 def test_place_items_on_map(self):
     map = Map()
     map.generate(20, 10, EMPTY)
     map.place(20, TREE)
     self.assertEqual(map.count(TREE), 20)
Пример #16
0
 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])
Пример #17
0
 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])
Пример #18
0
 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))
Пример #19
0
 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)