0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

小学生用のポケモン計算コード(日記)

Posted at
# hp_update.py
# プログラム名: hp_update.py
# Simple HP update and sanity check / シンプルなHP更新と検算

def update_hp(hp_before, damage):
    """
    Update HP after taking damage.
    HP更新:攻撃後の残りHPを計算する
    
    Args:
        hp_before (int): HP before attack / 攻撃前のHP
        damage (int): Damage dealt / 与えたダメージ
    
    Returns:
        int: HP after attack / 攻撃後の残りHP
    """
    hp_after = hp_before - damage
    return max(hp_after, 0)

if __name__ == "__main__":
    # --- Example parameters / 例としての値 ---
    hp_before = 100   # 攻撃前のHP / HP before attack
    damage    = 30    # 与えたダメージ / Damage dealt

    # HPを更新 / Update HP
    hp_after = update_hp(hp_before, damage)

    # 結果を表示 / Print results
    print(f"HP Before: {hp_before}")
    print(f"Damage:    {damage}")
    print(f"HP After:  {hp_after}")

    # --- Sanity check / 検算 ---
    # HP_after は hp_before - damage と一致するはず
    assert hp_after == max(hp_before - damage, 0), "HP update calculation error!"
    print("Sanity check passed. / 検算OK")



# damage_multiplier.py
# プログラム名: damage_multiplier.py
# Apply multipliers (STAB, type effectiveness, critical, randomness) to base damage
# 素ダメージに同種技ボーナス、タイプ相性、急所、乱数を掛け合わせて最終ダメージを計算

import math

def calculate_final_damage(base, stab, type_eff, crit, rand):
    """
    Calculate final damage by multiplying all factors.
    各種倍率を掛け合わせて最終ダメージを計算する
    
    Args:
        base (int): 素ダメージ / Base damage
        stab (float): 同種技ボーナス(1.0 or 1.5)/ Same-Type Attack Bonus
        type_eff (float): タイプ相性(0.0, 0.5, 1.0, 2.0)/ Type effectiveness
        crit (float): 急所倍率(1.0, 1.5 or 2.0)/ Critical hit multiplier
        rand (float): 乱数(0.85~1.00 の一様値)/ Random factor
    
    Returns:
        int: 最終ダメージ(切り捨て)/ Final damage (floored)
    """
    damage = math.floor(base * stab * type_eff * crit * rand)
    return damage

if __name__ == "__main__":
    # --- Example parameters / 例としての値 ---
    base      = 100    # 素ダメージ / Base damage
    stab      = 1.5    # 同種技ボーナス / STAB
    type_eff  = 2.0    # 効果抜群 / Super effective
    crit      = 1.5    # 急所 / Critical hit
    rand      = 0.95   # 固定乱数 / Fixed random factor

    # --- Calculate damage / ダメージ計算 ---
    damage = calculate_final_damage(base, stab, type_eff, crit, rand)

    # --- Print results / 結果を表示 ---
    print(f"Base Damage: {base}")
    print(f"STAB:        {stab}")
    print(f"TypeEff:     {type_eff}")
    print(f"Crit:        {crit}")
    print(f"Rand:        {rand}")
    print(f"Final Damage: {damage}")

    # --- Sanity check / 検算 ---
    assert damage >= 0, "Damage must be non-negative! / ダメージは非負である必要があります"
    print("Sanity check passed. / 検算OK")

# first_move_probability.py
# プログラム名: first_move_probability.py
# Calculate and print first-move probabilities for equal Speed and with Quick Claw
# スピード同速時およびせんせいのツメ使用時の先手確率を計算して表示する

def prob_first_equal_speed():
    """
    Probability of moving first when speeds are equal.
    スピードが同速のときに先手を取れる確率を返す
    """
    equal_prob = 1 / 2  # 数学的に 50% / Mathematically 50%
    return equal_prob

def prob_first_with_quick_claw(qc_rate=0.2):
    """
    Probability of moving first when holding Quick Claw.
    せんせいのツメ使用時の先手確率を返す
    
    発動時は100%先手、非発動時は同速タイブレーク(50%) / If activated: 100% first, else 50%
    """
    base_prob = prob_first_equal_speed()
    qc_prob = qc_rate + (1 - qc_rate) * base_prob
    return qc_prob

if __name__ == "__main__":
    # --- パラメータ / Parameters ---
    qc_rate = 0.2  # せんせいのツメ発動率(デフォルト20%)/ Quick Claw activation rate

    # --- 計算 / Calculations ---
    equal_prob = prob_first_equal_speed()
    qc_prob    = prob_first_with_quick_claw(qc_rate)

    # --- 結果出力 / Print results ---
    print(f"Equal-Speed First Move Probability: {equal_prob:.2%}")    # 英語ラベル
    print(f"スピード同速時の先手確率:             {equal_prob:.2%}")    # 日本語ラベル
    print(f"Quick Claw First Move Probability:   {qc_prob:.2%}")     # 英語ラベル
    print(f"せんせいのツメ使用時の先手確率:       {qc_prob:.2%}")     # 日本語ラベル

    # --- 検算 / Sanity check ---
    assert 0.0 <= equal_prob <= 1.0, "Equal speed probability out of range!"
    assert 0.0 <= qc_prob    <= 1.0, "Quick Claw probability out of range!"
    print("Sanity check passed. / 検算OK")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?