Ejemplo n.º 1
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)
Ejemplo n.º 2
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')
Ejemplo n.º 3
0
    def test_get_knowledge_about_object_if_user_move(self, MockMap, MockBrain):
        user = User('User')
        user.brain = MockBrain

        direction = choice(user.directions)
        MockBrain.see.return_value = TREE
        user.move(direction, MockMap)
        user.brain.knowledge.assert_called_with(MockMap, user.position,
                                                direction)
Ejemplo n.º 4
0
    def test_change_position_if_object_not_barier(self, MockMap, MockBrain):
        user = User('User')
        user.brain = MockBrain

        user_position = user.position
        object_position = [randint(0, 100), randint(0, 100)]
        direction = choice(user.directions)
        user.brain.knowledge.return_value = Empty(object_position)
        user.move(direction, MockMap)
        self.assertEqual(user.position, object_position)
        MockMap.move.assert_called_with(USER, user_position, object_position)
Ejemplo n.º 5
0
    def test_can_walk_to(self):
        user = User('user')
        knowledge_about = Empty([0, 0])
        self.assertEqual(
            user.can_walk_to(DIRECTION_UP, knowledge_about),
            [DIRECTION_UP, DIRECTION_DOWN, DIRECTION_LEFT, DIRECTION_RIGHT])

        knowledge_about = Stone([0, 0])
        self.assertEqual(user.can_walk_to(DIRECTION_UP, knowledge_about),
                         [DIRECTION_DOWN, DIRECTION_LEFT, DIRECTION_RIGHT])

        knowledge_about = Treasure([0, 0])
        self.assertEqual(user.can_walk_to(DIRECTION_LEFT, knowledge_about),
                         [DIRECTION_UP, DIRECTION_DOWN, DIRECTION_RIGHT])
Ejemplo n.º 6
0
    def test_user_has_items(self):
        user = User('Name')
        self.assertTrue(user.has(0, TREASURE))
        self.assertEqual(user.inventory.count(TREASURE), 0)

        self.assertFalse(user.has(1, TREASURE))
        user.to_inventory(TREASURE)
        self.assertTrue(user.has(1, TREASURE))
Ejemplo n.º 7
0
    def test_change_direction_if_user_move(self, MockMap, MockBrain):
        user = User('User')
        user.brain = MockBrain
        self.assertEqual(user.direction, DIRECTION_UP)
        user.move(DIRECTION_DOWN, MockMap)
        self.assertEqual(user.direction, DIRECTION_DOWN)

        direction = choice(user.directions)
        user.move(direction, MockMap)
        self.assertEqual(user.direction, direction)
Ejemplo n.º 8
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 = []
Ejemplo n.º 9
0
 def test_user_put_item_to_inventory(self):
     user = User('Name')
     self.assertEqual(user.inventory.count(TREASURE), 0)
     user.to_inventory(TREASURE)
     self.assertEqual(user.inventory.count(TREASURE), 1)
Ejemplo n.º 10
0
 def test_user_has_brain(self):
     user = User('Name')
     self.assertIsInstance(user.brain, Brain)
Ejemplo n.º 11
0
 def test_user_has_health(self):
     user = User('Name')
     self.assertEqual(user.health, MAX_USER_HEALTH)
     self.assertEqual(user.max_health, MAX_MONSTER_HEALTH)
Ejemplo n.º 12
0
 def test_user_has_inventory(self):
     user = User('Name')
     self.assertIsInstance(user.inventory, list)
     self.assertEqual(len(user.inventory), 0)
Ejemplo n.º 13
0
 def test_user_has_messages_list(self):
     user = User('Name')
     self.assertIsInstance(user.messages, list)
     self.assertEqual(len(user.messages), 0)
Ejemplo n.º 14
0
 def test_add_new_message_to_users_messages(self):
     user = User('User')
     self.assertEqual(len(user.messages), 0)
     user.new_message('lorem ipsum')
     self.assertEqual(len(user.messages), 1)
     self.assertEqual(user.messages.pop(), 'lorem ipsum')
Ejemplo n.º 15
0
 def test_make_user_object(self):
     user = User('Name')
     self.assertIsNotNone(user)
     self.assertEqual(user.name, 'Name')
     self.assertEqual(user.action, '')
     self.assertEqual(user.char, USER)