This craps game simulator is based on someone else's posted Python code that I've redone for practice. I'm interested in improving readability and making it more pythonic. I used unittest
instead of nose
because I'm using an online IDE.
from random import randrange
import unittest
class CrapsGame:
def __init__(self):
self.outcomes = {'2':False,'3':False,'12':False,'7':True,'11':True,
'4&4':True,'5&5':True,'6&6':True,
'8&8':True,'9&9':True,'10&10':True,
'4&7':False,'5&7':False,'6&7':False,
'8&7':False,'9&7':False,'10&7':False}
def play(self, roll_dice):
comeOut = str(roll_dice())
print 'began game with ' + comeOut
if comeOut in self.outcomes:
return self.outcomes[comeOut]
while True:
point = str(roll_dice())
print 'next roll is ' + point
state = comeOut+'&'+point
if state in self.outcomes:
return self.outcomes[state]
class CrapsTest(unittest.TestCase):
def testWinComeOut(self):
game = CrapsGame()
self.assertEquals(game.play(lambda: 7), True)
def testLoseComeOut(self):
game = CrapsGame()
self.assertEquals(game.play(lambda: 2), False)
def testWinPoint(self):
game = CrapsGame()
rollData = [5,6,5]
self.assertEquals(game.play(lambda: rollData.pop()), True)
def testLosePoint(self):
game = CrapsGame()
rollData = [7,5]
self.assertEquals(game.play(lambda: rollData.pop()), False)
if __name__ == '__main__':
unittest.main()
come_out
instead ofcomeOut
. \$\endgroup\$