Last active
April 29, 2016 20:57
-
-
Save wray/3f56149db45d954beed20764ad1b7c1e to your computer and use it in GitHub Desktop.
Multi Sided Die
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
# | |
# Let's go back to some games and start | |
# playing with a multi-sided dice. | |
# | |
# Think of the dice and how we interact with the dice. | |
# We roll it and then we read the number. | |
# | |
# The self keyword is similar to a | |
# regular variable, except it is a variable for every object | |
# created. A way to access the object in the class definition code. | |
# | |
# And a Class itself can be considered a container for functions | |
# where the class is defined once and then many objects are created | |
# for each class. Functions in a class are called methods. | |
# | |
# So, a simple class for a multi sided die would look like this: | |
from random import randint | |
class MultiSidedDie: | |
# | |
# Most classes will have an initialization method. | |
# | |
# 'self' is a special parameter that is a reference | |
# to the object (instance of this class). | |
def __init__(self, sides): | |
self.sides = sides | |
self.value = 1 | |
def roll(self): | |
self.value = randint(1,self.sides) | |
def getValue(self): | |
return self.value | |
# Example usage: | |
my_die = MultiSidedDie(6) | |
print(my_die.getValue()) | |
print("Roll the die!") | |
my_die.roll() | |
print(my_die.getValue()) | |
print("Roll the die!") | |
my_die.roll() | |
print(my_die.getValue()) | |
my_other_die = MultiSidedDie(6) | |
print("Roll my other die!") | |
my_other_die.roll() | |
print("My dice:") | |
print(my_die.getValue(),my_other_die.getValue()) | |
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
# | |
# Some fun stuff | |
# | |
# Challenge: | |
# Smart Die | |
# | |
# Add an instance variable to track roll history. | |
# | |
from random import randint | |
class MultiSidedDie: | |
# | |
# Most classes will have an initialization method. | |
# | |
# 'self' is a special parameter that is a reference | |
# to the object (instance of this class). | |
def __init__(self, sides): | |
self.sides = sides | |
self.value = 1 | |
self.roll_history = [] | |
def roll(self): | |
self.value = randint(1,self.sides) | |
self.roll_history.append(self.value) | |
def getValue(self): | |
return self.value | |
def getRollHistory(self): | |
return self.roll_history | |
# Example usage: | |
my_die = MultiSidedDie(6) | |
print(my_die.getValue()) | |
print("Roll the die!") | |
my_die.roll() | |
print(my_die.getValue()) | |
print("Roll the die!") | |
my_die.roll() | |
print(my_die.getValue()) | |
my_other_die = MultiSidedDie(6) | |
print("Roll my other die!") | |
my_other_die.roll() | |
print("My Die history") | |
print(my_die.getRollHistory()) | |
print("My Other Die History") | |
print(my_other_die.getRollHistory()) |
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
# | |
# Some fun stuff | |
# | |
# Challenge: | |
# Keep on Rolling | |
# | |
# Keep rolling in a loop -- similar to the choose your own adventure. | |
# Make the game more usable | |
# | |
from random import randint | |
class MultiSidedDie: | |
# | |
# Most classes will have an initialization method. | |
# | |
# 'self' is a special parameter that is a reference | |
# to the object (instance of this class). | |
def __init__(self, sides): | |
self.sides = sides | |
self.value = 1 | |
self.roll_history = [] | |
def roll(self): | |
self.value = randint(1,self.sides) | |
self.roll_history.append(self.value) | |
def getValue(self): | |
return self.value | |
def getRollHistory(self): | |
return self.roll_history | |
# Example usage: | |
my_die = MultiSidedDie(6) | |
my_other_die = MultiSidedDie(6) | |
roll_again = "y" | |
while roll_again == "y": | |
print("Dice Rolling...") | |
my_die.roll() | |
my_other_die.roll() | |
print("My Dice: " + str(my_die.getValue())) | |
print("My Other Dice: " + str(my_other_die.getValue())) | |
roll_again = raw_input("Press 'y' to roll again ") | |
print("My Dice History: ") | |
print(my_die.getRollHistory()) | |
print("My Other Dice History: ") | |
print(my_other_die.getRollHistory()) |
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
# | |
# Some fun stuff | |
# | |
# Challenge: | |
# Big Data | |
# | |
# Track Dice Stats | |
# | |
from random import randint | |
class MultiSidedDie: | |
# | |
# Most classes will have an initialization method. | |
# | |
# 'self' is a special parameter that is a reference | |
# to the object (instance of this class). | |
def __init__(self, sides): | |
self.sides = sides | |
self.value = 1 | |
self.roll_history = [] | |
self.die_stats = {} | |
for side in range(self.sides): | |
self.die_stats[side+1] = 0 | |
def roll(self): | |
self.value = randint(1,self.sides) | |
self.roll_history.append(self.value) | |
self.die_stats[self.value] += 1 | |
def getValue(self): | |
return self.value | |
def getRollHistory(self): | |
return self.roll_history | |
def getDieStats(self): | |
return self.die_stats | |
# Example usage: | |
my_die = MultiSidedDie(6) | |
my_other_die = MultiSidedDie(6) | |
roll_again = "y" | |
while roll_again == "y": | |
print("Dice Rolling...") | |
my_die.roll() | |
my_other_die.roll() | |
print("My Dice: " + str(my_die.getValue())) | |
print("My Other Dice: " + str(my_other_die.getValue())) | |
roll_again = raw_input("Press 'y' to roll again ") | |
print("My Dice History: ") | |
print(my_die.getRollHistory()) | |
print("My Other Dice History: ") | |
print(my_other_die.getRollHistory()) | |
print("My Dice Stats:") | |
print(my_die.getDieStats()) |
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
# | |
# Some fun stuff | |
# | |
# Challenge: | |
# Unfair Die? | |
# | |
# Use Dice Stats | |
# | |
from random import randint | |
class MultiSidedDie: | |
# | |
# Most classes will have an initialization method. | |
# | |
# 'self' is a special parameter that is a reference | |
# to the object (instance of this class). | |
def __init__(self, sides): | |
self.sides = sides | |
self.value = 1 | |
self.roll_history = [] | |
self.die_stats = {} | |
for side in range(self.sides): | |
self.die_stats[side+1] = 0 | |
def roll(self): | |
self.value = randint(1,self.sides) | |
self.roll_history.append(self.value) | |
self.die_stats[self.value] += 1 | |
def getValue(self): | |
return self.value | |
def getRollHistory(self): | |
return self.roll_history | |
def getDieStats(self): | |
return self.die_stats | |
def getSideProb(self,side): | |
side = int(side) | |
if self.die_stats.has_key(side): | |
print(self.die_stats[side]) | |
prob = self.die_stats[side] / (len(self.roll_history) * 1.0) | |
print(prob) | |
return prob * 100 | |
else: | |
return 0 | |
# Example usage: | |
my_die = MultiSidedDie(6) | |
my_other_die = MultiSidedDie(6) | |
roll_again = "y" | |
while roll_again == "y": | |
print("Dice Rolling...") | |
my_die.roll() | |
my_other_die.roll() | |
print("My Dice: " + str(my_die.getValue())) | |
print("My Other Dice: " + str(my_other_die.getValue())) | |
roll_again = raw_input("Press 'y' to roll again ") | |
print("My Dice History: ") | |
print(my_die.getRollHistory()) | |
print("My Other Dice History: ") | |
print(my_other_die.getRollHistory()) | |
print("My Dice Stats:") | |
print(my_die.getDieStats()) | |
side = raw_input("Type in a side to check probability ") | |
prob = my_die.getSideProb(side) | |
print("My Dice Prob - " + str(side) + " - " + str(prob)) | |
prob = my_other_die.getSideProb(side) | |
print("My Other Dice Prob - " + str(side) + "-" + str(prob)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment