|
Post by drmanhattan on Jul 4, 2024 15:39:22 GMT
For a laugh I described to ChatGPT a program to simulate fights based on player and monster stats, with the simple rule that player would use Luck any time Monster was on 3 Stamina to try and finish it off and if player was on 2 to try and stay alive on 1 stamina, and to report the win/loss rates and what the min, max and mean stamina remaining for each case. heres what it came up with
[div]import random import statistics
# Function to get player attributes def get_player_attributes(): while True: try: p_attributes = input("Enter pSkill, pStamina, pLuck (comma separated): ") pSkill, pStamina, pLuck = map(int, p_attributes.split(','))
if not (7 <= pSkill <= 12 and 14 <= pStamina <= 24 and 7 <= pLuck <= 12): raise ValueError("Values out of allowed range.") break except ValueError as ve: print(f"Invalid input: {ve}. Please enter the values again.") return pSkill, pStamina, pLuck
# Function to get monster attributes def get_monster_attributes(): while True: try: m_attributes = input("Enter mSkill, mStamina, mLuck (comma separated): ") mSkill, mStamina, mLuck = map(int, m_attributes.split(','))
if not (1 <= mSkill <= 18 and 1 <= mStamina <= 48 and 7 <= mLuck <= 12): raise ValueError("Values out of allowed range.") break except ValueError as ve: print(f"Invalid input: {ve}. Please enter the values again.") return mSkill, mStamina, mLuck
# Function to test luck def testLuck(pLuck): luck_roll = random.randint(1, 6) + random.randint(1, 6) return 1 if luck_roll <= pLuck else 0
# Function to simulate one fight def simulate_fight(pSkill, pStamina, pLuck, mSkill, mStamina, mLuck): nRounds = 0 pWin = False
while True: nRounds += 1 # Check if player is dead if pStamina <= 0: break
# Check if monster is dead if mStamina <= 0: pWin = True break # Combat logic pAttack = pSkill + random.randint(1, 6) + random.randint(1, 6) mAttack = mSkill + random.randint(1, 6) + random.randint(1, 6) if pAttack > mAttack: if mStamina == 3 and pLuck >= 2: luck_bonus = testLuck(pLuck) mStamina -= (2 + luck_bonus) pLuck -= 1 else: mStamina -= 2 elif pAttack < mAttack: if pLuck >= 2: luck_bonus = testLuck(pLuck) pStamina -= (2 - luck_bonus) pLuck -= 1 else: pStamina -= 2
return pWin, pStamina, mStamina
# Get the number of fights nFights = int(input("Enter the number of combats to simulate: "))
# Initialize win counters and stamina lists player_wins = 0 monster_wins = 0 player_stamina_list = [] monster_stamina_list = []
# Get player and monster attributes pSkill, pStamina, pLuck = get_player_attributes() mSkill, mStamina, mLuck = get_monster_attributes()
# Output the entered values for confirmation print(f"Player attributes: Skill={pSkill}, Stamina={pStamina}, Luck={pLuck}") print(f"Monster attributes: Skill={mSkill}, Stamina={mStamina}, Luck={mLuck}")
# Simulate the fights for _ in range(nFights): # Make a copy of the stamina and luck for each fight current_pStamina = pStamina current_pLuck = pLuck current_mStamina = mStamina pWin, final_pStamina, final_mStamina = simulate_fight(pSkill, current_pStamina, current_pLuck, mSkill, current_mStamina, mLuck) if pWin: player_wins += 1 player_stamina_list.append(final_pStamina) else: monster_wins += 1 monster_stamina_list.append(final_mStamina)
# Calculate min, max, and mean stamina values for player and monster wins player_stamina_stats = { 'min': min(player_stamina_list, default=0), 'max': max(player_stamina_list, default=0), 'mean': statistics.mean(player_stamina_list) if player_stamina_list else 0 }
monster_stamina_stats = { 'min': min(monster_stamina_list, default=0), 'max': max(monster_stamina_list, default=0), 'mean': statistics.mean(monster_stamina_list) if monster_stamina_list else 0 }
# Report result print(f"Player Wins: {player_wins}") print(f"Monster Wins: {monster_wins}")
print("Player Stamina Stats on Win:") print(f"Min: {player_stamina_stats['min']}") print(f"Max: {player_stamina_stats['max']}") print(f"Mean: {player_stamina_stats['mean']}")
print("Monster Stamina Stats on Win:") print(f"Min: {monster_stamina_stats['min']}") print(f"Max: {monster_stamina_stats['max']}") print(f"Mean: {monster_stamina_stats['mean']}")
[/div]
|
|
|
Post by drmanhattan on Jul 4, 2024 15:40:19 GMT
monsters dont use luck, but here it wants you to enter 7-12 for that anyways
example output:
Enter the number of combats to simulate: 10000 Enter pSkill, pStamina, pLuck (comma separated): 9,18,9 Enter mSkill, mStamina, mLuck (comma separated): 8,12,7 Player attributes: Skill=9, Stamina=18, Luck=9 Monster attributes: Skill=8, Stamina=12, Luck=7 Player Wins: 9840 Monster Wins: 160 Player Stamina Stats on Win: Min: 1 Max: 18 Mean: 13.009044715447155 Monster Stamina Stats on Win: Min: 2 Max: 12 Mean: 3.625
|
|
|
Post by drmanhattan on Jul 4, 2024 15:45:13 GMT
you can modify this since it is millions of loops I, this is 1000 simulations per iteration and the iterations vary player stats vs relative monster stats
import random import statistics import pandas as pd
# Function to get monster attributes def get_monster_attributes(): mLuck = 7 return mLuck
# Function to test luck def testLuck(pLuck): luck_roll = random.randint(1, 6) + random.randint(1, 6) return 1 if luck_roll <= pLuck else 0
# Function to simulate one fight def simulate_fight(pSkill, pStamina, pLuck, mSkill, mStamina, mLuck): nRounds = 0 pWin = False
while True: nRounds += 1 # Check if player is dead if pStamina <= 0: break
# Check if monster is dead if mStamina <= 0: pWin = True break # Combat logic pAttack = pSkill + random.randint(1, 6) + random.randint(1, 6) mAttack = mSkill + random.randint(1, 6) + random.randint(1, 6) if pAttack > mAttack: if mStamina == 3 and pLuck >= 2: luck_bonus = testLuck(pLuck) mStamina -= (2 + luck_bonus) pLuck -= 1 else: mStamina -= 2 elif pAttack < mAttack: if pLuck >= 2: luck_bonus = testLuck(pLuck) pStamina -= (2 - luck_bonus) pLuck -= 1 else: pStamina -= 2
return pWin, pStamina, mStamina
# Initialize results list results = []
# Iterate over ranges of player and monster attributes for pLuck in range(7, 13): for pSkill in range(7, 13): for pStamina in range(14, 25): mLuck = 7 for mSkill in range(pSkill - 6, pSkill + 7): for mStamina in range(1, 25): player_wins = 0 monster_wins = 0 player_stamina_list = [] monster_stamina_list = []
# Run the combat simulator 1000 times for _ in range(1000): # Make a copy of the stamina and luck for each fight current_pStamina = pStamina current_pLuck = pLuck current_mStamina = mStamina pWin, final_pStamina, final_mStamina = simulate_fight(pSkill, current_pStamina, current_pLuck, mSkill, current_mStamina, mLuck) if pWin: player_wins += 1 player_stamina_list.append(final_pStamina) else: monster_wins += 1 monster_stamina_list.append(final_mStamina)
# Calculate min, max, and mean stamina values for player and monster wins if player_stamina_list: player_min_stamina = min(player_stamina_list) player_max_stamina = max(player_stamina_list) player_mean_stamina = statistics.mean(player_stamina_list) else: player_min_stamina = 0 player_max_stamina = 0 player_mean_stamina = 0
if monster_stamina_list: monster_min_stamina = min(monster_stamina_list) monster_max_stamina = max(monster_stamina_list) monster_mean_stamina = statistics.mean(monster_stamina_list) else: monster_min_stamina = 0 monster_max_stamina = 0 monster_mean_stamina = 0
# Store the results results.append([ pSkill, pStamina, pLuck, mSkill, mStamina, mLuck, player_wins, monster_wins, player_min_stamina, player_max_stamina, player_mean_stamina, monster_min_stamina, monster_max_stamina, monster_mean_stamina ])
# Create a DataFrame to hold the results columns = [ "pSkill", "pStamina", "pLuck", "mSkill", "mStamina", "mLuck", "Player Wins", "Monster Wins", "Player Min Stamina", "Player Max Stamina", "Player Mean Stamina", "Monster Min Stamina", "Monster Max Stamina", "Monster Mean Stamina" ] df = pd.DataFrame(results, columns=columns)
# Display the DataFrame print(df)
|
|
|
Post by drmanhattan on Jul 4, 2024 15:47:29 GMT
heres back to a single fight simulation, timing the loop, but also showing the frequency of final stamina results
import random import statistics import time from collections import Counter
# Function to get player attributes def get_player_attributes(): while True: try: p_attributes = input("Enter pSkill, pStamina, pLuck (comma separated): ") pSkill, pStamina, pLuck = map(int, p_attributes.split(','))
if not (7 <= pSkill <= 12 and 14 <= pStamina <= 24 and 7 <= pLuck <= 12): raise ValueError("Values out of allowed range.") break except ValueError as ve: print(f"Invalid input: {ve}. Please enter the values again.") return pSkill, pStamina, pLuck
# Function to get monster attributes def get_monster_attributes(): while True: try: m_attributes = input("Enter mSkill, mStamina, mLuck (comma separated): ") mSkill, mStamina, mLuck = map(int, m_attributes.split(','))
if not (1 <= mSkill <= 18 and 1 <= mStamina <= 48 and 7 <= mLuck <= 12): raise ValueError("Values out of allowed range.") break except ValueError as ve: print(f"Invalid input: {ve}. Please enter the values again.") return mSkill, mStamina, mLuck
# Function to test luck def testLuck(pLuck): luck_roll = random.randint(1, 6) + random.randint(1, 6) return 1 if luck_roll <= pLuck else 0
# Function to simulate one fight def simulate_fight(pSkill, pStamina, pLuck, mSkill, mStamina, mLuck): nRounds = 0 pWin = False
while True: nRounds += 1 # Check if player is dead if pStamina <= 0: break
# Check if monster is dead if mStamina <= 0: pWin = True break # Combat logic pAttack = pSkill + random.randint(1, 6) + random.randint(1, 6) mAttack = mSkill + random.randint(1, 6) + random.randint(1, 6) if pAttack > mAttack: if mStamina == 3 and pLuck >= 2: luck_bonus = testLuck(pLuck) mStamina -= (2 + luck_bonus) pLuck -= 1 else: mStamina -= 2 elif pAttack < mAttack: if pLuck >= 2: luck_bonus = testLuck(pLuck) pStamina -= (2 - luck_bonus) pLuck -= 1 else: pStamina -= 2
return pWin, pStamina, mStamina
# Get the number of fights nFights = int(input("Enter the number of combats to simulate: "))
# Initialize win counters and stamina lists player_wins = 0 monster_wins = 0 player_stamina_list = [] monster_stamina_list = [] player_stamina_counter = Counter() monster_stamina_counter = Counter()
# Get player and monster attributes pSkill, pStamina, pLuck = get_player_attributes() mSkill, mStamina, mLuck = get_monster_attributes()
# Output the entered values for confirmation print(f"Player attributes: Skill={pSkill}, Stamina={pStamina}, Luck={pLuck}") print(f"Monster attributes: Skill={mSkill}, Stamina={mStamina}, Luck={mLuck}")
# Simulate the fights total_duration = 0
for _ in range(nFights): # Make a copy of the stamina and luck for each fight current_pStamina = pStamina current_pLuck = pLuck current_mStamina = mStamina start_time = time.time() pWin, final_pStamina, final_mStamina = simulate_fight(pSkill, current_pStamina, current_pLuck, mSkill, current_mStamina, mLuck) end_time = time.time()
duration = end_time - start_time total_duration += duration if pWin: player_wins += 1 player_stamina_list.append(final_pStamina) player_stamina_counter[final_pStamina] += 1 else: monster_wins += 1 monster_stamina_list.append(final_mStamina) monster_stamina_counter[final_mStamina] += 1
# Calculate min, max, and mean stamina values for player and monster wins player_stamina_stats = { 'min': min(player_stamina_list, default=0), 'max': max(player_stamina_list, default=0), 'mean': statistics.mean(player_stamina_list) if player_stamina_list else 0 }
monster_stamina_stats = { 'min': min(monster_stamina_list, default=0), 'max': max(monster_stamina_list, default=0), 'mean': statistics.mean(monster_stamina_list) if monster_stamina_list else 0 }
# Calculate average duration per fight average_duration = total_duration / nFights if nFights > 0 else 0
# Report result print(f"Player Wins: {player_wins}") print(f"Monster Wins: {monster_wins}")
print("Player Stamina Stats on Win:") print(f"Min: {player_stamina_stats['min']}") print(f"Max: {player_stamina_stats['max']}") print(f"Mean: {player_stamina_stats['mean']}")
print("Monster Stamina Stats on Win:") print(f"Min: {monster_stamina_stats['min']}") print(f"Max: {monster_stamina_stats['max']}") print(f"Mean: {monster_stamina_stats['mean']}")
print(f"Average duration per fight: {average_duration:.6f} seconds")
print("Player Stamina Distribution on Win:") print(dict(player_stamina_counter))
print("Monster Stamina Distribution on Win:") print(dict(monster_stamina_counter))
|
|
|
Post by drmanhattan on Jul 4, 2024 15:47:50 GMT
Enter the number of combats to simulate: 10000 Enter pSkill, pStamina, pLuck (comma separated): 9,18,9 Enter mSkill, mStamina, mLuck (comma separated): 8,12,7 Player attributes: Skill=9, Stamina=18, Luck=9 Monster attributes: Skill=8, Stamina=12, Luck=7 Player Wins: 9831 Monster Wins: 169 Player Stamina Stats on Win: Min: 1 Max: 18 Mean: 13.001118909571764 Monster Stamina Stats on Win: Min: 2 Max: 12 Mean: 3.668639053254438 Average duration per fight: 0.000033 seconds Player Stamina Distribution on Win: {13: 989, 12: 766, 14: 1087, 7: 289, 11: 620, 10: 496, 9: 397, 16: 1234, 15: 1203, 17: 1111, 18: 599, 5: 141, 4: 122, 6: 219, 2: 75, 3: 86, 1: 54, 8: 343} Monster Stamina Distribution on Win: {2: 81, 4: 49, 6: 28, 8: 9, 12: 1, 10: 1}
|
|
|
Post by drmanhattan on Jul 4, 2024 15:49:25 GMT
My aim is to boil down combats to a single %age random roll, and an average number of stamina lost in the case of a win. This could be printed out in a table based on the player stat
|
|
|
Post by philsadler on Aug 9, 2024 13:55:01 GMT
What do we do with all that?
|
|
|
Post by drmanhattan on Aug 11, 2024 9:41:42 GMT
run it in your python interpreter ![:)](//storage.proboards.com/forum/images/smiley/smiley.png)
|
|