小冰战斗模拟 -py 版

感谢凤雏的战斗值探索:《小冰游戏战斗数值计算》,@baba22222

看到很多人需要,将其转换为了python版,通过自定次数的战斗模拟成功概率,修改对应变量直接运行就好

#!/usr/bin/env python

import random

def get_monster_atk():
    global monster_lv,user_lv
    level_diff = monster_lv - user_lv
    if level_diff > 20:
        return 9999999
    elif level_diff > 15:
        return int(monster_lv * 20 + 100 * random.random() * 28 + 4)
    elif level_diff > 10:
        return int(monster_lv * 12 + 50 * random.random() * 21 + 3)
    elif level_diff > 7:
        return int(monster_lv * 8 + 20 * random.random() * 14 + 2)
    elif level_diff > 3:
        return int(monster_lv * 7 + 10 * random.random() * 7 + 1)
    else:
        return int(monster_lv * 7)

def battle_with_monster():
    global user_lv,user_arms,user_fire,user_armor,monster_lv

    uatk = (user_lv * 10 + user_arms) * (user_fire + 1)
    udef = user_lv * 10 + user_armor
    uhp = 100 + user_lv * 100
    matk = get_monster_atk()
    mdef = monster_lv * 5
    mhp = int(random.random() * monster_lv + monster_lv * 100)

    round_num = 0
    is_win = False
    while uhp > 0 and mhp > 0:
        round_num += 1
        critical_hit = random.random() * 100 > 70
        uhp -= max(0, matk - udef)
        mhp -= max(0, (uatk * 2 if critical_hit else uatk) - mdef)
    is_win = uhp > 0
    return is_win

def simulate_battles(num_battles):
    wins = 0
    for _ in range(num_battles):
        if battle_with_monster():
            wins += 1
    win_probability = wins / num_battles
    print(f"{num_battles} 次战斗成功的概率是: {win_probability:.2%}")
    return win_probability
  
user_lv = 65        # 人物等级
user_arms = 100     # 武器值
user_armor = 100    # 防具值
user_fire = 2     # 异火加成

monster_lv = 80     # 怪物等级

print("人物等级: ",user_lv,"\n武器值: ",user_arms)
print("防具值: ",user_armor,"\n异火加成: ",user_fire)
print("\n怪物等级: ",monster_lv)

# 运行5万次战斗模拟
simulate_battles(5_0000)