Skip to content

Instantly share code, notes, and snippets.

@robwiss
Created October 8, 2013 20:50
Show Gist options
  • Save robwiss/6891423 to your computer and use it in GitHub Desktop.
Save robwiss/6891423 to your computer and use it in GitHub Desktop.
Analysis of the advantage given by bunkers
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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