Esempio n. 1
0
 def test_init(self):
     with self.assertRaises(AssertionError):
         Map(width=0)
     with self.assertRaises(AssertionError):
         Map(height=0)
     with self.assertRaises(AssertionError):
         Map(width=.5)
     with self.assertRaises(AssertionError):
         Map(height=.5)
     with self.assertRaises(AssertionError):
         Map(default_field=None)
Esempio n. 2
0
 def test_get_next_field(self):
     game_map = Map(2, 1)
     bot = BotField()
     game_map[1, 0] = bot
     self.assertIs(
         game_map.get_next_field((0, 0), Orientation.EAST),
         bot
     )
     self.assertIsNone(
         game_map.get_next_field((0, 0), Orientation.NORTH)
     )
Esempio n. 3
0
    def test_get_positions_in_row(self):
        game_map = Map(width=3, height=3)

        with self.assertRaises(AssertionError):
            next(get_positions_in_row(None, (0, 0), Orientation.WEST))

        with self.assertRaises(AssertionError):
            next(get_positions_in_row(game_map, None, Orientation.WEST))

        with self.assertRaises(AssertionError):
            next(get_positions_in_row(game_map, (0, 0), None))

        with self.assertRaises(AssertionError):
            next(
                get_positions_in_row(game_map, (0, 0),
                                     Orientation.WEST,
                                     limit='foobar'))

        self.assertEqual(
            tuple(get_positions_in_row(game_map, (0, 0), Orientation.SOUTH)),
            ((0, 1), (0, 2)))

        self.assertEqual(
            tuple(get_positions_in_row(game_map, (0, 0), Orientation.WEST)),
            ())

        self.assertEqual(
            tuple(
                get_positions_in_row(game_map, (0, 0),
                                     Orientation.SOUTH,
                                     limit=1)), ((0, 1), ))
Esempio n. 4
0
 def test_setitem(self):
     game_map = Map(1, 1)
     field = EmptyField()
     game_map[0, 0] = field
     self.assertIs(game_map[0, 0], field, 'empty map')
     self.assertIsInstance(game_map[0, 0], EmptyField, 'empty map')
     with self.assertRaises(UnknownFieldError):
         game_map[0, 0] = None
Esempio n. 5
0
    def test_game_time_flags(self):
        dt = datetime.now()
        game = Game(Map(1, 1))

        self.assertAlmostEqual(
            game.created_at.timestamp(),
            dt.timestamp(),
            places=0,
            msg='created game at patched datetime'
        )
Esempio n. 6
0
    def test_action_step_on_bot(self):
        game_map = Map(width=1, height=2)
        fake_map = [
            [BotField(Orientation.SOUTH)],
            [BotField(Orientation.NORTH)],
        ]
        setattr(game_map, '_{}__map'.format(game_map.__class__.__name__), fake_map)
        game = Game(game_map)

        with self.assertRaises(MovementError):
            game.action('bot_id', Action.STEP)
Esempio n. 7
0
    def test_action_reach_treasure(self):
        game_map = Map(width=1, height=2)
        fake_map = [
            [TreasureField()],
            [BotField(Orientation.NORTH)],
        ]
        setattr(game_map, '_{}__map'.format(game_map.__class__.__name__), fake_map)
        game = Game(game_map)

        with self.assertRaises(GameFinished):
            game.action('bot_id', Action.STEP)
Esempio n. 8
0
    def test_laser_beam_bot_fight(self):
        class Conf(BaseConfiguration, RandomFieldPlacerMixin):
            map_width = 1
            map_height = 3
            bots = 1
            treasures = 0
            blocks = 0
            battery_game = True
            laser_game = True

        conf = Conf()
        game_map = Map(width=3, height=1)
        fake_map = [
            [LaserBatteryBotField(Orientation.SOUTH)],
            [EmptyField()],
            [LaserBatteryBotField(Orientation.NORTH)]
        ]
        setattr(game_map, '_{}__map'.format(game_map.__class__.__name__), fake_map)
        game = Game(game_map, configuration=conf)
        bot_id = 1
        game._bots_positions = {
            bot_id: (0, 0)
        }

        game.action(bot_id, Action.LASER_BEAM)
        self.assertIsInstance(
            game.map[0, 0],
            LaserBatteryBotField
        )
        self.assertIsInstance(
            game.map[0, 1],
            EmptyField
        )
        self.assertIsInstance(
            game.map[0, 2],
            LaserBatteryBotField
        )

        defender = game.map[0, 2]
        assert isinstance(defender, LaserBatteryBotField)
        self.assertEqual(
            LaserBatteryBotField.DEFAULT_BATTERY_LEVEL - LaserBatteryBotField.DEFAULT_LASER_DAMAGE,
            defender.actual_battery_level
        )
        attacker = game.map[0, 0]
        assert isinstance(attacker, LaserBatteryBotField)
        self.assertEqual(
            LaserBatteryBotField.DEFAULT_BATTERY_LEVEL - LaserBatteryBotField.DEFAULT_LASER_BATTERY_COST,
            attacker.actual_battery_level
        )
Esempio n. 9
0
    def test_random_conf(self):
        game_map = Map(10, 10)

        maze_field_placer = MazeFieldPlacerMixin()
        maze_field_placer.place_blocks(game_map, count=1000)

        positions = set()
        for x, y in permutations(range(1, 9, 2), 2):
            positions.add((x, y))
            positions.add((y, x))

        for position in positions:
            self.assertIsInstance(
                game_map[position],
                BlockField
            )
Esempio n. 10
0
    def test_charging_battery_in_battery_game(self):
        class Conf(BaseConfiguration, RandomFieldPlacerMixin):
            map_width = 1
            map_height = 3
            bots = 1
            treasures = 0
            blocks = 0
            battery_game = True

        conf = Conf()
        game_map = Map(width=3, height=1)
        fake_map = [
            [LaserBatteryBotField(Orientation.EAST)],
            [EmptyField()],
            [EmptyField()]
        ]
        setattr(game_map, '_{}__map'.format(game_map.__class__.__name__), fake_map)
        game = Game(game_map, configuration=conf)

        my_bot_id = 1

        game.add_bot(my_bot_id)
        bot_field = game.map[game._bots_positions[my_bot_id]]

        game.action(my_bot_id, Action.TURN_RIGHT)
        assert isinstance(bot_field, LaserBatteryBotField)
        self.assertEqual(LaserBatteryBotField.DEFAULT_BATTERY_LEVEL,
                         bot_field.actual_battery_level,
                         'Default battery level')

        game.action(my_bot_id, Action.STEP)

        self.assertEqual(LaserBatteryBotField.DEFAULT_BATTERY_LEVEL - 1,
                         bot_field.actual_battery_level,
                         'Drained battery level')

        game.action(my_bot_id, Action.WAIT)

        self.assertEqual(LaserBatteryBotField.DEFAULT_BATTERY_LEVEL,
                         bot_field.actual_battery_level,
                         'Charged battery level to default.')

        game.action(my_bot_id, Action.WAIT)

        self.assertEqual(LaserBatteryBotField.DEFAULT_BATTERY_LEVEL + 1,
                         bot_field.actual_battery_level,
                         'Charged battery level to higher level than default.')
Esempio n. 11
0
    def test__get(self):
        game_map = Map(1, 1)
        with self.assertRaises(AssertionError):
            game_map[0]
        with self.assertRaises(AssertionError):
            game_map[0, 0, 0]

        with self.assertRaises(OutOfMapError):
            game_map[0, 1]
        with self.assertRaises(OutOfMapError):
            game_map[0, 1]

        with self.assertRaises(TypeError):
            game_map[0.1, 0.1]

        with self.assertRaises(OutOfMapError):
            game_map[-1, -1]
Esempio n. 12
0
    def test_action_simple_movements(self):
        game_map = Map(width=1, height=1)
        fake_map = [
            [BotField(Orientation.EAST), EmptyField()],
            [EmptyField(), EmptyField()],
        ]
        setattr(game_map, '_{}__map'.format(game_map.__class__.__name__), fake_map)
        game = Game(game_map)
        game._empty_bots_positions = game._map.get_field_occurrences(BotField)

        self.assertFalse(game.is_filled, 'Game has free bot field.')

        fake_bot_id = 'fake_bot_id'

        game.action(fake_bot_id, Action.TURN_RIGHT)
        self.assertEqual(
            game._map[game._bots_positions[fake_bot_id]].orientation,
            Orientation.SOUTH
        )

        game.action(fake_bot_id, Action.TURN_LEFT)
        self.assertEqual(
            game.map[game._bots_positions[fake_bot_id]].orientation,
            Orientation.EAST
        )

        game.action(fake_bot_id, Action.STEP)
        self.assertEqual(
            game._bots_positions[fake_bot_id],
            (1, 0)
        )

        with self.assertRaises(MovementError):
            game.action(fake_bot_id, Action.STEP)
        self.assertEqual(
            game._bots_positions[fake_bot_id],
            (1, 0)
        )

        game.action(fake_bot_id, Action.TURN_RIGHT)

        game.action(fake_bot_id, Action.STEP)
        with self.assertRaises(MovementError):
            game.action(fake_bot_id, Action.STEP)
Esempio n. 13
0
    def test_draining_battery_in_battery_game(self):
        class Conf(BaseConfiguration, RandomFieldPlacerMixin):
            map_width = 12
            map_height = 1
            bots = 1
            treasures = 0
            blocks = 0
            battery_game = True

        conf = Conf()
        game_map = Map(width=12, height=1)
        fake_map = [
            [LaserBatteryBotField(Orientation.SOUTH)],
        ]
        fake_map.extend([[EmptyField()] for _ in range(11)])
        setattr(game_map, '_{}__map'.format(game_map.__class__.__name__), fake_map)
        game = Game(game_map, configuration=conf)

        bot_id = 1
        game.add_bot(bot_id)
        with self.assertRaises(CriticalBatteryLevel):
            while True:
                game.action(bot_id, Action.STEP)
Esempio n. 14
0
    def test_laser_beam_action_block_destroy(self):
        class Conf(BaseConfiguration, RandomFieldPlacerMixin):
            map_width = 1
            map_height = 3
            bots = 1
            treasures = 0
            blocks = 0
            battery_game = True
            laser_game = True

        conf = Conf()
        game_map = Map(width=3, height=1)
        fake_map = [
            [LaserBatteryBotField(Orientation.SOUTH)],
            [EmptyField()],
            [BlockField()]
        ]
        setattr(game_map, '_{}__map'.format(game_map.__class__.__name__), fake_map)
        game = Game(game_map, configuration=conf)
        bot_id = 1

        game.action(bot_id, Action.LASER_BEAM)
        self.assertIsInstance(
            game.map[0, 0],
            LaserBatteryBotField
        )
        self.assertIsInstance(
            game.map[0, 1],
            EmptyField
        )
        self.assertIsInstance(
            game.map[0, 2],
            EmptyField
        )
        with self.assertRaises(MovementError):
            while True:
                game.action(bot_id, Action.LASER_BEAM)
Esempio n. 15
0
 def test_export_map(self):
     game_map = Map(1, 1)
     self.assertEqual(game_map.export(), [[{FIELD_KEY: Field.EMPTY}]])
Esempio n. 16
0
 def test_getitem(self):
     game_map = Map(1, 1)
     self.assertIsInstance(game_map[0, 0], EmptyField, 'empty map')
Esempio n. 17
0
 def test__export_field(self):
     game_map = Map(1, 1)
     game_map.map[0][0] = None
     with self.assertRaises(UnknownFieldError):
         game_map._export_field(game_map[0, 0])
Esempio n. 18
0
 def test_export_map(self):
     game_map = Map(1, 1)
     self.assertEqual(game_map.export(), [[{FIELD_KEY: Field.EMPTY}]])
Esempio n. 19
0
 def test_get_next_field(self):
     game_map = Map(2, 1)
     bot = BotField()
     game_map[1, 0] = bot
     self.assertIs(game_map.get_next_field((0, 0), Orientation.EAST), bot)
     self.assertIsNone(game_map.get_next_field((0, 0), Orientation.NORTH))
Esempio n. 20
0
 def test__export_field(self):
     game_map = Map(1, 1)
     getattr(game_map,
             '_{}__map'.format(game_map.__class__.__name__))[0][0] = None
     with self.assertRaises(UnknownFieldError):
         game_map._export_field(game_map[0, 0])
Esempio n. 21
0
 def test_resolutions(self):
     game_map = Map(10, 20)
     self.assertEqual(game_map.width, 10, 'Map width')
     self.assertEqual(game_map.height, 20, 'Map height')
     self.assertIsInstance(game_map.map, list, 'Map property')
Esempio n. 22
0
 def test__export_field(self):
     game_map = Map(1, 1)
     getattr(game_map, '_{}__map'.format(game_map.__class__.__name__))[0][0] = None
     with self.assertRaises(UnknownFieldError):
         game_map._export_field(game_map[0, 0])
Esempio n. 23
0
 def test_map_iterator(self):
     game_map = Map(1, 1)
     self.assertIsInstance(iter(game_map), Iterator)
     self.assertEqual(len(list(iter(game_map))), 1)
Esempio n. 24
0
 def test_export_map(self):
     game_map = Map(1, 1)
     self.assertEqual(
         game_map.export(),
         [[Field.EMPTY]]
     )