示例#1
0
    def __init__(self,
                 players,
                 record_actions=False,
                 record_history=False,
                 print_info=False,
                 seed=None,
                 quiet=0,
                 delta_callback=None,
                 symmetric=True):
        self._players = players
        for i, player in enumerate(self._players):
            player.set_player_id(i)
        self._record_actions = record_actions
        self._record_history = record_history
        self._print_info = print_info
        if seed is None:
            seed = random.randint(0, settings.max_seed)
        self.seed = str(seed)
        self._random = random.Random(self.seed)
        self._quiet = quiet
        self._delta_callback = delta_callback
        self._state = GameState(use_start=True,
                                seed=self.seed,
                                symmetric=symmetric)

        self._actions_on_turn = {}
        self._states = {}
        self.history = []  # TODO: make private
示例#2
0
    def test_lack_of_location(self):
        state = GameState()
        state.add_robot((9, 9), 0)
        robot = state.robots[(9, 9)]

        with self.assertRaises(Exception):
            Player._validate_action(robot, ['move'])
示例#3
0
 def test_add_robot(self):
     state = GameState()
     state.add_robot((9, 9), 0, robot_id=7, hp=42)
     self.assertTrue((9, 9) in state.robots)
     self.assertEqual(state.robots[(9, 9)].player_id, 0)
     self.assertEqual(state.robots[(9, 9)].robot_id, 7)
     self.assertEqual(state.robots[(9, 9)].hp, 42)
示例#4
0
 def test_add_robot(self):
     state = GameState()
     state.add_robot((9, 9), 0, robot_id=7, hp=42)
     self.assertTrue((9, 9) in state.robots)
     self.assertEquals(state.robots[(9, 9)].player_id, 0)
     self.assertEquals(state.robots[(9, 9)].robot_id, 7)
     self.assertEquals(state.robots[(9, 9)].hp, 42)
示例#5
0
    def test_first_turn_spawning(self):
        state = GameState()

        actions = {}

        state2 = state.apply_actions(actions)

        self.assertEqual(state2.get_scores(), [settings.spawn_per_player, settings.spawn_per_player])
示例#6
0
    def test_spawn_hop_in(self):
        state = GameState()

        state.add_robot((4, 4), 0)

        actions = {(4, 4): ["move", (4, 3)]}

        state2 = state.apply_actions(actions)

        self.assertTrue(not state2.is_robot((4, 3)) or state2.robots[(4, 3)].robot_id != state.robots[4, 4].robot_id)
示例#7
0
    def test_spawn_dodge(self):
        state = GameState()

        state.add_robot((4, 3), 0)

        actions = {(4, 3): ["move", (4, 4)]}

        state2 = state.apply_actions(actions)

        self.assertTrue(state2.is_robot((4, 4)))
示例#8
0
    def test_spawn_kills(self):
        state = GameState()

        state.add_robot((4, 3), 0)

        actions = {(4, 3): ["guard"]}

        state2 = state.apply_actions(actions)

        self.assertTrue(not state2.is_robot((4, 3)) or state2.robots[(4, 3)].robot_id != state.robots[4, 3].robot_id)
示例#9
0
    def test_repr_too_long(self):
        state = GameState()
        state.add_robot((9, 9), 0)

        class long_action(list):
            def __repr__(self):
                return 'leethaxxor' * 100

        action = long_action(['guard'])
        self.assertFalse(state.is_valid_action((9, 9), action))
示例#10
0
    def test_first_turn_spawning(self):
        state = GameState()

        actions = {}

        state2 = state.apply_actions(actions)

        self.assertEqual(state2.get_scores(),
                         [settings.spawn_per_player,
                          settings.spawn_per_player])
示例#11
0
    def test_unobvious_collision(self):
        state = GameState()
        state.add_robot((9, 10), 0)
        state.add_robot((9, 12), 1)
        state.add_robot((9, 11), 0)
        state.add_robot((11, 11), 1)

        actions = {
            (9, 10): ['move', (9, 11)],
            (9, 12): ['move', (9, 11)],
            (9, 11): ['move', (10, 11)],
            (11, 11): ['move', (10, 11)]
        }

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((9, 10)))
        self.assertEqual(state2.robots[9, 10].hp, settings.robot_hp)
        self.assertTrue(state2.is_robot((9, 12)))
        self.assertEqual(state2.robots[9, 12].hp,
                         settings.robot_hp - settings.collision_damage)
        self.assertTrue(state2.is_robot((9, 11)))
        self.assertEqual(state2.robots[9, 11].hp,
                         settings.robot_hp - settings.collision_damage * 2)
        self.assertTrue(state2.is_robot((11, 11)))
        self.assertEqual(state2.robots[11, 11].hp,
                         settings.robot_hp - settings.collision_damage)
