Guest User

ChatGPT 40k Simulation

a guest
Feb 17th, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.34 KB | Source Code | 0 0
  1. import random
  2. import statistics
  3.  
  4. class Weapon:
  5.     def __init__(self, name: str, attacks: int, strength: int, armor_penetration: int, damage: int):
  6.         self.name = name
  7.         self.attacks = attacks
  8.         self.strength = strength
  9.         self.armor_penetration = armor_penetration
  10.         self.damage = damage
  11.  
  12.     def __str__(self):
  13.         return (f"Weapon: {self.name}, Attacks: {self.attacks}, Strength: {self.strength}, AP: {self.armor_penetration}, "
  14.                 f"Damage: {self.damage}")
  15.  
  16.  
  17. class Unit:
  18.     def __init__(self, name: str, model_count: int, toughness: int, hit_skill: int, armor: int, weapon: Weapon, wounds: int):
  19.         self.name = name
  20.         self.model_count = model_count
  21.         self.toughness = toughness
  22.         self.hit_skill = hit_skill
  23.         self.armor = armor
  24.         self.weapon = weapon
  25.         self.wounds = wounds
  26.         self.total_wounds = model_count * wounds
  27.  
  28.     def attack(self, target):
  29.         hits = 0
  30.         wounds = 0
  31.         failed_saves = 0
  32.  
  33.         # Roll to hit
  34.         for _ in range(self.model_count * self.weapon.attacks):
  35.             roll = random.randint(1, 6)
  36.             if roll >= self.hit_skill:
  37.                 hits += 1
  38.  
  39.         # Roll to wound
  40.         for _ in range(hits):
  41.             roll = random.randint(1, 6)
  42.            
  43.             if self.weapon.strength >= 2 * target.toughness:
  44.                 wound_roll_needed = 2
  45.             elif self.weapon.strength > target.toughness:
  46.                 wound_roll_needed = 3
  47.             elif self.weapon.strength == target.toughness:
  48.                 wound_roll_needed = 4
  49.             elif self.weapon.strength * 2 <= target.toughness:
  50.                 wound_roll_needed = 6
  51.             else:
  52.                 wound_roll_needed = 5
  53.            
  54.             if roll >= wound_roll_needed:
  55.                 wounds += 1
  56.         # Target rolls armor saves
  57.         for _ in range(wounds):
  58.             save_roll_needed = max(2, target.armor - self.weapon.armor_penetration)
  59.             roll = random.randint(1, 6)
  60.             if roll < save_roll_needed:
  61.                 failed_saves += 1
  62.  
  63.         # Apply damage
  64.         total_damage = failed_saves * self.weapon.damage
  65.         target.total_wounds -= total_damage
  66.         target.model_count = max(0, target.total_wounds // target.wounds)
  67.  
  68.     def is_alive(self):
  69.         return self.model_count > 0
  70.  
  71.  
  72. def combat_simulation(unit1, unit2):
  73.     first_attacker = random.choice([unit1, unit2])
  74.     second_attacker = unit2 if first_attacker == unit1 else unit1
  75.     rounds = 0
  76.  
  77.     while unit1.is_alive() and unit2.is_alive():
  78.         first_attacker.attack(second_attacker)
  79.         if second_attacker.is_alive():
  80.             second_attacker.attack(first_attacker)
  81.         rounds += 1
  82.  
  83.     winner = unit1 if unit1.is_alive() else unit2
  84.     first_attacker_won = (winner == first_attacker)
  85.     return (winner == unit1, winner.total_wounds, rounds, first_attacker_won)
  86.  
  87. simulation_count = 9999
  88. space_marine_model_count = 100
  89. guardsman_model_count = 400
  90.  
  91. def run_simulations(n=simulation_count):
  92.     results = []
  93.     for i in range(n):
  94.         guardsmen = Unit("Guardsmen", model_count=guardsman_model_count, toughness=3, hit_skill=4, armor=5, weapon=lasgun, wounds=1)
  95.         space_marines = Unit("Space Marines", model_count=space_marine_model_count, toughness=4, hit_skill=3, armor=3, weapon=bolter, wounds=2)
  96.         result = combat_simulation(guardsmen, space_marines)
  97.         if result:
  98.             results.append(result)
  99.     if not results:
  100.         print("Error: No results recorded. Check combat simulation logic.")
  101.         return
  102.  
  103.     # Compute statistics
  104.     guardsmen_wins = sum(1 for r in results if r[0])
  105.     win_percentage = (guardsmen_wins / n) * 100
  106.  
  107.     guardsmen_wound_results = [r[1] for r in results if r[0]]
  108.     space_marines_wound_results = [r[1] for r in results if not r[0]]
  109.     round_results = [r[2] for r in results]
  110.     first_attacker_wins = sum(1 for r in results if r[3])
  111.     first_attacker_win_percentage = (first_attacker_wins / n) * 100
  112.  
  113.     if guardsmen_wound_results:
  114.         guardsmen_median_wounds = statistics.median(guardsmen_wound_results)
  115.     else:
  116.         guardsmen_median_wounds = 0
  117.     if space_marines_wound_results:
  118.         space_marines_median_wounds = statistics.median(space_marines_wound_results)
  119.     else:
  120.         space_marines_median_wounds = 0
  121.     median_rounds = statistics.median(round_results)
  122.     space_percentage = space_marines_median_wounds / (2 * space_marine_model_count)
  123.     guard_percentage = guardsmen_median_wounds / guardsman_model_count
  124.  
  125.     if win_percentage < 50:
  126.         print(f"Space Marines won {100 - win_percentage:.2f}% of the time.")
  127.         print(f"Median wounds remaining for the Space Marine winner: {space_marines_median_wounds} ({space_percentage:.2f})")
  128.     else:
  129.         print(f"Guardsmen won {win_percentage:.2f}% of the time.")
  130.         print(f"Median wounds remaining for the Guardsmen winner: {guardsmen_median_wounds} ({guard_percentage:.2f})")
  131.     print(f"There were a median of {median_rounds} rounds.")
  132.     print(f"The first attacker won {first_attacker_win_percentage:.2f}% of the time.")
  133.  
  134. # Example Usage
  135. lasgun = Weapon("Lasgun", attacks=1, strength=3, armor_penetration=0, damage=1)
  136. bolter = Weapon("Bolter", attacks=2, strength=4, armor_penetration=0, damage=1)
  137.  
  138. run_simulations()
Advertisement
Add Comment
Please, Sign In to add comment