コード例 #1
0
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))
コード例 #2
0
    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()