示例#12
0
    def test_train_collision(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((11, 10), 1)
        state.add_robot((12, 10), 0)
        state.add_robot((8, 10), 1)

        actions = {
            (10, 10): ['move', (9, 10)],
            (11, 10): ['move', (10, 10)],
            (12, 10): ['move', (11, 10)],
            (8, 10): ['move', (9, 10)]
        }

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((10, 10)))
        self.assertEqual(state2.robots[10, 10].hp,
                         settings.robot_hp - settings.collision_damage * 2)
        self.assertTrue(state2.is_robot((11, 10)))
        self.assertEqual(state2.robots[11, 10].hp,
                         settings.robot_hp - settings.collision_damage * 2)
        self.assertTrue(state2.is_robot((12, 10)))
        self.assertEqual(state2.robots[12, 10].hp,
                         settings.robot_hp - settings.collision_damage)
        self.assertTrue(state2.is_robot((8, 10)))
        self.assertEqual(state2.robots[8, 10].hp,
                         settings.robot_hp - settings.collision_damage)
示例#13
0
    def __init__(self, player1_path, player2_path):
        self.match_id = None

        self.player = Player(player1_path) if player1_path else None
        if self.player:
            self.player1_path = player1_path
            self.player.set_player_id(1)

        self.player2 = Player(player2_path) if player2_path else None
        if self.player2:
            self.player2_path = player2_path
            self.player2.set_player_id(0)

        self.turn_repeat = False

        self.UI = SimulatorUI(settings)
        self.UI.setTitle("Robot Game Simulator")

        self.state = GameState(settings, turn=1)
        self.cached_actions = None
        self.human_actions = {}

        self.UI.setTurn(self.state.turn)

        self.UI.bind("w", lambda ev: self.UI.moveSelection((0, -1)))
        self.UI.bind("a", lambda ev: self.UI.moveSelection((-1, 0)))
        self.UI.bind("s", lambda ev: self.UI.moveSelection((0, 1)))
        self.UI.bind("d", lambda ev: self.UI.moveSelection((1, 0)))
        self.UI.bind("<Up>", lambda ev: self.UI.moveSelection((0, -1)))
        self.UI.bind("<Left>", lambda ev: self.UI.moveSelection((-1, 0)))
        self.UI.bind("<Down>", lambda ev: self.UI.moveSelection((0, 1)))
        self.UI.bind("<Right>", lambda ev: self.UI.moveSelection((1, 0)))
        self.UI.bind("<ButtonPress-1>", lambda ev: self.UI.onMouseClick(ev))

        self.UI.bind("l", self.onLoadMatch)
        self.UI.bind("k", self.onLoadTurn)
        self.UI.bind("o", self.onReloadPlayer)
        self.UI.bind("p", self.onSwapPlayer)

        self.UI.bind("t", self.onEditTurn)
        self.UI.bind("f", self.onAddTeammate)
        self.UI.bind("e", self.onAddEnemy)
        self.UI.bind("r", self.onRemove)
        self.UI.bind("c", self.onClear)
        self.UI.bind("<Delete>", self.onRemove)
        self.UI.bind("<BackSpace>", self.onRemove)
        self.UI.bind("h", self.onEditHP)
        self.UI.bind("<space>", self.onShowActions)
        self.UI.bind("<Return>", self.onSimulate)
        self.UI.bind("n", self.onNextAction)
        self.UI.bind("g", self.onSpawnRobots)

        self.UI.run()
示例#14
0
    def test_apply_actions(self):
        state = GameState.create_from_json(self.game1)
        moves = GameState.create_actions_from_json(self.actions1)
        new_state = state.apply_actions(moves)
        info = new_state.get_game_info(json=True, seed=True)

        # TODO: Less mysterious randomization please -_-.
        r1 = random.Random('0' + 's')
        self.result1['seed'] = str(r1.randint(0, settings.max_seed))
        r2 = random.Random('0' + 'a')
        hp_left = 50 - r2.randint(*settings.attack_range)
        self.result1['robots'][0]['hp'] = hp_left

        self.assertEqual(self.sprint(self.result1), self.sprint(info))
示例#15
0
文件: move_test.py 项目: mic159/rgkit
    def test_unobvious_collision(self):
        state = GameState(settings)
        state.add_robot((9, 10), 0)
        state.add_robot((9, 12), 1)
        state.add_robot((9, 11), 0)
        state.add_robot((11, 11), 1)

        actions = {
            (9, 10): ['move', (9, 11)],
            (9, 12): ['move', (9, 11)],
            (9, 11): ['move', (10, 11)],
            (11, 11): ['move', (10, 11)]
        }

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((9, 10)))
        self.assertEqual(state2.robots[9, 10].hp, settings.robot_hp)
        self.assertTrue(state2.is_robot((9, 12)))
        self.assertEqual(state2.robots[9, 12].hp,
                         settings.robot_hp - settings.collision_damage)
        self.assertTrue(state2.is_robot((9, 11)))
        self.assertEqual(state2.robots[9, 11].hp,
                         settings.robot_hp - settings.collision_damage*2)
        self.assertTrue(state2.is_robot((11, 11)))
        self.assertEqual(state2.robots[11, 11].hp,
                         settings.robot_hp - settings.collision_damage)
