def main(): num_examples = 0 num_poss = 0 for i in range(10): for state in sample_game_states: playerlist = [ RandomPlayer(), RandomPlayer(), RandomPlayer(), RandomPlayer() ] game = Game(players=playerlist, game_state=state) while not game.finished(): if len(game.trick_game.tricks) < 7: if game.trick_game.current_trick.num_cards != 0: current_player = game.trick_game.get_current_player() options = game.trick_game.possible_cards( game.trick_game.current_trick, current_player.get_hand()) num_poss += len(options) num_examples += 1 game.trick_game.play_next_card() game.trick_game.trick_finished() print('Average number of legal cards to play after {} situations is : {}'. format(num_examples, num_poss / num_examples))
def sample_game_state(self, public_info): # sample opponent hands if public_info["current_trick"] is None: current_trick = Trick( leading_player_index=public_info["leading_player_index"]) else: current_trick = public_info["current_trick"] player_hands = sample_opponent_hands( tricks=public_info["tricks"], current_trick=current_trick, trumpcards=public_info["trumpcards"], playerindex=public_info["current_player_index"], player_hand=self.hand) # recreate possible mode proposals from public info mode_proposals = sample_mode_proposals(public_info) # add player_hands and possible actions to game state game_state = deepcopy(public_info) game_state["mode_proposals"] = mode_proposals game_state["player_hands"] = player_hands game = Game(game_state=game_state, players=[ RandomPlayer(), RandomPlayer(), RandomPlayer(), RandomPlayer() ]) game_state["possible_actions"] = game.get_possible_actions() return game_state
def play_standardized_tournament(playerlist): total_rewards = [0, 0, 0, 0] with open('game_states_before_bidding.p', 'rb') as f: for game_num in range(1, 101): print('------- Starting game {} ------'.format(game_num)) gamestate = pickle.load(f) # play each game four times, in a way that each player had every hand once for i in range(4): print('Round ', i) permuted_playerlist = playerlist[i:4] + playerlist[:i] game = Game(players=permuted_playerlist, game_state=gamestate) game.play() payouts = game.get_payouts() print('Game mode : ', game.bidding_game.game_mode) print('Offensive players : ', [(pl - i) % 4 for pl in game.bidding_game.offensive_players]) print('Payouts : ', [payouts[(pl - i) % 4] for pl in range(4)]) for j in range(4): total_rewards[j] += payouts[(j - i) % 4] print('\n Final Results : ', total_rewards)
def choose_card(self, card, *args): # update current game state self.playerlist[self.human_player_index].favorite_cards = [card] game = Game(players=self.playerlist, game_state=self.current_game_state) game.next_action() self.current_game_state = game.get_game_state() # display image widget_id = 'player{}_card'.format(self.human_player_index) file_path = self.get_filepath(card) self.add_widget_to_display_by_id(widget_id) self.ids[widget_id].source = file_path # remove all callbacks for btn in self.ids['player_hand'].children: self.clear_callbacks(btn) # remove played card widget self.remove_card_from_display(card) if game.trick_game.current_trick.num_cards == 0: self.finish_trick() else: self.play_next_card()
def get_new_state(self, game_state, action): playerlist = [DummyPlayer(favorite_mode=action, favorite_cards=[action]), DummyPlayer(favorite_mode=action, favorite_cards=[action]), DummyPlayer(favorite_mode=action, favorite_cards=[action]), DummyPlayer(favorite_mode=action, favorite_cards=[action])] game = Game(game_state=deepcopy(game_state), players=playerlist) game.next_action() return game.get_game_state()
def example_public_info(): game_state = sample_game_states[0] players = [ DummyPlayer(favorite_cards=[(OBER, HEARTS), (TEN, BELLS)]), DummyPlayer(favorite_cards=[(OBER, ACORNS), (KING, BELLS)]), DummyPlayer(favorite_cards=[(SEVEN, HEARTS), (ACE, BELLS)]), DummyPlayer(favorite_cards=[(ACE, HEARTS), (EIGHT, BELLS)]) ] game = Game(players=players, game_state=game_state) for _ in range(8): game.next_action() return game.get_public_info()
def make_first_opponent_proposal(self, curr_pl, *args): # play one action game = Game(players=self.playerlist, game_state=self.current_game_state) game.next_action() self.current_game_state = game.get_game_state() # set proposal text in screen last_proposal = self.current_game_state['mode_proposals'][-1] label_id = 'player{}_proposal'.format(curr_pl) if last_proposal[0] == NO_GAME: self.set_proposition_text(label_id, 'Weiter') else: self.set_proposition_text(label_id, 'I dad spuin!')
def start_bidding(self): curr_pl = self.current_game_state['current_player_index'] while curr_pl != self.human_player_index: self.make_first_opponent_proposal(curr_pl) curr_pl = self.current_game_state['current_player_index'] # set callbacks for legal actions game = Game(players=self.playerlist, game_state=self.current_game_state) legal_actions = game.get_possible_actions() for action in legal_actions: action_id = BIDDING_IDS[action] btn = self.ids[action_id] self.set_callback(btn=btn, callback=partial(self.make_first_proposal, action))
def main(): game = Game(game_state=game_state_after_bidding, players=playerlist) public_info = game.get_public_info() curr_player = game.trick_game.get_current_player() start_time = time.time() print('Start !') actions = curr_player.isuct_search(public_info=public_info) end_time = time.time() print("possible actions : ", game.get_possible_actions()) print("Best action", actions) print("Simulation took {} seconds".format(end_time - start_time))
def make_first_proposal(self, proposal, *args): self.playerlist[self.human_player_index].favorite_mode = proposal # update game_state game = Game(players=self.playerlist, game_state=self.current_game_state) game.next_action() self.current_game_state = game.get_game_state() # update screen label_id = 'player{}_proposal'.format(self.human_player_index) if proposal[0] == NO_GAME: self.set_proposition_text(label_id, 'Weiter') else: self.set_proposition_text(label_id, 'I dad spuin!') # remove bindings for btn in self.ids['mode_buttons'].children: self.clear_callbacks(btn) self.finish_first_proposals()
def continue_bidding(self, *args): curr_pl = self.current_game_state['current_player_index'] game = Game(players=self.playerlist, game_state=self.current_game_state) if curr_pl == self.human_player_index: # set callbacks for legal actions legal_actions = game.get_possible_actions() for action in legal_actions: action_id = BIDDING_IDS[action] btn = self.ids[action_id] self.set_callback(btn=btn, callback=partial(self.make_second_proposal, action)) else: curr_pl = game.get_current_player() game.next_action() self.current_game_state = game.get_game_state() label_id = 'player{}_proposal'.format(curr_pl) # update proposal text last_proposal = self.current_game_state['mode_proposals'][-1] if last_proposal[0] == NO_GAME: self.set_proposition_text(label_id, 'Weiter') elif last_proposal[0] == PARTNER_MODE: self.set_proposition_text(label_id, 'I hätt a Saupiel!') elif last_proposal[0] == WENZ: self.set_proposition_text(label_id, 'I hätt an Wenz') elif last_proposal[0] == SOLO: self.set_proposition_text(label_id, 'I hätt a Solo!') if game.bidding_game.finished(): self.prepare_trick_play() else: self.continue_bidding()
def finish_first_proposals(self): while len(self.current_game_state['mode_proposals']) < 4: curr_pl = self.current_game_state['current_player_index'] self.make_first_opponent_proposal(curr_pl) game = Game(players=playerlist, game_state=self.current_game_state) if game.bidding_game.finished(): self.prepare_trick_play() else: self.continue_bidding()
def main(): tournament_game_states = schafkopf.tournaments.game_states_trick_play.sample_game_states[: 40] total_scores = [0, 0, 0, 0] for num in range(len(tournament_game_states)): start_time = time.time() game_state = tournament_game_states[num] game = Game(game_state=game_state, players=playerlist) game.play() rewards = game.get_payouts() for i in range(4): total_scores[i] += rewards[i] end_time = time.time() print("Game {} took {} seconds".format(num + 1, end_time - start_time)) print("Rewards : ", rewards) print("Final scores : ", total_scores)
def finish_game(self, *args): # calculate winners, rewards etc. game = Game(players=self.playerlist, game_state=self.current_game_state) game_mode = self.current_game_state['game_mode'] if game_mode[0] != NO_GAME: game_mode_str = GAME_MODE_TEXTS[game_mode] final_score = game.score_offensive_players() rewards = game.get_payouts() winners = game.determine_winners() # display results on result screen result_screen = self.manager.get_screen('result_screen') if final_score > 60: result_screen.ids[ 'winners'].text = '{} gewonnen von {} mit {} Punkten'.format( game_mode_str, winners, final_score) else: result_screen.ids[ 'winners'].text = '{} gewonnen von {} mit {} Punkten'.format( game_mode_str, winners, final_score) result_screen.ids['rewards'].text = 'Auszahlung : ' + format( rewards) self.manager.current = 'result_screen' else: game_mode_str = GAME_MODE_TEXTS[game_mode] rewards = game.get_payouts() # display results on result screen result_screen = self.manager.get_screen('result_screen') result_screen.ids['winners'].text = 'Zamgschmissen' result_screen.ids['rewards'].text = 'Auszahlung : ' + format( rewards) self.manager.current = 'result_screen'
def simulation(self, selected_node): if self.simulation_player_list is None: playerlist = [RandomPlayer(), RandomPlayer(), RandomPlayer(), RandomPlayer()] # if bidding isn't over and >= 2 proposals are made, in simulation uct_player should stick with his proposal game = Game(players=playerlist, game_state=selected_node.game_state) if not game.bidding_game.finished(): player_pos = self.get_player_position(selected_node.game_state) favorite_mode = selected_node.game_state['mode_proposals'][(player_pos - game.leading_player_index) % 4] playerlist[player_pos] = DummyPlayer(favorite_mode=favorite_mode) else: playerlist = self.simulation_player_list # in case the game mode is not yet publicly declared (in bidding phase), take a random suit sim_game_state = deepcopy(selected_node.game_state) game_type = sim_game_state['game_mode'][0] game_suit = sim_game_state['game_mode'][1] if game_type == PARTNER_MODE and game_suit is None: ran_suit = random.choice([BELLS, ACORNS, LEAVES]) sim_game_state['game_mode'] = (game_type, ran_suit) # if game_type is not known yet, but at least two proposals are made: elif game_type > PARTNER_MODE and len(sim_game_state['mode_proposals']) <= 4 \ and sim_game_state['declaring_player'] != sim_game_state['current_player_index']: sim_game_state['game_mode'] = random.choice([(WENZ, None), (SOLO, ACORNS), (SOLO, HEARTS), (SOLO, BELLS), (SOLO, LEAVES)]) game_simulation = Game(players=playerlist, game_state=deepcopy(sim_game_state)) game_simulation.play() rewards = game_simulation.get_payouts() return rewards
def make_second_proposal(self, proposal, *args): self.playerlist[self.human_player_index].favorite_mode = proposal # update game_state game = Game(players=self.playerlist, game_state=self.current_game_state) game.next_action() self.current_game_state = game.get_game_state() # update screen label_id = 'player{}_proposal'.format(self.human_player_index) if proposal[0] == NO_GAME: self.set_proposition_text(label_id, 'Weiter') elif proposal[0] == PARTNER_MODE: self.set_proposition_text(label_id, 'I hätt a Sauspiel!') elif proposal[0] == WENZ: self.set_proposition_text(label_id, 'I hätt an Wenz!') elif proposal[0] == SOLO: self.set_proposition_text(label_id, 'I hätt a Solo!') # remove bindings for btn in self.ids['mode_buttons'].children: self.clear_callbacks(btn) self.continue_bidding()
def play_next_card(self, *args): game = Game(players=self.playerlist, game_state=self.current_game_state) if not game.finished(): curr_pl = game.get_current_player() if curr_pl == self.human_player_index: # set callbacks for legal actions legal_actions = game.get_possible_actions() for card in legal_actions: # determine which button corresponds to this card widget_ids = ['card_{}'.format(i) for i in range(1, 9)] for widget_id in widget_ids: btn = self.ids[widget_id] if btn.text == str(card): break assert btn self.set_callback(btn=btn, callback=partial(self.choose_card, card)) else: # play opponent game, update current game state game.next_action() self.current_game_state = game.get_game_state() # display image widget_id = 'player{}_card'.format(curr_pl) if game.trick_game.current_trick.num_cards != 0: card = self.current_game_state['current_trick'].cards[ curr_pl] else: card = self.current_game_state['tricks'][-1].cards[curr_pl] file_path = self.get_filepath(card) self.add_widget_to_display_by_id(widget_id) self.ids[widget_id].source = file_path if game.trick_game.current_trick.num_cards == 0: self.finish_trick() else: self.play_next_card() else: self.finish_game()
def prepare_new_game(self): player_hands = self.deal_cards() game_state = { "player_hands": player_hands, "leading_player_index": self.leading_player_index, "mode_proposals": [], "game_mode": (NO_GAME, None), "declaring_player": None, "tricks": [], "current_trick": None, "possible_actions": [(NO_GAME, None), (WENZ, None)] + [(PARTNER_MODE, suit) for suit in [ACORNS, LEAVES, BELLS]] + [(SOLO, suit) for suit in SUITS] } return Game(players=self.playerlist, game_state=game_state)
def solo_game(solo_player_list, game_state_solo): return Game(players=solo_player_list, game_state=game_state_solo)
def wenz_game(wenz_player_list, game_state_wenz): return Game(players=wenz_player_list, game_state=game_state_wenz)
def partner_game(partner_player_list, game_state_partner): return Game(players=partner_player_list, game_state=game_state_partner)
def test_playing(game_state_after_bidding, uct_playerlist): game = Game(game_state=game_state_after_bidding, players=uct_playerlist)