乌龟对对碰开局应该抽几只?

news/2024/11/16 22:56:47/文章来源:https://www.cnblogs.com/EvaKo/p/18351883

结论: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 turtlesinitial_turtles = [random.choice(colors) for _ in range(n)]  # Initialize turtles with random colorsreturn 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 poolturtle = 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 poolif turtle == lucky_color:turtle_pool.append(random.choice(range(10)))return turtlereturn 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 spacesif 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 = 0two_in_a_row_count = 0# Check for three in a row/columnfor 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 += 1for 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 gridfor i, j in to_remove:grid[i][j] = None# Refill turtle pool if three in a row were foundif 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 thempairs = []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 gridif pairs:i, j, x, y = pairs[0]grid[i][j] = Nonegrid[x][y] = Noneturtle_pool.append(random.choice(range(10)))two_in_a_row_count += 1# Check if all grid colors are differentcolors_in_grid = set()all_different = Truefor 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 = Falsecolors_in_grid.add(grid[i][j])else:all_different = False# Clear the grid if all colors are different and reward with more turtlesif all_different:for i in range(3):for j in range(3):grid[i][j] = Nonefor _ 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 Truefor j in range(3):if grid[0][j] is not None and grid[0][j] == grid[1][j] == grid[2][j]:return Truefor 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 Truereturn 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 = nthree_in_a_row_count = 0two_in_a_row_count = 0lucky_turtle_draws = 0all_different_count = 0# Continue game while there are moves leftwhile turtle_pool or grid_has_pairs(grid):fill_grid(turtle_pool, grid, lucky_color)# Check grid and update it while there are matcheswhile 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:breakthree_in_a_row_count += three_counttwo_in_a_row_count += two_countif all_different:all_different_count += 1# Check for and count lucky color drawsfor row in grid:for turtle in row:if turtle == lucky_color:lucky_turtle_draws += 1# Calculate total number of drawstotal_draws = n + lucky_turtle_draws + two_in_a_row_count + 5 * three_in_a_row_count + 5 * all_different_countreturn 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 = 10000for 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()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/780670.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

写一个cpp uos系统

#include <iostream>int main() {std::cout << "Hello, UOS!" << std::endl;return 0; }创建一个test.cpp文件, 在当前目录打开终端, 把上面代码内容写里面, 输入g++ test.cpp, 生成了一个a.out文件, ./a.out 输出 Hello, UOS!

C#项目—彩票选号

C#彩票选号软件 今天做了一个彩票选号的小软件,将学到的知识点总结如下; 1.写程序的思路实体类(属性、方法) 主程序调用类方法2.学到的知识点判断、循环 文件的操作(保存、打开) 集合、数组(增加-AddRange、删除-RemoveAt、排序-Reverse) 关键字:Random(随机数-Next)…

C#学习——彩票选号软件

C#彩票选号软件 今天做了一个彩票选号的小软件,将学到的知识点总结如下; 1.写程序的思路实体类(属性、方法) 主程序调用类方法2.学到的知识点判断、循环 文件的操作(保存、打开) 集合、数组(增加-AddRange、删除-RemoveAt、排序-Reverse) 关键字:Random(随机数-Next)…

探究grid_sample函数

一、函数介绍torch.nn.functional.grid_sample(input, grid, mode=bilinear, padding_mode=zeros, align_corners=None)对于4D输入,input维度为 \((N,C,H_{in},W_{in})\), grid维度为 \((N,H_{out},W_{out},2)\) ,则output维度为 \((N,C,H_{out},W_{out})\)对于5D输入,input…

036.CI4框架CodeIgniter,VUE+CI4互通,VUE通过伪静态地址访问,内容为调用的CI4的API

CI4和VUE前端代码,可以看我在https://www.cnblogs.com/tianpan2019写的前2篇运行CI4能正常打开 使用npm run dev 也可以正常打开VUE 根据https://www.cnblogs.com/tianpan2019前1篇把根目录设置成CI4的publi目录 把伪静态内容也写上,内容如下:location / { if (!-e $reques…

2024年1000个计算机毕业设计项目源码(源码+论文【万字】)

【计算机毕设项目】2025级100个热门项目推荐 (前后端Web项目) 以下项目选题适合计算机专业大部分专业,技术栈主要为:Java语言,SpringBoot+Vue框架,MySQL数据库以下项目选题适合计算机专业大部分专业,技术栈主要为:Java语言,SpringBoot+Vue框架,MySQL数据库 ①后台免费获…

035.CI4框架CodeIgniter,安装前端VUE并执行

安装node22.5 选择路径 安装完成 装好后使用cmd查看node -v和 npm -v,看一下就知道有没有装好了 nodejs的安装目录,需要完全授权才可以,不然后面会报错 在网站的根目录,打开Powershell 显示权限不足的错误 管理员模式打开系统的Powershell 输入set-ExecutionPolicy RemoteS…

7月编程心得

7 月份非常忙碌,想系统性的写一篇文章,好几次不知道如何下手,后来想想还不如顺其自然,写一点自己的学习心得体会。 这篇文章,聊聊 7月份笔者的编程心得 ,希望对大家有所帮助。 1 IntelliJ IDEA 社区版 工欲善其事,必先利其器。 笔者的 Mac 电脑安装了 IntelliJ IDEA Ult…

三分钟对可视化表单设计器开源优点做简单介绍

为了帮助大家了解可视化表单设计器开源的多个优势特点,本文将为大家做一个系统介绍。想要实现流程化办公,需要了解低代码技术平台、可视化表单设计器开源在现代化办公中的作用和价值。为了帮助大家了解可视化表单设计器开源的多个优势特点,本文将为大家做一个系统介绍。 要了…

校园集市小程序开发-系统架构与Django后端

引子: 烂尾就烂尾吧,大不了以后自己创业。唉,也算给自己一个警戒。E-R模型接口设计数据模型设计 基本模型放在models.py中 class School(models.Model):id = models.AutoField(primary_key=True)name = models.CharField(max_length=100, verbose_name=学校名称) # 学校校区 …

基于MPPT最大功率跟踪算法的光伏并网发电系统simulink仿真

1.课题概述基于MPPT最大功率跟踪算法的光伏并网发电系统simulink仿真,包括PV模型建模(不使用simulink自带的PV模块,根据公式进行建模),MPPT最大功率控制器,PI控制器,锁相环,逆变器等等。输出系统的收敛曲线。2.系统仿真结果 上面三个波形分别表示光伏并网输出的Ipv,Upv和…

如何选择最适合你的免费项目管理软件

国内外主流的10款免费项目管理软件对比:PingCode、Worktile、Teambition、Tower、Tapd、Asana、Trello、Wrike、Basecamp、ClickUp、Monday.com。在众多项目管理工具中,找到一个既免费又能满足你专业需求的软件似乎是一项挑战。市场上免费和付费工具琳琅满目,但很多小型团队…