示例#16
0
 def test_ok(self):
     state = GameState()
     state.add_robot((9, 9), 0)
     robot = state.robots[(9, 9)]
     Player._validate_action(robot, ['guard'])
     Player._validate_action(robot, ['suicide'])
     Player._validate_action(robot, ['guard', None])
     Player._validate_action(robot, ['suicide', (1, 1)])
     Player._validate_action(robot, ['attack', (9, 10)])
     Player._validate_action(robot, ['move', (9, 10)])
     Player._validate_action(robot, ('guard', (2, 3)))
     Player._validate_action(robot, ('suicide', None))
     Player._validate_action(robot, ('attack', (9, 10)))
     Player._validate_action(robot, ('move', (9, 10)))
示例#17
0
文件: move_test.py 项目: mic159/rgkit
    def test_train_collision(self):
        state = GameState(settings)
        state.add_robot((10, 10), 0)
        state.add_robot((11, 10), 1)
        state.add_robot((12, 10), 0)
        state.add_robot((8, 10), 1)

        actions = {
            (10, 10): ['move', (9, 10)],
            (11, 10): ['move', (10, 10)],
            (12, 10): ['move', (11, 10)],
            (8, 10): ['move', (9, 10)]
        }

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((10, 10)))
        self.assertEqual(state2.robots[10, 10].hp,
                         settings.robot_hp - settings.collision_damage*2)
        self.assertTrue(state2.is_robot((11, 10)))
        self.assertEqual(state2.robots[11, 10].hp,
                         settings.robot_hp - settings.collision_damage*2)
        self.assertTrue(state2.is_robot((12, 10)))
        self.assertEqual(state2.robots[12, 10].hp,
                         settings.robot_hp - settings.collision_damage)
        self.assertTrue(state2.is_robot((8, 10)))
        self.assertEqual(state2.robots[8, 10].hp,
                         settings.robot_hp - settings.collision_damage)
示例#18
0
    def test_apply_actions(self):
        state = GameState.create_from_json(self.game1)
        moves = GameState.create_actions_from_json(self.actions1)
        new_state = state.apply_actions(moves)
        info = new_state.get_game_info(json=True, seed=True)

        # TODO: Less mysterious randomization please -_-.
        r1 = random.Random('0' + 's')
        self.result1['seed'] = str(r1.randint(0, settings.max_seed))
        r2 = random.Random('0' + 'a')
        hp_left = 50 - r2.randint(*settings.attack_range)
        self.result1['robots'][0]['hp'] = hp_left

        self.assertEqual(self.sprint(self.result1), self.sprint(info))
示例#19
0
    def loadBotsfromTurn(self, new_turn):
        if self.match_id is not None:
            self.UI.clearActions()
            self.UI.clearBots()
            self.cached_actions = None
            self.human_actions = {}
            self.state = GameState()
            for bot in self.moves[new_turn]:
                loc = tuple(bot['location'])
                # print bot
                self.state.add_robot(loc, bot['player_id'])
                self.state.robots[loc].hp = bot['hp']
                self.UI.renderBot(loc, bot['hp'], bot['player_id'])

            self.UI.setTurn(new_turn)
            self.state.turn = new_turn
示例#20
0
    def test_load_json_game_state(self):
        # Dump and load to validate
        data = json.loads(json.dumps(self.game1))
        state = GameState.create_from_json(data)
        info = state.get_game_info(json=True, seed=True)

        self.assertEqual(self.sprint(self.game1), self.sprint(info))
示例#21
0
    def test_load_json_actions(self):
        data = json.loads(json.dumps(self.actions1))
        moves = GameState.create_actions_from_json(data)

        self.assertTrue((10, 12) in moves)
        self.assertEqual(json.dumps(moves[(10, 12)]),
                         json.dumps(['move', [10, 11]]))
示例#22
0
    def test_spawn_dodge_overwrite(self):
        state = GameState()

        state.add_robot((4, 3), 0)

        actions = {
            (4, 3): ['move', (4, 4)]
        }

        # ensure that a robot will get spawned at (4, 3)
        state._get_spawn_locations = lambda: [(i, 3) for i in range(10)]

        state2 = state.apply_actions(actions)

        self.assertTrue(state2.is_robot((4, 4)))
        self.assertTrue(state2.is_robot((4, 3)))
示例#23
0
    def test_load_json_game_state(self):
        # Dump and load to validate
        data = json.loads(json.dumps(self.game1))
        state = GameState.create_from_json(data)
        info = state.get_game_info(json=True, seed=True)

        self.assertEqual(self.sprint(self.game1), self.sprint(info))
示例#24
0
    def test_load_json_actions(self):
        data = json.loads(json.dumps(self.actions1))
        moves = GameState.create_actions_from_json(data)

        self.assertTrue((10, 12) in moves)
        self.assertEqual(json.dumps(moves[(10, 12)]),
                         json.dumps(['move', [10, 11]]))
示例#25
0
    def test_suicide(self):
        state = GameState()
        loc1 = (10, 11)
        dest1 = (10, 10)
        loc2 = (9, 10)

        state.add_robot(loc1, 0)
        state.add_robot(loc2, 1)

        actions = {loc1: ['move', dest1], loc2: ['suicide']}
        deltas = state.get_delta(actions)

        d_guard = delta_for(deltas, loc1)
        d_suicide = delta_for(deltas, loc2)

        self.assertEqual(d_guard.hp - d_guard.hp_end, d_suicide.damage_caused)
示例#26
0
    def test_attack(self):
        state = GameState()
        loc1 = (10, 11)
        dest1 = (10, 10)
        loc2 = (9, 10)
        dest2 = dest1

        state.add_robot(loc1, 0)
        state.add_robot(loc2, 1)

        actions = {loc1: ['move', dest1], loc2: ['attack', dest2]}
        deltas = state.get_delta(actions)

        d_move = delta_for(deltas, loc1)
        d_attack = delta_for(deltas, loc2)

        self.assertEqual(d_move.hp - d_move.hp_end, d_attack.damage_caused)
示例#27
0
    def test_three_collision(self):
        state = GameState()
        loc1 = (10, 11)
        loc2 = (9, 10)
        loc3 = (11, 10)

        dest = (10, 10)

        state.add_robot(loc1, 0)
        state.add_robot(loc2, 1)
        state.add_robot(loc3, 1)

        actions = {
            loc1: ['move', dest],
            loc2: ['move', dest],
            loc3: ['move', dest]
        }

        deltas = state.get_delta(actions)

        d_player0 = delta_for(deltas, loc1)
        d_player1_1 = delta_for(deltas, loc2)
        d_player1_2 = delta_for(deltas, loc3)

        self.assertEqual(d_player0.hp - d_player0.hp_end,
                         d_player1_1.damage_caused + d_player1_2.damage_caused)

        player1_hp_lose = d_player1_1.hp - d_player1_1.hp_end + \
            d_player1_2.hp - d_player1_2.hp_end

        self.assertEqual(player1_hp_lose, d_player0.damage_caused)
示例#28
0
    def test_try_move_in_circle(self):
        state = GameState()
        state.add_robot((8, 8), 0)
        state.add_robot((9, 8), 1)
        state.add_robot((9, 9), 0)
        state.add_robot((8, 9), 1)

        actions = {
            (8, 8): ['move', (9, 8)],
            (9, 8): ['move', (9, 9)],
            (9, 9): ['move', (8, 9)],
            (8, 9): ['move', (8, 8)]
        }

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((9, 8)))
        self.assertEqual(state2.robots[9, 8].hp, settings.robot_hp)
        self.assertEqual(state2.robots[9, 8].player_id, 0)
        self.assertTrue(state2.is_robot((9, 9)))
        self.assertEqual(state2.robots[9, 9].hp, settings.robot_hp)
        self.assertEqual(state2.robots[9, 9].player_id, 1)
        self.assertTrue(state2.is_robot((8, 9)))
        self.assertEqual(state2.robots[8, 9].hp, settings.robot_hp)
        self.assertEqual(state2.robots[8, 9].player_id, 0)
        self.assertTrue(state2.is_robot((8, 8)))
        self.assertEqual(state2.robots[8, 8].hp, settings.robot_hp)
        self.assertEqual(state2.robots[8, 8].player_id, 1)
示例#29
0
 def test_ok(self):
     state = GameState()
     state.add_robot((9, 9), 0)
     self.assertTrue(state.is_valid_action((9, 9), ['guard']))
     self.assertTrue(state.is_valid_action((9, 9), ['suicide']))
     self.assertTrue(state.is_valid_action((9, 9), ['attack', (9, 10)]))
     self.assertTrue(state.is_valid_action((9, 9), ['move', (9, 10)]))
示例#30
0
    def test_suicide(self):
        state = GameState()
        loc1 = (10, 11)
        dest1 = (10, 10)
        loc2 = (9, 10)

        state.add_robot(loc1, 0)
        state.add_robot(loc2, 1)

        actions = {
            loc1: ['move', dest1],
            loc2: ['suicide']
        }
        deltas = state.get_delta(actions)

        d_guard = delta_for(deltas, loc1)
        d_suicide = delta_for(deltas, loc2)

        self.assertEqual(d_guard.hp - d_guard.hp_end, d_suicide.damage_caused)
示例#31
0
    def test_simple_collision(self):
        state = GameState()
        loc1 = (10, 11)
        dest1 = (10, 10)
        loc2 = (9, 10)
        dest2 = dest1

        state.add_robot(loc1, 0)
        state.add_robot(loc2, 1)

        actions = {loc1: ['move', dest1], loc2: ['move', dest2]}

        deltas = state.get_delta(actions)

        d_move1 = delta_for(deltas, loc1)
        d_move2 = delta_for(deltas, loc2)

        self.assertEqual(d_move1.hp - d_move1.hp_end, d_move2.damage_caused)
        self.assertEqual(d_move2.hp - d_move2.hp_end, d_move1.damage_caused)
示例#32
0
    def test_attack(self):
        state = GameState()
        loc1 = (10, 11)
        dest1 = (10, 10)
        loc2 = (9, 10)
        dest2 = dest1

        state.add_robot(loc1, 0)
        state.add_robot(loc2, 1)

        actions = {
            loc1: ['move', dest1],
            loc2: ['attack', dest2]
        }
        deltas = state.get_delta(actions)

        d_move = delta_for(deltas, loc1)
        d_attack = delta_for(deltas, loc2)

        self.assertEqual(d_move.hp - d_move.hp_end, d_attack.damage_caused)
示例#33
0
    def test_try_swap(self):
        state = GameState()
        state.add_robot((9, 9), 0)
        state.add_robot((8, 9), 1)

        actions = {(9, 9): ['move', (8, 9)], (8, 9): ['move', (9, 9)]}

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((9, 9)))
        self.assertEqual(state2.robots[9, 9].player_id, 0)
        self.assertEqual(state2.robots[9, 9].robot_id,
                         state.robots[9, 9].robot_id)
        self.assertEqual(state2.robots[9, 9].hp,
                         settings.robot_hp - settings.collision_damage)
        self.assertTrue(state2.is_robot((8, 9)))
        self.assertEqual(state2.robots[8, 9].player_id, 1)
        self.assertEqual(state2.robots[8, 9].robot_id,
                         state.robots[8, 9].robot_id)
        self.assertEqual(state2.robots[8, 9].hp,
                         settings.robot_hp - settings.collision_damage)
示例#34
0
    def __init__(self, player1_path, player2_path):
        self.match_id = None

        self.player = Player(player1_path) if player1_path else None
        if self.player:
            self.player1_path = player1_path
            self.player.set_player_id(1)

        self.player2 = Player(player2_path) if player2_path else None
        if self.player2:
            self.player2_path = player2_path
            self.player2.set_player_id(0)

        self.turn_repeat = False

        self.UI = SimulatorUI(settings)
        self.UI.setTitle("Robot Game Simulator")

        self.state = GameState(settings, turn=1)
        self.cached_actions = None
        self.human_actions = {}

        self.UI.setTurn(self.state.turn)

        self.UI.bind("w", lambda ev: self.UI.moveSelection((0, -1)))
        self.UI.bind("a", lambda ev: self.UI.moveSelection((-1, 0)))
        self.UI.bind("s", lambda ev: self.UI.moveSelection((0, 1)))
        self.UI.bind("d", lambda ev: self.UI.moveSelection((1, 0)))
        self.UI.bind("<Up>", lambda ev: self.UI.moveSelection((0, -1)))
        self.UI.bind("<Left>", lambda ev: self.UI.moveSelection((-1, 0)))
        self.UI.bind("<Down>", lambda ev: self.UI.moveSelection((0, 1)))
        self.UI.bind("<Right>", lambda ev: self.UI.moveSelection((1, 0)))
        self.UI.bind("<ButtonPress-1>", lambda ev: self.UI.onMouseClick(ev))

        self.UI.bind("l", self.onLoadMatch)
        self.UI.bind("k", self.onLoadTurn)
        self.UI.bind("o", self.onReloadPlayer)
        self.UI.bind("p", self.onSwapPlayer)

        self.UI.bind("t", self.onEditTurn)
        self.UI.bind("f", self.onAddTeammate)
        self.UI.bind("e", self.onAddEnemy)
        self.UI.bind("r", self.onRemove)
        self.UI.bind("c", self.onClear)
        self.UI.bind("<Delete>", self.onRemove)
        self.UI.bind("<BackSpace>", self.onRemove)
        self.UI.bind("h", self.onEditHP)
        self.UI.bind("<space>", self.onShowActions)
        self.UI.bind("<Return>", self.onSimulate)
        self.UI.bind("n", self.onNextAction)
        self.UI.bind("g", self.onSpawnRobots)

        self.UI.run()
示例#35
0
文件: move_test.py 项目: mic159/rgkit
    def test_try_move_in_circle(self):
        state = GameState(settings)
        state.add_robot((8, 8), 0)
        state.add_robot((9, 8), 1)
        state.add_robot((9, 9), 0)
        state.add_robot((8, 9), 1)

        actions = {
            (8, 8): ['move', (9, 8)],
            (9, 8): ['move', (9, 9)],
            (9, 9): ['move', (8, 9)],
            (8, 9): ['move', (8, 8)]
        }

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((9, 8)))
        self.assertEqual(state2.robots[9, 8].hp, settings.robot_hp)
        self.assertEqual(state2.robots[9, 8].player_id, 0)
        self.assertTrue(state2.is_robot((9, 9)))
        self.assertEqual(state2.robots[9, 9].hp, settings.robot_hp)
        self.assertEqual(state2.robots[9, 9].player_id, 1)
        self.assertTrue(state2.is_robot((8, 9)))
        self.assertEqual(state2.robots[8, 9].hp, settings.robot_hp)
        self.assertEqual(state2.robots[8, 9].player_id, 0)
        self.assertTrue(state2.is_robot((8, 8)))
        self.assertEqual(state2.robots[8, 8].hp, settings.robot_hp)
        self.assertEqual(state2.robots[8, 8].player_id, 1)
示例#36
0
    def test_three_collision(self):
        state = GameState()
        loc1 = (10, 11)
        loc2 = (9, 10)
        loc3 = (11, 10)

        dest = (10, 10)

        state.add_robot(loc1, 0)
        state.add_robot(loc2, 1)
        state.add_robot(loc3, 1)

        actions = {
            loc1: ['move', dest],
            loc2: ['move', dest],
            loc3: ['move', dest]
        }

        deltas = state.get_delta(actions)

        d_player0 = delta_for(deltas, loc1)
        d_player1_1 = delta_for(deltas, loc2)
        d_player1_2 = delta_for(deltas, loc3)

        self.assertEqual(d_player0.hp - d_player0.hp_end,
                         d_player1_1.damage_caused + d_player1_2.damage_caused)

        player1_hp_lose = d_player1_1.hp - d_player1_1.hp_end + \
            d_player1_2.hp - d_player1_2.hp_end

        self.assertEqual(player1_hp_lose, d_player0.damage_caused)
示例#37
0
    def test_suicide_dodge(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((9, 10), 1)

        actions = {(10, 10): ['sucide'], (9, 10): ['move', (8, 10)]}

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((8, 10)))
        self.assertEqual(state2.robots[8, 10].hp, settings.robot_hp)
示例#38
0
    def test_simple_collision(self):
        state = GameState()
        loc1 = (10, 11)
        dest1 = (10, 10)
        loc2 = (9, 10)
        dest2 = dest1

        state.add_robot(loc1, 0)
        state.add_robot(loc2, 1)

        actions = {
            loc1: ['move', dest1],
            loc2: ['move', dest2]
        }

        deltas = state.get_delta(actions)

        d_move1 = delta_for(deltas, loc1)
        d_move2 = delta_for(deltas, loc2)

        self.assertEqual(d_move1.hp - d_move1.hp_end, d_move2.damage_caused)
        self.assertEqual(d_move2.hp - d_move2.hp_end, d_move1.damage_caused)
示例#39
0
    def test_suicide_does_no_damage_to_teammates(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((9, 10), 0)

        actions = {(10, 10): ['guard'], (9, 10): ['suicide']}

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((10, 10)))
        self.assertEqual(state2.robots[10, 10].hp, settings.robot_hp)
        self.assertFalse(state2.is_robot((9, 10)))
示例#40
0
    def test_attack_hop_in(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((8, 10), 1)

        actions = {(10, 10): ['attack', (9, 10)], (8, 10): ['move', (9, 10)]}

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((9, 10)))
        damage = settings.robot_hp - state2.robots[9, 10].hp
        self.assertTrue(
            settings.attack_range[0] <= damage <= settings.attack_range[1])
示例#41
0
    def __init__(self, settings, p1_path, p2_path):
        self.settings = settings
        self.match_id = None
        self.p1_path = p1_path
        self.p2_path = p2_path
        self.code = open(p1_path).read()
        if p2_path is None:
            self.p2_path = map_data = open(pkg_resources.resource_filename('rgkit', 'bots/guardbot.py')).read()
        self.code2 = open(p2_path).read()

        self.player = Player(self.code)
        self.player.set_player_id(1)
        self.player2 = Player(self.code2)
        self.player2.set_player_id(0)
        self.UI = SimulatorUI(settings)
        self.UI.setTitle("Robot Game Simulator")

        self.state = GameState(settings, turn=1)
        self.cached_actions = None

        self.UI.setTurn(self.state.turn)

        self.UI.bind("w", lambda ev: self.UI.moveSelection((0, -1)))
        self.UI.bind("a", lambda ev: self.UI.moveSelection((-1, 0)))
        self.UI.bind("s", lambda ev: self.UI.moveSelection((0, 1)))
        self.UI.bind("d", lambda ev: self.UI.moveSelection((1, 0)))
        self.UI.bind("<Up>", lambda ev: self.UI.moveSelection((0, -1)))
        self.UI.bind("<Left>", lambda ev: self.UI.moveSelection((-1, 0)))
        self.UI.bind("<Down>", lambda ev: self.UI.moveSelection((0, 1)))
        self.UI.bind("<Right>", lambda ev: self.UI.moveSelection((1, 0)))
        self.UI.bind("<ButtonPress-1>", lambda ev: self.UI.onMouseClick(ev))

        self.UI.bind("l", self.onLoadMatch)
        self.UI.bind("k", self.onLoadTurn)
        self.UI.bind("o", self.onReloadPlayer)
        self.UI.bind("p", self.onSwapPlayer)
        
        self.UI.bind("t", self.onEditTurn)
        self.UI.bind("f", self.onAddTeammate)
        self.UI.bind("e", self.onAddEnemy)
        self.UI.bind("r", self.onRemove)
        self.UI.bind("c", self.onClear)
        self.UI.bind("<Delete>", self.onRemove)
        self.UI.bind("<BackSpace>", self.onRemove)
        self.UI.bind("h", self.onEditHP)
        self.UI.bind("<space>", self.onShowActions)
        self.UI.bind("<Return>", self.onSimulate)

        self.UI.run()
示例#42
0
    def test_move_no_collision(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((8, 10), 0)

        actions = {(10, 10): ['move', (11, 10)], (8, 10): ['move', (7, 10)]}

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((11, 10)))
        self.assertEqual(state2.robots[11, 10].hp, settings.robot_hp)
        self.assertTrue(state2.is_robot((7, 10)))
        self.assertEqual(state2.robots[7, 10].hp, settings.robot_hp)
示例#43
0
    def test_move_into_suicide_bot(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((9, 10), 1)

        actions = {(10, 10): ['suicide'], (9, 10): ['move', (10, 10)]}

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((9, 10)))
        self.assertEqual(
            state2.robots[9, 10].hp, settings.robot_hp -
            settings.collision_damage - settings.suicide_damage)
示例#44
0
    def test_guarding_reduces_damage(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((9, 10), 1)

        actions = {(10, 10): ['guard'], (9, 10): ['suicide']}

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((10, 10)))
        self.assertEqual(state2.robots[10, 10].hp,
                         settings.robot_hp - settings.suicide_damage // 2)
        self.assertFalse(state2.is_robot((9, 10)))
示例#45
0
    def test_guarding_reduces_damage(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((9, 10), 1)

        actions = {(10, 10): ['attack', (9, 10)], (9, 10): ['guard']}

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((9, 10)))
        damage = settings.robot_hp - state2.robots[9, 10].hp
        self.assertTrue(settings.attack_range[0] //
                        2 <= damage <= settings.attack_range[1] // 2)
示例#46
0
 def loadBotsfromTurn (self, new_turn):
     if self.match_id is not None:
         self.UI.clearActions()
         self.UI.clearBots()
         self.cached_actions = None
         self.state = GameState(self.settings)
         for bot in self.moves[new_turn]:
             loc = tuple(bot['location'])
             # print bot
             self.state.add_robot(loc, bot['player_id'])
             self.state.robots[loc].hp = bot['hp']
             self.UI.renderBot(loc, bot['hp'], bot['player_id'])
             
         self.UI.setTurn(new_turn)
         self.state.turn = new_turn
示例#47
0
文件: move_test.py 项目: mic159/rgkit
    def test_try_swap(self):
        state = GameState(settings)
        state.add_robot((9, 9), 0)
        state.add_robot((8, 9), 1)

        actions = {
            (9, 9): ['move', (8, 9)],
            (8, 9): ['move', (9, 9)]
        }

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((9, 9)))
        self.assertEqual(state2.robots[9, 9].player_id, 0)
        self.assertEqual(state2.robots[9, 9].robot_id,
                         state.robots[9, 9].robot_id)
        self.assertEqual(state2.robots[9, 9].hp,
                         settings.robot_hp - settings.collision_damage)
        self.assertTrue(state2.is_robot((8, 9)))
        self.assertEqual(state2.robots[8, 9].player_id, 1)
        self.assertEqual(state2.robots[8, 9].robot_id,
                         state.robots[8, 9].robot_id)
        self.assertEqual(state2.robots[8, 9].hp,
                         settings.robot_hp - settings.collision_damage)
示例#48
0
    def test_guarding_ingnores_collisions(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((9, 10), 1)

        actions = {(10, 10): ['move', (9, 10)], (9, 10): ['guard']}

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((10, 10)))
        self.assertEqual(state2.robots[10, 10].hp,
                         settings.robot_hp - settings.collision_damage)
        self.assertTrue(state2.is_robot((9, 10)))
        self.assertEqual(state2.robots[9, 10].hp, settings.robot_hp)
示例#49
0
    def test_basic_suicide(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((9, 10), 1)

        actions = {(10, 10): ['attack', (9, 10)], (9, 10): ['suicide']}

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((10, 10)))
        self.assertEqual(state2.robots[10, 10].hp,
                         settings.robot_hp - settings.suicide_damage)

        self.assertFalse(state2.is_robot((9, 10)))
示例#50
0
    def test_attack_kills(self):
        state = GameState()
        state.add_robot((10, 10), 0, hp=settings.attack_range[0])
        state.add_robot((9, 10), 1, hp=settings.attack_range[0])

        actions = {
            (10, 10): ['attack', (9, 10)],
            (9, 10): ['attack', (10, 10)]
        }

        state2 = state.apply_actions(actions, spawn=False)

        self.assertFalse(state2.is_robot((10, 10)))
        self.assertFalse(state2.is_robot((9, 10)))
示例#51
0
    def test_suicide_dodge(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((9, 10), 1)

        actions = {
            (10, 10): ['sucide'],
            (9, 10): ['move', (8, 10)]
        }

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((8, 10)))
        self.assertEqual(state2.robots[8, 10].hp, settings.robot_hp)
示例#52
0
 def test_get_game_info(self):
     state = GameState()
     state.add_robot((9, 9), 0)
     state.add_robot((6, 11), 1)
     game_info = state.get_game_info(0)
     self.assertEquals(game_info.robots[9, 9].location, (9, 9))
     self.assertEquals(game_info.robots[9, 9].hp, state.robots[(9, 9)].hp)
     self.assertEquals(game_info.robots[9, 9].player_id, state.robots[(9, 9)].player_id)
     self.assertEquals(game_info.robots[9, 9].robot_id, state.robots[(9, 9)].robot_id)
     self.assertEquals(game_info.robots[6, 11].location, (6, 11))
     self.assertEquals(game_info.robots[6, 11].hp, state.robots[(6, 11)].hp)
     self.assertEquals(game_info.robots[6, 11].player_id, state.robots[(6, 11)].player_id)
     self.assertRaises(AttributeError, lambda: game_info.robots[6, 11].robot_id)
     self.assertEquals(game_info.turn, state.turn)
示例#53
0
    def test_suicide_does_no_damage_to_teammates(self):
        state = GameState()
        state.add_robot((10, 10), 0)
        state.add_robot((9, 10), 0)

        actions = {
            (10, 10): ['guard'],
            (9, 10): ['suicide']
        }

        state2 = state.apply_actions(actions, spawn=False)

        self.assertTrue(state2.is_robot((10, 10)))
        self.assertEqual(state2.robots[10, 10].hp, settings.robot_hp)
        self.assertFalse(state2.is_robot((9, 10)))
示例#54
0
文件: game.py 项目: ramk13/rgkit
    def __init__(self, players, record_actions=False, record_history=False,
                 print_info=False, seed=None, quiet=0, delta_callback=None,
                 symmetric=True):
        self._players = players
        for i, player in enumerate(self._players):
            player.set_player_id(i)
        self._record_actions = record_actions
        self._record_history = record_history
        self._print_info = print_info
        if seed is None:
            seed = random.randint(0, settings.max_seed)
        self.seed = str(seed)
        self._random = random.Random(self.seed)
        self._quiet = quiet
        self._delta_callback = delta_callback
        self._state = GameState(use_start=True, seed=self.seed,
                                symmetric=symmetric)

        self._actions_on_turn = {}
        self._states = {}
        self.history = []  # TODO: make private
示例#55
0
文件: game.py 项目: mueslo/rgkit
    def __init__(self, player1, player2, record_actions=False,
                 record_history=False, print_info=False,
                 seed=None, quiet=0):
        self._settings = settings
        self._player1 = player1
        self._player1.set_player_id(0)
        self._player2 = player2
        self._player2.set_player_id(1)
        self._state = GameState(self._settings, use_start=True, seed=seed)
        self._record_actions = record_actions
        self._record_history = record_history
        self._print_info = print_info
        if seed is None:
            seed = random.randint(0, sys.maxint)
        self.seed = str(seed)
        self._random = random.Random(seed)
        self._quiet = quiet

        self._actions_on_turn = {}
        self._states = {}
        self.history = []  # TODO: make private