コード例 #1
0
ファイル: driver.py プロジェクト: kpeng94/splendor-ai
    def __init__(self,
                 agents,
                 gamebox=None,
                 random_seed=None,
                 game_state=None,
                 game_rules=None):
        # Initialize game state.
        self._agents = agents
        if (gamebox is not None and game_state is None and game_rules is None):
            self._game_state = setup.InitializeGameState(
                gamebox, len(self._agents), random_seed)
            self._game_rules = gamebox.game_rules
        elif (gamebox is None and random_seed is None
              and game_state is not None and game_rules is not None):
            self._game_state = game_state
            self._game_rules = game_rules
        else:
            raise ValueError("Invalid driver initialization inputs")

        player_states = self._game_state.player_states
        if (len(self._agents) != len(player_states)):
            raise IndexError("Number of agents and player states don't match")
        # Verify agents are Players.
        for player_agent in self._agents:
            if not isinstance(player_agent, agent.Agent):
                raise TypeError("Agents must be of type Agent")
コード例 #2
0
 def test_CheckPlayerAction(self):
     num_players = 4
     game_state = setup.InitializeGameState(gamebox.GAMEBOX, num_players, random_seed=0)
     game_rules = gamebox.GAMEBOX.game_rules
     player_game_state = PlayerGameState(game_state, game_rules)
     result = engine.check_player_action(player_game_state, player_action.PLAYER_ACTION)
     self.assertIsNone(result)
コード例 #3
0
    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        post_raw_data = self.rfile.read(content_length)
        try:
            post_data = json.loads(post_raw_data)
        except:
            self.send_error(404, "Posted data malformed.")
            return
        if "game_state" in post_data:
            try:
                game_state = ParseGameState(post_data["game_state"])
            except:
                self.send_error(404, "GameState data malformed.")
                return
        else:
            game_state = None
        if "player_action" in post_data:
            try:
                player_action = PlayerAction(**post_data["player_action"])
            except:
                self.send_error(404, "PlayerAction data malformed.")
                return
        else:
            player_action = None

        response = {}
        if game_state is None:
            if player_action is None:
                # Return a fresh game state.
                response["game_state"] = GameStateAsDict(
                    setup.InitializeGameState(gamebox.GAMEBOX, num_players=4))
            else:
                self.send_error(
                    404, "Cannot specify PlayerAction without GameState")
                return
        else:
            if player_action is None:
                # TODO: Run an opponent agent.
                pass
            else:
                # TODO: Update game state.
                pass

        self.send_response(200)
        self.send_header('Content-type', 'application/json')
        self.end_headers()
        self.wfile.write(json.dumps(response))
コード例 #4
0
ファイル: setup_test.py プロジェクト: kpeng94/splendor-ai
    def test_InitializeGameStateTwoPlayers(self):
        num_players = 2
        game_state = setup.InitializeGameState(gamebox.GAMEBOX,
                                               num_players,
                                               random_seed=0)

        # Check that all gems are used.
        self.assertEquals(
            game_state.available_gems, {
                GemType.BLUE: 4,
                GemType.GREEN: 4,
                GemType.RED: 4,
                GemType.WHITE: 4,
                GemType.BROWN: 4,
                GemType.GOLD: 5
            })
        self._AssertAllDecksExist(game_state)
        self._AssertNumberNobles(game_state, num_players)
        self._AssertPlayerStates(game_state, num_players)
コード例 #5
0
ファイル: setup_test.py プロジェクト: kpeng94/splendor-ai
 def test_InitializeGameStateTooFewPlayers(self):
     num_players = 1
     with self.assertRaises(NotImplementedError):
         game_state = setup.InitializeGameState(gamebox.GAMEBOX,
                                                num_players,
                                                random_seed=0)