python抽卡游戏概率计算

#!/usr/bin/python3
import random

# ry荣耀,gg桂冠,sg碎骨,yh永恒阵营,殿堂、大师、杰出品阶
zhenying = ('ry', 'gg', 'sg', 'yh')
diantang = []
dashi = []
jiechu = []

# 给每一张卡编号,计算各品级数量,每个阵营7张殿堂级,3大师级,2杰出级
for i in zhenying:
    for a in range(1, 8):
        diantang.append('{0}DT{1}'.format(i, a))
for i in zhenying:
    for a in range(1, 4):
        dashi.append('{0}DS{1}'.format(i, a))
for i in zhenying:
    for a in range(1, 3):
        jiechu.append('{0}JC{1}'.format(i, a))

# 荣耀阵营的殿堂级和大师级要多一个,单独加上去
diantang.append('ryDT8')
dashi.append('ryDS4')

# 来自大师级进阶的假紫卡
JZ = []
for i,k in enumerate(dashi):
    JZ.append('{0}JZ'.format(k))

# 卡池:紫卡包括假紫、殿堂,蓝卡大师级,绿卡杰出级和紫色碎片
zika = JZ + diantang
lanka = dashi
lvka = jiechu + ['Suipian']

# 抽卡概率,紫卡4.61%,蓝卡51.69%,绿卡43.7%
def chouka():
    a = random.randint(0, 10000)
    if a >= 9539:
        return random.choice(zika)
    elif 4370 < a < 9539:
        return random.choice(lanka)
    elif a <= 4370:
        return random.choice(lvka)

# 按次数抽卡并得到结果
zongji = []
cishu = input('你想抽多少次:')
while True:
    if cishu.isdigit() == True:
        cishu = int(cishu)
        if cishu < 1:
            cishu = input('请输入大于0的数字:')
        else:
            break
    else:
        cishu = input("请输入一个整数:")
for i in range(cishu):
    zongji.append(chouka())

#将抽到的卡进行分类
cd1 = []
cd2 = []
cd3 = []
jz =[]
for i in zongji:
    if i in diantang:
        cd1.append(i)
    if i in dashi:
        cd2.append(i)
    if i in jiechu:
        cd3.append(i)
    if i in JZ:
        jz.append(i)
print(len(cd1), '个殿堂级')
print(len(cd2), '个大师级')
print(len(cd3), '个杰出级')
sp = zongji.count('Suipian')
print(sp, '个碎片')
print('假紫的个数是:',len(jz))
print('殿堂级英雄是:', cd1)

# 假设我想抽到某张特定卡,需要多少次
b = 0  # 初始次数
c = random.choice(diantang)  # 随机一个殿堂级英雄
while True:
    a = chouka()
    if a == c:
        print('抽到', c, '一共用了', b, '次')
        break
    else:
        b = b + 1

你可能感兴趣的:(python,游戏)