Skip to content

Instantly share code, notes, and snippets.

@betatim
Created April 7, 2014 17:39
Show Gist options
  • Select an option

  • Save betatim/10024797 to your computer and use it in GitHub Desktop.

Select an option

Save betatim/10024797 to your computer and use it in GitHub Desktop.
import random
random.seed(12345)
class Dice(object):
def __init__(self, sides=6):
print "creating a dice with %i sides."%sides
self.sides = sides
def roll(self):
return random.choice(range(1, self.sides+1))
class EightBall(object):
COLOUR = "black"
def __init__(self, messages=["go go go",
"maybe tmz",
"No!",
"Yes!",
"ask again"]):
self.messages = messages
self.dice = Dice(len(messages))
def shake(self):
return "The %s eight ball says: %s"%(self.COLOUR,
self.messages[self.dice.roll()-1])
if __name__ == "__main__":
sixer = Dice()
sevener = Dice(7)
coin = Dice(2)
for n in xrange(10):
print "sixer:", sixer.roll()
for n in xrange(10):
print "sevener:", sevener.roll()
for n in xrange(10):
print "coin:", coin.roll()
eight = EightBall()
print eight.shake()
print eight.shake()
eight_ = EightBall()
print eight_.shake()
print eight_.shake()
print "changing colour to green"
EightBall.COLOUR = "green"
print eight.shake()
print eight_.shake()
print "one blue, one green"
eight.COLOUR= "blue"
print eight.shake()
print eight_.shake()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment