class Simulation: def __init__(self, games=5000, max_games=10000, default_games=5000): self.points = {} self.played = {} self.won = {} self.max_games = max_games self.default_games = default_games self.games = self.default_games if self.games > self.max_games: print '

Can\'t play %s games, playing %s games instead' % (self.games,self.max_games) self.games = self.max_games else: self.games = games def run(self): from CrapsGame import CrapsGame a = CrapsGame() for i in range(0,self.games): a.play() length = str(a.length) self.increment(self.played,length) if (a.outcome == 'won'): self.increment(self.won,length) def show_form(self): print '
' print '' print '

Number of games (1-%d)' % self.max_games print '' % self.default_games print '

' print '' print '
' def print_stats(self): cumulative_odds = 0 print '' print '

Fast Jack\'s Floating Hall of Craps
' print 'Stats on %s games

' % self.games print '' print '' print '' print '' print '' print '' print '' print '' keys = self.played.keys() for i in range(0,len(keys)): keys[i] = int(keys[i]) keys.sort() total_rounds_played = 0 for key in keys: length = key total_rounds_played = total_rounds_played + (key * self.played[str(key)]) key = str(key) if self.played.has_key(key): number_played = self.played[key] percent_played = float(number_played) / self.games * 100 else: number_played = 0 if self.won.has_key(key): number_won = self.won[key] percent_won = float(number_won) / number_played * 100 else: number_won = 0 percent_won = 0 number_lost = number_played - number_won if number_lost == 0: percent_lost = 0 else: percent_lost = float(number_lost) / number_played * 100 cumulative_odds = cumulative_odds + ((float(number_won) / self.games) * 100) print '' print '' % length print '' % (number_played, percent_played) print '' % (number_won, percent_won) print '' % (number_lost, percent_lost) print '' % (cumulative_odds) print '' print '
Length of GameNumber of GamesGames wonGames lostCumulative odds
%d%d (%.2f%%)%d (%.2f%%)%d (%.2f%%)%.2f%%
' print '

Total probability of winning: %.2f%%

' % cumulative_odds def increment(self,dict,key): if dict.has_key(key): dict[key] = dict[key] + 1 else: dict[key] = 1