Skip to content

Instantly share code, notes, and snippets.

@gerald-kim
Created August 7, 2010 11:50
Show Gist options
  • Save gerald-kim/512730 to your computer and use it in GitHub Desktop.
Save gerald-kim/512730 to your computer and use it in GitHub Desktop.
import random
GOAT = 0
CAR = 1
class Door:
def __init__(self, index):
self.index = index
self.gift = GOAT
def putCar(self):
self.gift = CAR
def hasCar(self):
return self.gift == CAR
class Game:
def __init__(self):
self.doors = [Door(0), Door(1), Door(2)]
self.doors[random.randint(0,2)].putCar()
def choice(self):
self.choice = random.randint(0,2)
# print "Choice: ", self.choice
def didWon(self):
return self.doors[self.choice].gift == CAR
def eliminateGoatThenChoiceAnotherDoor(self):
remainingDoors = self.doors[:]
del remainingDoors[self.choice]
if remainingDoors[0].hasCar():
del remainingDoors[1]
else:
del remainingDoors[0]
self.choice = remainingDoors[0].index
if __name__ == '__main__':
winCountForStick = 0
COUNT = 1000
for i in range(COUNT):
g = Game()
g.choice()
if g.didWon():
winCountForStick = winCountForStick + 1
winCountForSwap = 0
for i in range(COUNT):
g = Game()
g.choice()
g.eliminateGoatThenChoiceAnotherDoor()
if g.didWon():
winCountForSwap = winCountForSwap + 1
print "Did %d games..." % (COUNT)
print "win for stick ", winCountForStick
print "win for swap", winCountForSwap
import unittest
from choice import *
class GameInitTest(unittest.TestCase):
def testInit(self):
g = Game()
self.assertEqual(3, len(g.doors))
def testChoice(self):
for x in range(10):
g = Game()
g.choice()
self.assertTrue(g.choice >= 0)
self.assertTrue(g.choice <= 2)
def testDidWon(self):
g = Game()
g.doors[0].gift = CAR
g.choice = 0
# g.eliminateGoat()
self.assertTrue(g.didWon())
def testEliminateGoatAndChoiceAnotherDoor1(self):
g = Game()
g.doors = [Door(0), Door(1), Door(2)]
g.doors[1].putCar()
g.choice = 0
g.eliminateGoatThenChoiceAnotherDoor()
self.assertEqual(1, g.choice)
def testEliminateGoatAndChoiceAnotherDoor2(self):
g = Game()
g.doors = [Door(0), Door(1), Door(2)]
g.doors[0].putCar()
g.choice = 0
g.eliminateGoatThenChoiceAnotherDoor()
self.assertEqual(2, g.choice)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment