怎么用Python写一个三体的气候模拟程序

所谓三体气候模拟,就是将太阳出现的情况进行分类讨论,然后将其呈现出来。比如说一颗太阳就是恒纪元,两颗太阳可能是二日凌空或二日连珠,三颗太阳也可能是三日凌空或三日连珠。只要明白了这一点,这个三体气候模拟的程序就很好写了。

在写程序前,得先导入一个库。由于三体问题的复杂性,我们姑且将三颗太阳出现的概率定位三分之一,也就是说要用到随机的方法。所以我们这里需要导入random库中的randint——随机数函数。


from random import randint

在插入完random库后,要先确定几个变量。由于三体世界有三颗太阳,且可能出现在不同的位置,所以可以定义变量:


# 其中0代表该太阳为飞星,1代表该太阳出现
sun1 = randint(0, 1)
sun2 = randint(0, 1)
sun3 = randint(0, 1)
# 1~3分别代表不同的位置,如果位置相同就是连珠
sun1_pos = randint(1, 3)
sun2_pos = randint(1, 3)
sun3_pos = randint(1, 3)

除了这几个基础变量,还需要两个用来输出气候类型和纪元类型的变量:weather和era_mode


weather = ""
era_mode = ""

因为后面将这两个变量以文字形式输出,所以是String的形式

完成变量设定后,就该考虑三体世界的气候情况了。

依照书中的描述,我们可以分为(恒纪元就不讨论了):三颗飞星(即没有太阳)、二日凌空、二日连珠、三日凌空和三日连珠

这么一来,就可以开始写程序了。

首先是没有太阳,即三颗飞星情况:


if sun1 == sun2 == sun3 == 0:   # 三颗太阳都没有出现
    weather = "三颗飞星"
    era_mode = "乱纪元"
    print(era_mode, weather)    # 输出气候情况

然后检测是否为恒纪元,即一颗太阳:


if sun1 == 1 and sun2 == sun3 == 0:
    era_mode = "恒纪元"
    print(era_mode)
elif sun2 == 1 and sun1 == sun3 == 0:
    era_mode = "恒纪元"
    print(era_mode)
elif sun3 == 1 and sun1 == sun2 == 0:
    era_mode = "恒纪元"
    print(era_mode)

 接着是三颗太阳的情况:


if sun1 == sun2 == sun3 == 1:
    if sun1_pos == sun2_pos == sun3_pos:
        weather = "三日连珠"
        era_mode = "乱纪元"
        print(era_mode, weather)
    else:
        weather = "三日凌空"
        era_mode = "乱纪元"
        print(era_mode, weather)

 最后是两颗太阳的情况,就相对比较麻烦了:

if sun1 == sun2 == 1:
    if sun1_pos == sun2_pos:
        weather = "二日连珠"
        era_mode = "乱纪元"
        print(era_mode, weather)
    else:
        weather = "二日凌空"
        era_mode = "乱纪元"
        print(era_mode, weather)
elif sun1 == sun3 == 1:
    if sun1_pos == sun2_pos:
        weather = "二日连珠"
        era_mode = "乱纪元"
        print(era_mode, weather)
    else:
        weather = "二日凌空"
        era_mode = "乱纪元"
        print(era_mode, weather)
elif sun2 == sun3 == 1:
    if sun2_pos == sun3_pos:
        weather = "二日连珠"
        era_mode = "乱纪元"
        print(era_mode, weather)
    else:
        weather = "二日凌空"
        era_mode = "乱纪元"
        print(era_mode, weather)

 

注意,这个从三颗飞星、一颗太阳、三颗太阳、两颗太阳的顺序是不能打乱的,否则就会出现气候判断不准的情况,因为这个程序的运行是从上往下走的,是线性的。举个例子,如果现在有三颗太阳,而两颗太阳的判定在三颗太阳的判定之前,程序运行时就会出现只输出二日连珠或二日凌空的情况,而不会输出三日凌空或三日连珠。

当然,你也可以用其他的方法让气候判断的排序改变,比如可以全部把这些判断啥的写道不同的函数里在进行判断,但在这里就不加以赘述。

最后把全部代码放上来:


from random import randint

# 其中0代表该太阳为飞星,1代表该太阳出现
sun1 = randint(0, 1)
sun2 = randint(0, 1)
sun3 = randint(0, 1)
# 1~3分别代表不同的位置,如果位置相同就是连珠
sun1_pos = randint(1, 3)
sun2_pos = randint(1, 3)
sun3_pos = randint(1, 3)

weather = ""
era_mode = ""

if sun1 == sun2 == sun3 == 0:   # 三颗太阳都没有出现
    weather = "三颗飞星"
    era_mode = "乱纪元"
    print(era_mode, weather)    # 输出气候情况
elif sun1 == 1 and sun2 == sun3 == 0:
    era_mode = "恒纪元"
    print(era_mode)
elif sun2 == 1 and sun1 == sun3 == 0:
    era_mode = "恒纪元"
    print(era_mode)
elif sun3 == 1 and sun1 == sun2 == 0:
    era_mode = "恒纪元"
    print(era_mode)
elif sun1 == sun2 == sun3 == 1:
    if sun1_pos == sun2_pos == sun3_pos:
        weather = "三日连珠"
        era_mode = "乱纪元"
        print(era_mode, weather)
    else:
        weather = "三日凌空"
        era_mode = "乱纪元"
        print(era_mode, weather)
elif sun1 == sun2 == 1:
    if sun1_pos == sun2_pos:
        weather = "二日连珠"
        era_mode = "乱纪元"
        print(era_mode, weather)
    else:
        weather = "二日凌空"
        era_mode = "乱纪元"
        print(era_mode, weather)
elif sun1 == sun3 == 1:
    if sun1_pos == sun2_pos:
        weather = "二日连珠"
        era_mode = "乱纪元"
        print(era_mode, weather)
    else:
        weather = "二日凌空"
        era_mode = "乱纪元"
        print(era_mode, weather)
elif sun2 == sun3 == 1:
    if sun2_pos == sun3_pos:
        weather = "二日连珠"
        era_mode = "乱纪元"
        print(era_mode, weather)
    else:
        weather = "二日凌空"
        era_mode = "乱纪元"
        print(era_mode, weather)

总行数不超过100行,还是挺精简的

如果要查看太阳的生成,可以在添加完变量后(即上述代码的11行处)添加:


print("sun1: %u , sun2: %u , sun3: %u" % (sun1, sun2, sun3))
print("sun1_pos: %u , sun2_pos: %u , sun3: %u" % (sun1_pos, sun2_pos, sun3_pos))

 

 

你可能感兴趣的:(怎么用Python写一个三体的气候模拟程序)