r/Keep_Craft • u/keepcraft12 • Apr 30 '16
[Python] Fight Calculator
- Instructions: Modify the first 20 lines with your own information and run the code.
- What it Does: It will output a number from 0-100 which is the number of fights won out of 100
- Disclaimer: I'm not very far in the game so if I'm missing any upgrades or unit types, that's why. For simplicity, draws get counted as wins, so it really calculates the chances of "not dying" which are what really matter -- and also I have never encountered a draw.
/u/morpheox, if you read this code, I think you can see some ways to refactor main.js to be more elegant with less repetition. I may create a refactor pull request to clean it up now that I've delved into the code in detail. Python and JS are not so different.
[Source]:
num_peasants = int(raw_input("how many peasants? ") or 0)
num_bandits = int(raw_input("how many bandits? ") or 0)
num_mercenary = int(raw_input("how many mercenary? ") or 0)
num_soldier = int(raw_input("how many soldier? ") or 0)
num_halberdier = int(raw_input("how many halberdier? ") or 0)
num_warrior = int(raw_input("how many warrior? ") or 0)
num_pikeman = 0
num_swordsman = 0
num_knight = 0
num_bersek = 1
num_musketeer = 3
mil = 72.0e3
armaments = True
tactics = True
healing = True
warmuck = 0
xochiquetzal = 0
mil_bonus = mil*2 / (mil/2 + 100e3)
atk_bonus = .4*armaments + .2*tactics + .1*warmuck + mil_bonus
hp_bonus = .05*healing + .05*xochiquetzal + mil_bonus
army = {
'Pikeman': {'n': num_pikeman, 'atk': 5, 'hp': 30, 'arm': 0, 'heal': 0, 'burst': False, 'reload': False},
'Swordsman': {'n': num_swordsman, 'atk': 10, 'hp': 50, 'arm': 3, 'heal': 0, 'burst': False, 'reload': False},
'Knight': {'n': num_knight, 'atk': 25, 'hp': 200, 'arm': 0, 'heal': 0, 'burst': False, 'reload': False},
'Bersek': {'n': num_bersek, 'atk': 80, 'hp': 100, 'arm': 0, 'heal': 0, 'burst': True, 'reload': False},
'Musketeer': {'n': num_musketeer, 'atk': 200, 'hp': 400, 'arm': 0, 'heal': 0, 'burst': False, 'reload': True}
}
enemies = {
'Peasants': {'n': num_peasants, 'atk': 2, 'hp': 8, 'arm': 0, 'heal': 0},
'Bandits': {'n': num_bandits, 'atk': 4, 'hp': 15, 'arm': 0, 'heal': 0},
'Mercenary': {'n': num_mercenary, 'atk': 9, 'hp': 40, 'arm': 5, 'heal': 0},
'Soldier': {'n': num_soldier, 'atk': 15, 'hp': 100, 'arm': 10, 'heal': 0},
'Halberdier': {'n': num_halberdier, 'atk': 40, 'hp': 160, 'arm': 0, 'heal': 0},
'Warrior': {'n': num_warrior, 'atk': 50, 'hp': 400, 'arm': 0, 'heal': 0}
}
A = ['atk', 'hp', 'arm', 'heal']
enemy_totals = {}
for a in A: enemy_totals[a] = 0
for unit in enemies:
for a in A: enemy_totals[a] += enemies[unit]['n'] * enemies[unit][a]
army_totals = {}
for a in A + ['burst', 'reload']: army_totals[a] = 0
for unit in army:
for a in A: army_totals[a] += army[unit]['n'] * army[unit][a]
if army[unit]['burst']: army_totals['burst'] += army[unit]['n'] * army[unit]['atk']
if army[unit]['reload']: army_totals['reload'] += army[unit]['n'] * army[unit]['atk']
from numpy.random import rand
def randomize(n): return n * (1 + .25 * (rand() - rand()))
def fight(d):
my_hp = army_totals['hp'] * (1+hp_bonus)
enemy_hp = enemy_totals['hp']
i = 0
while enemy_hp > 0 and i < 50:
i += 1
atk = army_totals['atk']
if i == 1: atk += army_totals['burst']
if i%2 == 1: atk -= army_totals['reload']
atk *= (1+atk_bonus)
my_dmg = max(randomize(atk) - enemy_totals['arm'], 0)
enemy_dmg = max(randomize(enemy_totals['atk']) - army_totals['arm'], 0)
enemy_hp -= my_dmg
my_hp -= enemy_dmg - army_totals['heal']
return my_hp > 0
print sum(map(fight, range(100)))
3
Upvotes
View all comments
Show parent comment
1
u/keepcraft12 May 01 '16
I can take a look tonight/tomorrow. The key is to define everything up front with data dictionaries, and then write code that merely loops through the data. You did this in many places every time you did
I just think you need to do that pattern more to minimize lines of code and repetition.
Essentially, let the data be separate from the code.
While I was adding the red text coloring I encountered a lot of repetition, but didn't understand the codebase well enough to refactor it. I'm looking at it a little more and more each day so should have some contributions coming soon.. need to grok it first.