Пример #1
0
def simulate_game_step(game_elements, num_active_players, num_die_rolls,
                       current_player_index):

    current_player = game_elements['players'][current_player_index]
    ##################################################################################################################
    while current_player.status == 'lost':
        current_player_index += 1
        current_player_index = current_player_index % len(
            game_elements['players'])
        current_player = game_elements['players'][current_player_index]

    # set current move to current player
    current_player.status = 'current_move'
    # pre-roll for current player + out-of-turn moves for everybody else,
    # till we get num_active_players skip turns in a row.
    skip_turn = 0
    # make make_pre_roll_moves for current player -> player has allowable actions and then call agent.pre-roll-move
    if current_player.make_pre_roll_moves(
            game_elements
    ) == 2:  # 2 is the special skip-turn code #in player.py
        skip_turn += 1

    out_of_turn_player_index = current_player_index + 1
    out_of_turn_count = 0
    while skip_turn != num_active_players and out_of_turn_count <= 200:
        out_of_turn_count += 1
        # print 'checkpoint 1'
        out_of_turn_player = game_elements['players'][
            out_of_turn_player_index % len(game_elements['players'])]
        if out_of_turn_player.status == 'lost':
            out_of_turn_player_index += 1
            continue
        oot_code = out_of_turn_player.make_out_of_turn_moves(game_elements)
        # add to game history
        game_elements['history']['function'].append(
            out_of_turn_player.make_out_of_turn_moves)
        params = dict()
        params['self'] = out_of_turn_player
        params['current_gameboard'] = game_elements
        game_elements['history']['param'].append(params)
        game_elements['history']['return'].append(oot_code)

        if oot_code == 2:
            skip_turn += 1
        else:
            skip_turn = 0
        out_of_turn_player_index += 1
##################################################################################################################
# now we roll the dice and get into the post_roll phase,
# but only if we're not in jail.

    r = roll_die(game_elements['dies'], np.random.choice)
    # add to game history
    game_elements['history']['function'].append(roll_die)
    params = dict()
    params['die_objects'] = game_elements['dies']
    params['choice'] = np.random.choice
    game_elements['history']['param'].append(params)
    game_elements['history']['return'].append(r)

    num_die_rolls += 1
    game_elements['current_die_total'] = sum(r)
    print 'dies have come up ', str(r)
    if not current_player.currently_in_jail:
        check_for_go = True
        move_player_after_die_roll(current_player, sum(r), game_elements,
                                   check_for_go)
        # add to game history
        game_elements['history']['function'].append(move_player_after_die_roll)
        params = dict()
        params['player'] = current_player
        params['rel_move'] = sum(r)
        params['current_gameboard'] = game_elements
        params['check_for_go'] = check_for_go
        game_elements['history']['param'].append(params)
        game_elements['history']['return'].append(None)

        current_player.process_move_consequences(game_elements)
        # add to game history
        game_elements['history']['function'].append(
            current_player.process_move_consequences)
        params = dict()
        params['self'] = current_player
        params['current_gameboard'] = game_elements
        game_elements['history']['param'].append(params)
        game_elements['history']['return'].append(None)

        # post-roll for current player. No out-of-turn moves allowed at this point.
        current_player.make_post_roll_moves(game_elements, [])

        # add to game history
        game_elements['history']['function'].append(
            current_player.make_post_roll_moves)
        params = dict()
        params['self'] = current_player
        params['current_gameboard'] = game_elements
        game_elements['history']['param'].append(params)
        game_elements['history']['return'].append(None)

    else:
        current_player.currently_in_jail = False  # the player is only allowed to skip one turn (i.e. this one)

    if current_player.current_cash < 0:
        code = current_player.handle_negative_cash_balance(
            current_player, game_elements)
        # add to game history
        game_elements['history']['function'].append(
            current_player.handle_negative_cash_balance)
        params = dict()
        params['player'] = current_player
        params['current_gameboard'] = game_elements
        game_elements['history']['param'].append(params)
        game_elements['history']['return'].append(code)
        if code == -1 or current_player.current_cash < 0:
            current_player.begin_bankruptcy_proceedings(game_elements)
            # add to game history
            game_elements['history']['function'].append(
                current_player.begin_bankruptcy_proceedings)
            params = dict()
            params['self'] = current_player
            params['current_gameboard'] = game_elements
            game_elements['history']['param'].append(params)
            game_elements['history']['return'].append(None)

            num_active_players -= 1
            diagnostics.print_asset_owners(game_elements)
            diagnostics.print_player_cash_balances(game_elements)

            if num_active_players == 1:
                for p in game_elements['players']:
                    if p.status != 'lost':
                        winner = p
                        p.status = 'won'
    else:
        current_player.status = 'waiting_for_move'

    current_player_index = (current_player_index + 1) % len(
        game_elements['players'])

    if diagnostics.max_cash_balance(
            game_elements
    ) > 300000:  # this is our limit for runaway cash for testing purposes only.
        # We print some diagnostics and return if any player exceeds this.
        diagnostics.print_asset_owners(game_elements)
        diagnostics.print_player_cash_balances(game_elements)
        return
    return game_elements, num_active_players, num_die_rolls, current_player_index
Пример #2
0
def simulate_game_instance(game_elements, history_log_file=None, np_seed=6):
    """
    Simulate a game instance.
    :param game_elements: The dict output by set_up_board
    :param np_seed: The numpy seed to use to control randomness.
    :return: None
    """
    np.random.seed(np_seed)
    np.random.shuffle(game_elements['players'])
    game_elements['seed'] = np_seed
    game_elements['card_seed'] = np_seed
    game_elements['choice_function'] = np.random.choice

    num_die_rolls = 0
    # game_elements['go_increment'] = 100 # we should not be modifying this here. It is only for testing purposes.
    # One reason to modify go_increment is if your decision agent is not aggressively trying to monopolize. Since go_increment
    # by default is 200 it can lead to runaway cash increases for simple agents like ours.

    print 'players will play in the following order: ','->'.join([p.player_name for p in game_elements['players']])
    print 'Beginning play. Rolling first die...'
    current_player_index = 0
    num_active_players = 4
    winner = None
    workbook = None
    if history_log_file:
        workbook = xlsxwriter.Workbook(history_log_file)

    while num_active_players > 1:
        current_player = game_elements['players'][current_player_index]
        while current_player.status == 'lost':
            current_player_index += 1
            current_player_index = current_player_index % len(game_elements['players'])
            current_player = game_elements['players'][current_player_index]
        current_player.status = 'current_move'

        # pre-roll for current player + out-of-turn moves for everybody else,
        # till we get num_active_players skip turns in a row.

        skip_turn = 0
        if current_player.make_pre_roll_moves(game_elements) == 2: # 2 is the special skip-turn code
            skip_turn += 1
        out_of_turn_player_index = current_player_index + 1
        out_of_turn_count = 0
        while skip_turn != num_active_players and out_of_turn_count<=200:
            out_of_turn_count += 1
            # print 'checkpoint 1'
            out_of_turn_player = game_elements['players'][out_of_turn_player_index%len(game_elements['players'])]
            if out_of_turn_player.status == 'lost':
                out_of_turn_player_index += 1
                continue
            oot_code = out_of_turn_player.make_out_of_turn_moves(game_elements)
            # add to game history
            game_elements['history']['function'].append(out_of_turn_player.make_out_of_turn_moves)
            params = dict()
            params['self']=out_of_turn_player
            params['current_gameboard']=game_elements
            game_elements['history']['param'].append(params)
            game_elements['history']['return'].append(oot_code)

            if  oot_code == 2:
                skip_turn += 1
            else:
                skip_turn = 0
            out_of_turn_player_index += 1

        # now we roll the dice and get into the post_roll phase,
        # but only if we're not in jail.


        r = roll_die(game_elements['dies'], np.random.choice)
        # add to game history
        game_elements['history']['function'].append(roll_die)
        params = dict()
        params['die_objects'] = game_elements['dies']
        params['choice'] = np.random.choice
        game_elements['history']['param'].append(params)
        game_elements['history']['return'].append(r)

        num_die_rolls += 1
        game_elements['current_die_total'] = sum(r)
        print 'dies have come up ',str(r)
        if not current_player.currently_in_jail:
            check_for_go = True
            move_player_after_die_roll(current_player, sum(r), game_elements, check_for_go)
            # add to game history
            game_elements['history']['function'].append(move_player_after_die_roll)
            params = dict()
            params['player'] = current_player
            params['rel_move'] = sum(r)
            params['current_gameboard'] = game_elements
            params['check_for_go'] = check_for_go
            game_elements['history']['param'].append(params)
            game_elements['history']['return'].append(None)

            current_player.process_move_consequences(game_elements)
            # add to game history
            game_elements['history']['function'].append(current_player.process_move_consequences)
            params = dict()
            params['self'] = current_player
            params['current_gameboard'] = game_elements
            game_elements['history']['param'].append(params)
            game_elements['history']['return'].append(None)

            # post-roll for current player. No out-of-turn moves allowed at this point.
            current_player.make_post_roll_moves(game_elements)
            # add to game history
            game_elements['history']['function'].append(current_player.make_post_roll_moves)
            params = dict()
            params['self'] = current_player
            params['current_gameboard'] = game_elements
            game_elements['history']['param'].append(params)
            game_elements['history']['return'].append(None)

        else:
            current_player.currently_in_jail = False # the player is only allowed to skip one turn (i.e. this one)

        if current_player.current_cash < 0:
            code = current_player.handle_negative_cash_balance(current_player, game_elements)
            # add to game history
            game_elements['history']['function'].append(current_player.handle_negative_cash_balance)
            params = dict()
            params['player'] = current_player
            params['current_gameboard'] = game_elements
            game_elements['history']['param'].append(params)
            game_elements['history']['return'].append(code)
            if code == -1 or current_player.current_cash < 0:
                current_player.begin_bankruptcy_proceedings(game_elements)
                # add to game history
                game_elements['history']['function'].append(current_player.begin_bankruptcy_proceedings)
                params = dict()
                params['self'] = current_player
                params['current_gameboard'] = game_elements
                game_elements['history']['param'].append(params)
                game_elements['history']['return'].append(None)

                num_active_players -= 1
                diagnostics.print_asset_owners(game_elements)
                diagnostics.print_player_cash_balances(game_elements)

                if num_active_players == 1:
                    for p in game_elements['players']:
                        if p.status != 'lost':
                            winner = p
                            p.status = 'won'
        else:
            current_player.status = 'waiting_for_move'

        current_player_index = (current_player_index+1)%len(game_elements['players'])

        if diagnostics.max_cash_balance(game_elements) > 300000: # this is our limit for runaway cash for testing purposes only.
                                                                 # We print some diagnostics and return if any player exceeds this.
            diagnostics.print_asset_owners(game_elements)
            diagnostics.print_player_cash_balances(game_elements)
            return
    if workbook:
        write_history_to_file(game_elements, workbook)
    # let's print some numbers
    print 'printing final asset owners: '
    diagnostics.print_asset_owners(game_elements)
    print 'number of dice rolls: ',str(num_die_rolls)
    print 'printing final cash balances: '
    diagnostics.print_player_cash_balances(game_elements)

    if winner:
        print 'We have a winner: ', winner.player_name

    return
Пример #3
0
def after_agent(game_elements, num_active_players, num_die_rolls,
                current_player_index, actions_vector, a, params):
    current_player = game_elements['players'][current_player_index]
    if not current_player.currently_in_jail:
        #got state and masked actions => agent => output actions and move
        #action vector => actions
        move_actions = a.vector_to_actions(params['current_gameboard'],
                                           current_player, actions_vector)
        print 'move_actions =====>', move_actions
        current_player.make_post_roll_moves(game_elements, move_actions)
        #####################################################################

        # add to game history
        game_elements['history']['function'].append(
            current_player.make_post_roll_moves)
        params = dict()
        params['self'] = current_player
        params['current_gameboard'] = game_elements
        game_elements['history']['param'].append(params)
        game_elements['history']['return'].append(None)

    else:
        current_player.currently_in_jail = False  # the player is only allowed to skip one turn (i.e. this one)

    if current_player.current_cash < 0:
        code = current_player.handle_negative_cash_balance(
            current_player, game_elements)
        # add to game history
        game_elements['history']['function'].append(
            current_player.handle_negative_cash_balance)
        params = dict()
        params['player'] = current_player
        params['current_gameboard'] = game_elements
        game_elements['history']['param'].append(params)
        game_elements['history']['return'].append(code)
        if code == -1 or current_player.current_cash < 0:
            current_player.begin_bankruptcy_proceedings(game_elements)
            # add to game history
            game_elements['history']['function'].append(
                current_player.begin_bankruptcy_proceedings)
            params = dict()
            params['self'] = current_player
            params['current_gameboard'] = game_elements
            game_elements['history']['param'].append(params)
            game_elements['history']['return'].append(None)

            num_active_players -= 1
            diagnostics.print_asset_owners(game_elements)
            diagnostics.print_player_cash_balances(game_elements)

            if num_active_players == 1:
                for p in game_elements['players']:
                    if p.status != 'lost':
                        winner = p
                        p.status = 'won'
    else:
        current_player.status = 'waiting_for_move'

    current_player_index = (current_player_index + 1) % len(
        game_elements['players'])

    if diagnostics.max_cash_balance(
            game_elements
    ) > 300000:  # this is our limit for runaway cash for testing purposes only.
        # We print some diagnostics and return if any player exceeds this.
        diagnostics.print_asset_owners(game_elements)
        diagnostics.print_player_cash_balances(game_elements)
        return
    return game_elements, num_active_players, num_die_rolls, current_player_index
Пример #4
0
    def simulate_game_instance(self):
        """
        Simulate a game instance.
        :param game_elements: The dict output by set_up_board
        :param np_seed: The numpy seed to use to control randomness.
        :return: None
        """
        np.random.seed(np_seed)
        np.random.shuffle(self.game_elem['players'])
        self.game_elem['seed'] = np_seed
        self.game_elem['card_seed'] = np_seed
        self.game_elem['choice_function'] = np.random.choice
        randstate1 = np.random.RandomState(seed=np_seed)
        randstate2 = np.random.RandomState(seed=np_seed)
        self.game_elem['chance_choice_function'] = randstate1.choice
        self.game_elem['cc_choice_function'] = randstate2.choice
        num_die_rolls = 0
        # game_elements['go_increment'] = 100 # we should not be modifying this here. It is only for testing purposes.
        # One reason to modify go_increment is if your decision agent is not aggressively trying to monopolize. Since go_increment
        # by default is 200 it can lead to runaway cash increases for simple agents like ours.

        print 'players will play in the following order: ', '->'.join(
            [p.player_name for p in self.game_elem['players']])
        print 'Beginning play. Rolling first die...'
        current_player_index = 0
        num_active_players = 4
        winner = None
        list_1 = []
        list_2 = []
        while num_active_players > 1:
            if self.start_stop_flag == True:
                current_player = self.game_elem['players'][
                    current_player_index]
                while current_player.status == 'lost':
                    current_player_index += 1
                    current_player_index = current_player_index % len(
                        self.game_elem['players'])
                    current_player = self.game_elem['players'][
                        current_player_index]
                current_player.status = 'current_move'

                # pre-roll for current player + out-of-turn moves for everybody else,
                # till we get num_active_players skip turns in a row.

                skip_turn = 0
                if current_player.make_pre_roll_moves(
                        self.game_elem
                ) == 2:  # 2 is the special skip-turn code
                    skip_turn += 1
                out_of_turn_player_index = current_player_index + 1
                out_of_turn_count = 0
                while skip_turn != num_active_players and out_of_turn_count <= 200:
                    out_of_turn_count += 1
                    # print 'checkpoint 1'
                    out_of_turn_player = self.game_elem['players'][
                        out_of_turn_player_index %
                        len(self.game_elem['players'])]
                    if out_of_turn_player.status == 'lost':
                        out_of_turn_player_index += 1
                        continue
                    oot_code = out_of_turn_player.make_out_of_turn_moves(
                        self.game_elem)
                    # add to game history
                    self.game_elem['history']['function'].append(
                        out_of_turn_player.make_out_of_turn_moves)
                    params = dict()
                    params['self'] = out_of_turn_player
                    params['current_gameboard'] = self.game_elem
                    self.game_elem['history']['param'].append(params)
                    self.game_elem['history']['return'].append(oot_code)

                    if oot_code == 2:
                        skip_turn += 1
                    else:
                        skip_turn = 0
                    out_of_turn_player_index += 1

                # now we roll the dice and get into the post_roll phase,
                # but only if we're not in jail.

                r = roll_die(self.game_elem['dies'], np.random.choice)
                list_1.append(r[0])
                list_2.append(r[1])
                self.dice_list = r
                # add to game history
                self.game_elem['history']['function'].append(roll_die)
                params = dict()
                params['die_objects'] = self.game_elem['dies']
                params['choice'] = np.random.choice
                self.game_elem['history']['param'].append(params)
                self.game_elem['history']['return'].append(r)

                num_die_rolls += 1
                self.game_elem['current_die_total'] = sum(r)
                print 'dies have come up ', str(r)
                if not current_player.currently_in_jail:
                    check_for_go = True
                    move_player_after_die_roll(current_player, sum(r),
                                               self.game_elem, check_for_go)
                    # add to game history
                    self.game_elem['history']['function'].append(
                        move_player_after_die_roll)
                    params = dict()
                    params['player'] = current_player
                    params['rel_move'] = sum(r)
                    params['current_gameboard'] = self.game_elem
                    params['check_for_go'] = check_for_go
                    self.game_elem['history']['param'].append(params)
                    self.game_elem['history']['return'].append(None)

                    current_player.process_move_consequences(self.game_elem)
                    # add to game history
                    self.game_elem['history']['function'].append(
                        current_player.process_move_consequences)
                    params = dict()
                    params['self'] = current_player
                    params['current_gameboard'] = self.game_elem
                    self.game_elem['history']['param'].append(params)
                    self.game_elem['history']['return'].append(None)

                    # post-roll for current player. No out-of-turn moves allowed at this point.
                    current_player.make_post_roll_moves(self.game_elem)
                    # add to game history
                    self.game_elem['history']['function'].append(
                        current_player.make_post_roll_moves)
                    params = dict()
                    params['self'] = current_player
                    params['current_gameboard'] = self.game_elem
                    self.game_elem['history']['param'].append(params)
                    self.game_elem['history']['return'].append(None)

                else:
                    current_player.currently_in_jail = False  # the player is only allowed to skip one turn (i.e. this one)

                if current_player.current_cash < 0:
                    code = current_player.handle_negative_cash_balance(
                        current_player, self.game_elem)
                    # add to game history
                    self.game_elem['history']['function'].append(
                        current_player.handle_negative_cash_balance)
                    params = dict()
                    params['player'] = current_player
                    params['current_gameboard'] = self.game_elem
                    self.game_elem['history']['param'].append(params)
                    self.game_elem['history']['return'].append(code)
                    if code == -1 or current_player.current_cash < 0:
                        current_player.begin_bankruptcy_proceedings(
                            self.game_elem)
                        # add to game history
                        self.game_elem['history']['function'].append(
                            current_player.begin_bankruptcy_proceedings)
                        params = dict()
                        params['self'] = current_player
                        params['current_gameboard'] = self.game_elem
                        self.game_elem['history']['param'].append(params)
                        self.game_elem['history']['return'].append(None)

                        num_active_players -= 1
                        diagnostics.print_asset_owners(self.game_elem)
                        diagnostics.print_player_cash_balances(self.game_elem)

                        if num_active_players == 1:
                            for p in self.game_elem['players']:
                                if p.status != 'lost':
                                    winner = p
                                    p.status = 'won'
                else:
                    current_player.status = 'waiting_for_move'

                current_player_index = (current_player_index + 1) % len(
                    self.game_elem['players'])

                time.sleep(0.1)
                self.update_board()

                if diagnostics.max_cash_balance(
                        self.game_elem
                ) > 300000:  # this is our limit for runaway cash for testing purposes only.
                    # We print some diagnostics and return if any player exceeds this.
                    diagnostics.print_asset_owners(self.game_elem)
                    diagnostics.print_player_cash_balances(self.game_elem)
                    return
        '''
        df = pd.DataFrame(list(zip(list_1, list_2)), columns=['die1', 'die2'])
        csv_name = "check_die_roll_0.csv"
        df.to_csv(csv_name)
        '''
        # let's print some numbers
        print 'printing final asset owners: '
        diagnostics.print_asset_owners(self.game_elem)
        print 'number of dice rolls: ', str(num_die_rolls)
        print 'printing final cash balances: '
        diagnostics.print_player_cash_balances(self.game_elem)

        if winner:
            print 'We have a winner: ', winner.player_name
            #winner_list.append(winner.player_name)
        return