Skip to content

Instantly share code, notes, and snippets.

@craigmaloney
Created October 13, 2014 11:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigmaloney/8d4cb4f733d93584650e to your computer and use it in GitHub Desktop.
Save craigmaloney/8d4cb4f733d93584650e to your computer and use it in GitHub Desktop.
Grossly over-simplified D&D combat algorithm
#!/usr/bin/env python
import random
LARGE_SHORT_SWORD_DAMAGE = 8
def roll_d(die_size):
return random.randint(1, die_size)
class Goblin(object):
def __init__(self):
self.hit_points = roll_d(8) + 1 # 1d8+1
self.armor_class = 15 + 1 # AC 15 + 1 Dex, no armor, no shield
def attack_roll(self):
roll = roll_d(20) + 2
print "Goblin rolls: {roll}".format(roll=roll)
return roll
def deal_damage(self):
roll = roll_d(6)
print "Goblin deals {roll} damage".format(roll=roll)
return roll
def take_damage(self, damage):
self.hit_points = self.hit_points - damage
print "Goblin takes {damage} damage".format(damage=damage)
def dead(self):
if self.hit_points <= 0:
return True
return False
class Player(object):
# These are loosely based on the following fighter:
# http://paizo.com/pathfinderRPG/prd/npcCodex/iconic/valeros.html
def __init__(self):
self.hit_points = 16
self.armor_class = 17
self.strength = 16
self.strength_modifier = 3
self.dexterity = 15
self.constitution = 14
self.intelligence = 12
self.wisdom = 10
self.charisma = 11
def attack_roll(self):
roll = roll_d(20)
attack = roll + self.strength_modifier
print "Player rolls: {roll}, which is {attack} with strength modifier".\
format(
roll=roll,
attack=attack)
return attack
def deal_damage(self):
roll = roll_d(8) + 3
print "Player deals {roll} damage".format(roll=roll)
return roll
def take_damage(self, damage):
self.hit_points = self.hit_points - damage
print "Player takes {damage} damage".format(damage=damage)
def dead(self):
if self.hit_points <= 0:
return True
return False
def combat():
player = Player()
goblin = Goblin()
fighting = True
while fighting:
if player.attack_roll() >= goblin.armor_class:
damage = player.deal_damage()
goblin.take_damage(damage)
if goblin.dead():
print "Goblin is dead"
fighting = False
else:
print "Player misses"
if goblin.dead() is False and fighting is True:
if goblin.attack_roll() >= player.armor_class:
damage = goblin.deal_damage()
player.take_damage(damage)
if player.dead():
print "Player is dead"
fighting = False
else:
print "Goblin misses"
if __name__ == "__main__":
combat()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment