def test_tictactoe_take_win(): gm = GameMaster(get_gdl_for_game("ticTacToe")) # add two c++ players gm.add_player(get.get_player("ggtest1"), "xplayer") gm.add_player(get.get_player("ggtest1"), "oplayer") str_state = ''' (true (control xplayer)) (true (cell 2 2 x)) (true (cell 3 2 o)) (true (cell 3 3 x)) (true (cell 2 3 o)) (true (cell 3 1 b)) (true (cell 2 1 b)) (true (cell 1 3 b)) (true (cell 1 2 b)) (true (cell 1 1 b)) ''' gm.start(meta_time=30, move_time=2, initial_basestate=gm.convert_to_base_state(str_state)) # play a single move - should take win move = gm.play_single_move() assert str(move[0]) == "(mark 1 1)" assert str(move[1]) == "noop" gm.play_to_end(last_move=move) # check scores/depth make some sense assert gm.scores['xplayer'] == 100 assert gm.scores['oplayer'] == 0 assert gm.get_game_depth() == 1
def test_not_taking_win(): gm = GameMaster(get_gdl_for_game("breakthrough")) # add two players gm.add_player(PUCTPlayer(default_puct_config), "white") gm.add_player(PUCTPlayer(default_puct_config), "black") str_state = """ (true (control black)) (true (cellHolds 8 8 black)) (true (cellHolds 8 7 black)) (true (cellHolds 8 2 white)) (true (cellHolds 8 1 white)) (true (cellHolds 7 8 black)) (true (cellHolds 6 7 white)) (true (cellHolds 6 2 white)) (true (cellHolds 6 1 white)) (true (cellHolds 5 4 black)) (true (cellHolds 5 3 black)) (true (cellHolds 5 2 white)) (true (cellHolds 5 1 white)) (true (cellHolds 4 8 black)) (true (cellHolds 4 7 black)) (true (cellHolds 4 1 white)) (true (cellHolds 3 8 black)) (true (cellHolds 3 6 black)) (true (cellHolds 3 2 white)) (true (cellHolds 2 8 black)) (true (cellHolds 2 7 black)) (true (cellHolds 2 2 white)) (true (cellHolds 2 1 white)) (true (cellHolds 1 8 black)) (true (cellHolds 1 7 black)) (true (cellHolds 1 2 white)) (true (cellHolds 1 1 white)) """ gm.start(meta_time=30, move_time=5, initial_basestate=gm.convert_to_base_state(str_state)) last_move = gm.play_single_move(last_move=None) assert last_move[1] == "(move 7 8 6 7)"
class GameMasterByGame(object): def __init__(self, game_config): assert isinstance(game_config, GameConfig) self.game_config = game_config game_name = self.game_config.game_name if game_name == "breakthrough2": game_name = "breakthrough" self.gm = GameMaster(get_gdl_for_game(game_name)) # add players puct_conf = self.get_puct_config() for role in self.gm.sm.get_roles(): self.gm.add_player(puctplayer.PUCTPlayer(conf=puct_conf), role) def get_puct_config(self): multiplier = self.game_config.sims_multiplier if multiplier == 0: playouts_per_iteration = 1 else: playouts_per_iteration = 100 * multiplier conf = confs.PUCTPlayerConfig( name="clx", generation=self.game_config.generation, verbose=True, playouts_per_iteration=playouts_per_iteration, playouts_per_iteration_noop=0, dirichlet_noise_alpha=-1, root_expansions_preset_visits=-1, fpu_prior_discount=0.25, puct_before_expansions=3, puct_before_root_expansions=4, puct_constant_before=3.0, puct_constant_after=0.75, choose="choose_temperature", temperature=1.5, depth_temperature_max=self.game_config.depth_temperature_max, depth_temperature_start=4, depth_temperature_increment=0.25, depth_temperature_stop=self.game_config.depth_temperature_stop, random_scale=0.9, max_dump_depth=2) return conf def wtf(self, str_state): bs = self.gm.convert_to_base_state(str_state) nn = self.gm.get_player(0).nn print nn.predict_1(bs.to_list()) def get_move(self, str_state, depth, lead_role_index): print "GameMasterByGame.get_move", str_state self.gm.reset() self.gm.start( meta_time=120, move_time=120, initial_basestate=self.gm.convert_to_base_state(str_state), game_depth=depth) move = self.gm.play_single_move(last_move=None) player = self.gm.get_player(lead_role_index) return move[ lead_role_index], player.last_probability, self.gm.finished()