Created
October 8, 2013 20:50
-
-
Save robwiss/6891423 to your computer and use it in GitHub Desktop.
Analysis of the advantage given by bunkers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def dieroll(n): | |
"""Roll an n-sided die and return result""" | |
return int(random.random() * n) + 1 | |
def roll_once( | |
a_army, d_army, a_dice=None, d_dice=None, firepower=False, ambush=False, | |
double_casualties=False, bunker=False, atkrad=False, dfndrad=False | |
): | |
# bail if the attacker or defender doesn't have enough guys to roll | |
if a_army < 2 or d_army < 1: | |
return (a_army, d_army) | |
a_dice_requested = a_dice | |
d_dice_requested = d_dice | |
# figure out how many dice are allowed | |
a_dice_modifier = 0 | |
if firepower: | |
a_dice_modifier += 1 | |
if atkrad: | |
a_dice_modifier -= 1 | |
a_dice = min(a_army - 1, 3) + a_dice_modifier | |
if a_dice == 0: | |
return (a_army, d_army) | |
d_dice_modifier = 0 | |
if bunker: | |
d_dice_modifier += 1 | |
if dfndrad: | |
d_dice_modifier -= 1 | |
d_dice = min(d_army, 2) + d_dice_modifier | |
if d_dice == 0: | |
return (a_army, d_army) | |
# if the user has requested an allowable amount of dice, set it | |
if 0 < a_dice_requested <= a_dice: | |
a_dice = a_dice_requested | |
if 0 < d_dice_requested <= d_dice: | |
d_dice = d_dice_requested | |
# roll the dice | |
a_rolls = sorted([dieroll(6) for i in xrange(a_dice)]) | |
d_rolls = sorted([dieroll(6) for i in xrange(d_dice)]) | |
# figure out the number of casualties. Should be equal to the minimum | |
# amount of dice rolled. However, defender casualties are capped at 2 b/c | |
# the bonus die from the bunker doesn't count toward their possible losses. | |
num_casualties = min( a_dice, min(d_dice, 2) ) | |
for i in xrange(num_casualties): | |
if ambush: | |
a_wins = a_rolls[-1] >= d_rolls[-1] | |
else: | |
a_wins = a_rolls[-1] > d_rolls[-1] | |
if a_wins: | |
if double_casualties: | |
d_army -= 2 | |
else: | |
d_army -= 1 | |
else: | |
a_army -= 1 | |
a_rolls.pop() | |
d_rolls.pop() | |
return (a_army, d_army) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment