def PlayGames(player1, player2, numgames): victories1 = 0 victories2 = 0 for _ in range(numgames): game = Game(n_players=2, dice_number=4, dice_value=3, column_range=[2, 6], offset=2, initial_height=1) is_over = False who_won = None number_of_moves = 0 current_player = game.player_turn while not is_over: moves = game.available_moves() if game.is_player_busted(moves): if current_player == 1: current_player = 2 else: current_player = 1 continue else: if game.player_turn == 1: chosen_play = player1.get_action(game) else: chosen_play = player2.get_action(game) if chosen_play == 'n': if current_player == 1: current_player = 2 else: current_player = 1 #print('Chose: ', chosen_play) #game.print_board() game.play(chosen_play) #game.print_board() number_of_moves += 1 #print() who_won, is_over = game.is_finished() if number_of_moves >= 200: is_over = True who_won = -1 #print('No Winner!') if who_won == 1: victories1 += 1 if who_won == 2: victories2 += 1 #print(victories1, victories2) #print('Player 1: ', victories1 / (victories1 + victories2)) #print('Player 2: ', victories2 / (victories1 + victories2)) if victories1 + victories2 == 0: return (0, 0) p1victoryrate = victories1 / (victories1 + victories2) p2victoryrate = victories2 / (victories1 + victories2) return (p1victoryrate, p2victoryrate)
def play_game_and_give_action_pairs(player1, player2, max_moves=200): """ Plays a game of Can't-Stop between the given players Returns (-1, -1) if max_moves has been exceeded. Returns (1, 0) if player 1 wins, and returns (0, 1) if player 2 wins. """ state_action_pairs = list() #Create the game game = Game(n_players=2, dice_number=4, dice_value=3, column_range=[2, 6], offset=2, initial_height=1) current_player = game.player_turn #Cycle through the game until it concludes, or until a move count limit has been reached who_won = None move_count = 0 is_over = False while not is_over: moves = game.available_moves() #Progress the game to the next player's turn if a player has busted if game.is_player_busted(moves): current_player = 2 if current_player == 1 else 1 continue #Otherwise, we're in the middle of a turn else: #Get the current player's action and play it play_chooser = player1 if game.player_turn == 1 else player2 chosen_play = play_chooser.get_action(game) state_action_pairs.append((deepcopy(game), deepcopy(chosen_play))) game.play(chosen_play) move_count += 1 #if the player chooses to conclude their turn prior to busting, progress the game if chosen_play == 'n': current_player = 2 if current_player == 1 else 1 #Get whether the game has concluded, as well as the current standings who_won, is_over = game.is_finished() if move_count > max_moves: is_over = True who_won = -1 #Get the final scores #print("who won", who_won) if who_won == -1: final_scores = (0, 0) else: final_scores = (1, 0) if who_won == 1 else (0, 1) return final_scores, state_action_pairs