jweinst1
6/6/2015 - 8:11 AM

A simple class game that has a function that tries to pick what the next randomly picked ball will be based on the past balls picked, called

A simple class game that has a function that tries to pick what the next randomly picked ball will be based on the past balls picked, called player_turn. Can run a game of N max turns.

import random
class Gambling_Model (object):
	
	def __init__(self):
		self.bag = ['Red', 'Yellow', 'Blue']
		self.cache = []
		self.turns, self.player, self.comp = 0, 0, 0
		self.playerguess = None 
	def start_game(self, maxturns):
		self.maxturns = maxturns
		Gambling_Model.player_turn(self)
	def draw_ball(self):
		self.cache.append(self.bag[random.randrange(0, 3)])
		if self.cache[len(self.cache) - 1] == self.playerguess:
			self.player += 1
			self.turns += 1
			if self.turns == self.maxturns:
				Gambling_Model.gameover(self)
			else:
				Gambling_Model.player_turn(self)
		else:
			self.comp += 1
			self.turns += 1
			if self.turns == self.maxturns:
				Gambling_Model.gameover(self)
			else:
				Gambling_Model.player_turn(self)
	def player_turn(self):
		if self.cache == []:
			self.playerguess = self.bag[random.randrange(0, 3)]
			Gambling_Model.draw_ball(self)
		else:
			frequency = {self.cache.count(x):x for x in self.cache}
			self.playerguess = frequency[max(frequency.keys())]
			Gambling_Model.draw_ball(self)
	def gameover(self):
		print "Final Score: player = %d, computer = %d" %(self.player, self.comp)
		self.turns, self.player, self.comp = 0, 0, 0
		self.playerguess = None
		self.cache = []