烏龜對對碰開局應該抽幾隻?

evaq發表於2024-08-10

結論:30只以上為佳,3的倍數

https://www.bilibili.com/video/BV17nYVekEWU/?vd_source=9b97428fe30142d2bb4f9bab6ef082a4

image

image

# Import necessary libraries
import numpy as np
import random
from collections import defaultdict
import matplotlib.pyplot as plt

# Function to initialize the game with a specified number of turtles with random colors
def initialize_game(n):
    colors = list(range(10))  # Define ten possible colors for the turtles
    initial_turtles = [random.choice(colors) for _ in range(n)]  # Initialize turtles with random colors
    return initial_turtles

# Function to draw a turtle from the pool; may modify the pool if the lucky color is drawn
def draw_turtle(turtle_pool, lucky_color):
    if turtle_pool:
        # Randomly select and remove a turtle from the pool
        turtle = turtle_pool.pop(random.randint(0, len(turtle_pool) - 1))
        # If the turtle's color matches the lucky color, add a new random turtle to the pool
        if turtle == lucky_color:
            turtle_pool.append(random.choice(range(10)))
        return turtle
    return None

# Function to fill a 3x3 grid with turtles from the pool
def fill_grid(turtle_pool, grid, lucky_color):
    for i in range(3):
        for j in range(3):
            # Only place a new turtle in empty grid spaces
            if grid[i][j] is None and turtle_pool:
                grid[i][j] = draw_turtle(turtle_pool, lucky_color)

# Function to check for three in a row or column and update the grid accordingly
def check_and_update_grid(grid, turtle_pool, lucky_color):
    to_remove = []
    three_in_a_row_count = 0
    two_in_a_row_count = 0
    
    # Check for three in a row/column
    for i in range(3):
        if grid[i][0] is not None and grid[i][0] == grid[i][1] == grid[i][2]:
            to_remove.extend([(i, 0), (i, 1), (i, 2)])
            three_in_a_row_count += 1
    for j in range(3):
        if grid[0][j] is not None and grid[0][j] == grid[1][j] == grid[2][j]:
            to_remove.extend([(0, j), (1, j), (2, j)])
            three_in_a_row_count += 1
    
    # Remove turtles from the grid
    for i, j in to_remove:
        grid[i][j] = None
    
    # Refill turtle pool if three in a row were found
    if to_remove:
        for _ in range(5 * three_in_a_row_count):
            turtle_pool.append(random.choice(range(10)))
    
    # Check for two in a row/column and clear them
    pairs = []
    for i in range(3):
        for j in range(3):
            if grid[i][j] is not None:
                for x in range(i, 3):
                    for y in range(3):
                        if (i != x or j != y) and grid[i][j] == grid[x][y]:
                            pairs.append((i, j, x, y))
    
    # Remove pairs of turtles from the grid
    if pairs:
        i, j, x, y = pairs[0]
        grid[i][j] = None
        grid[x][y] = None
        turtle_pool.append(random.choice(range(10)))
        two_in_a_row_count += 1
    
    # Check if all grid colors are different
    colors_in_grid = set()
    all_different = True
    for i in range(3):
        for j in range(3):
            if grid[i][j] is not None:
                if grid[i][j] in colors_in_grid:
                    all_different = False
                colors_in_grid.add(grid[i][j])
            else:
                all_different = False
    
    # Clear the grid if all colors are different and reward with more turtles
    if all_different:
        for i in range(3):
            for j in range(3):
                grid[i][j] = None
        for _ in range(5):
            turtle_pool.append(random.choice(range(10)))
    
    return (three_in_a_row_count, two_in_a_row_count, all_different)

# Function to determine if there are any pairs of turtles left in the grid
def grid_has_pairs(grid):
    """Check if there are any removable groups of turtles in the grid"""
    for i in range(3):
        if grid[i][0] is not None and grid[i][0] == grid[i][1] == grid[i][2]:
            return True
    for j in range(3):
        if grid[0][j] is not None and grid[0][j] == grid[1][j] == grid[2][j]:
            return True
    for i in range(3):
        for j in range(3):
            if grid[i][j] is not None:
                for x in range(i, 3):
                    for y in range(3):
                        if (i != x or j != y) and grid[i][j] == grid[x][y]:
                            return True
    return False

# Main function to simulate the gameplay
def play_game(n, lucky_color):
    turtle_pool = initialize_game(n)
    grid = [[None for _ in range(3)] for _ in range(3)]
    total_draws = n
    three_in_a_row_count = 0
    two_in_a_row_count = 0
    lucky_turtle_draws = 0
    all_different_count = 0
    
    # Continue game while there are moves left
    while turtle_pool or grid_has_pairs(grid):
        fill_grid(turtle_pool, grid, lucky_color)
        
        # Check grid and update it while there are matches
        while True:
            three_count, two_count, all_different = check_and_update_grid(grid, turtle_pool, lucky_color)
            if three_count == 0 and two_count == 0 and not all_different:
                break
            three_in_a_row_count += three_count
            two_in_a_row_count += two_count
            if all_different:
                all_different_count += 1
        
        # Check for and count lucky color draws
        for row in grid:
            for turtle in row:
                if turtle == lucky_color:
                    lucky_turtle_draws += 1
    
    # Calculate total number of draws
    total_draws = n + lucky_turtle_draws + two_in_a_row_count + 5 * three_in_a_row_count + 5 * all_different_count
    
    return total_draws

# Simulation function to run multiple trials and analyze results
def run_simulation(trials, n):
    results = []
    lucky_color = random.choice(range(10))
    for _ in range(trials):
        m = play_game(n, lucky_color)
        results.append(m)
    return np.array(results)

# Main function to conduct simulations and plot results
def main():
    n_list=[]
    m_mean_list=[]
    m_variance_list=[]
    m_mean_div_n_list=[]
    trials = 10000
    for n in range(1, 11, 1):
        results = run_simulation(trials, n)
        mean = np.mean(results)
        variance = np.var(results)
        distribution = dict(zip(*np.unique(results, return_counts=True)))
        print(f'n = {n}:')
        print(f'  Mean = {mean}')
        print(f'  Variance = {variance}')
        print(f'  Probability Distribution = {distribution}\n')
        n_list.append(n)
        m_mean_list.append(mean)
        m_variance_list.append(variance)  
        m_mean_div_n_list.append(mean/n)
    plt.plot(n_list, m_mean_list)
    plt.plot(n_list, m_variance_list)
    plt.plot(n_list, m_mean_div_n_list)

# Check if the script is the main program and run it
if __name__ == "__main__":
    main()

相關文章