欢迎来到随便乱穿的猫猫王国!
思路来自于某点的《乱穿是一种病》,感谢作者剑南烧春大佬的脑洞。
模拟器仅供娱乐,版权归我的猫所有,它看我打游戏的时候写的。
import random
import time
import copy
########################以上勿动##########################
#######################修改参数在此########################
################
#以下两个要一一对应!!!
# 每个猫猫王国等级穿越概率,以英文逗号分割
chuanyue_probs = [1, 0.5, 0.001]
# 每个猫猫王国等级所占人口比例,以英文逗号分割,和必须为1!!!
fortune_radio = [0.2, 0.7, 0.1]
#以上两个要一一对应!!!
################
# 猫猫的数量
cat_count = 1e4
# 猫猫王国经过的天数
steps = 1e2
#######################以下勿动##############################
debug = False
every_day_output = False
max_grade = len(fortune_radio) - 1
class cat:
def __init__(self, chuanyue_prob, id, social_grade):
self.chuanyue_prob = chuanyue_prob
self.body_id = id
self.soul_id = id
self.social_grade = social_grade
self.soul_social_grade = social_grade
self.chuanyue_flag = -1
def will_chuanyue(self):
# 当低等级猫猫置换到最高等级猫猫后停止置换
if self.soul_social_grade < self.social_grade == max_grade:
return False
else:
return random.random() <= self.chuanyue_prob
def chuanyue(self, target_cat):
soul_id_temp = self.soul_id
self.soul_id = target_cat.soul_id
target_cat.soul_id = soul_id_temp
soul_social_grade_temp = self.soul_social_grade
self.soul_social_grade = target_cat.soul_social_grade
target_cat.soul_social_grade = soul_social_grade_temp
def validate(fortune_radio, chuanyue_probs):
sum = 0
for i in fortune_radio:
sum += i
assert abs(sum - 1) < 1e-10, "猫猫王国等级比例和不为1"
assert len(fortune_radio) == len(chuanyue_probs), "猫猫王国等级比例和穿越概率需一一对应"
def analyse_cats(cats):
data = [[0, 0, 0] for _ in range(len(fortune_radio))]
for cat in cats:
if debug:
print("身体id:{0},灵魂id:{1},身体等级:{2},灵魂等级{3}".format(cat.body_id, cat.soul_id, cat.social_grade,
cat.soul_social_grade))
if cat.soul_social_grade < cat.social_grade:
data[cat.social_grade][0] += 1
if cat.soul_social_grade == cat.social_grade:
data[cat.social_grade][1] += 1
if cat.soul_social_grade > cat.social_grade:
data[cat.social_grade][2] += 1
for grade in range(len(data)):
sum = data[grade][0] + data[grade][1] + data[grade][2]
print("猫猫王国等级{0}共有猫猫{1}只,现有低等级猫猫{2}只,本等级猫猫{3}只,高等级猫猫{4}只"
.format(grade, sum, data[grade][0], data[grade][1], data[grade][2]))
if __name__ == '__main__':
validate(fortune_radio, chuanyue_probs)
init_start_time = time.time()
cats = []
id = 0
for grade in range(len(fortune_radio)):
for _ in range(int(cat_count * fortune_radio[grade])):
cats.append(cat(chuanyue_probs[grade], id, grade))
id += 1
init_end_time = time.time()
print("初始化完成耗时{0}秒".format(init_end_time - init_start_time))
print("----------------------------")
for day in range(1, int(steps) + 1):
day_start_time = time.time()
chuanyue_group = [cat for cat in cats if cat.will_chuanyue()]
print("第{0}天有{1}个猫猫选择置换".format(day, len(chuanyue_group)))
chuanyue_group_for_remove = copy.copy(chuanyue_group)
for cat in chuanyue_group:
if (len(chuanyue_group_for_remove) == 1):
break
if (cat in chuanyue_group_for_remove):
chuanyue_group_for_remove.remove(cat)
target_cat = chuanyue_group_for_remove[random.randint(0, len(chuanyue_group_for_remove) - 1)]
cat.chuanyue(target_cat)
chuanyue_group_for_remove.remove(target_cat)
if debug:
print("第{0}天耗时{1}秒".format(day, time.time() - day_start_time))
if every_day_output:
analyse_cats(cats)
print("----------------------------")
print("---------------------\n最后的猫猫王国")
analyse_cats(cats)
版权声明:本文为kyfzx原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。