# P-3 MCS 260 Monday 8 March : simulate slot game # The goal of this project is to simulate a slot machine game. # The slot machines we consider have a varying number of reels, # between 2 and 6. This implies that the slot machine spins # numbers of 2, 3, 4, 5, or 6 digits. # Numbers displayed to the player are padded by zeroes from the left. # For example, if the number of reels equals 4 and the random number # generator returns 93, then 0093 is shown to the user. # The user of the program is first prompted to enter the number of reels. # This number stays fixed during the whole duration of the game. # At the start of the game, the player enters the number of tokens. # Each spin costs the player one token. The game ends when the user # chooses not to continue, or when the balance of tokens equals zero. # The player hits the jackpot if the same digit occurs exactly as many # times as the number of reels. We call the largest number of the same # digits in one number the weight. # The payoff is the number of tokens given as a reward to the player. # For number of reels r and weight w, the payoff is as follows: # If w equals the number of same digits # that occur in one spin, then the payoff (number of tokens given as # reward to the player) is as follows: # w=1 w=2 w=3 w=4 w=5 w=6 # r=2 0 10 -- -- -- -- # r=3 0 2 46 -- -- -- # r=4 0 1 8 253 -- -- # r=5 0 1 3 10 1000 -- # r=6 0 0 1 11 1002 18037 # At each spin, the player is shown the generated number, # the weight of the number, and the corresponding payoff, # all on the same line. In the next line, the program shows # the player the accumulated balance of tokens, and asks if # the player wants to continue the game. from random import randint def generate_number(R): """ Generates a number of R digits. Returns a string of length R, padding the number from the left with zeroes. """ n = randint(0,10**R-1) f = '%0' + ('%d' % R) + 'd' s = f % n return s def equal_digits(s): """ Returns the maximum number of equal symbols in the string s. """ L = [e for e in s] D = {} for c in L: if D.has_key(c): D[c] = D[c] + 1 else: D[c] = 1 return max(D.values()) def payout(R,e): """ Returns the payout for the number e of equal digits, where R is the number of reels. """ D = {2:{1:0,2:10},\ 3:{1:0,2:2,3:46},\ 4:{1:0,2:1,3:8,4:253},\ 5:{1:0,2:1,3:3,4:10,5:1000},\ 6:{1:0,2:0,3:1,4:11,5:1002,6:18037}} return D[R][e] def show_spin(s,e,p): """ Shows user result of a spin. On input is s a string with a number and e, the number of equal digits. """ a = 'spin ' + s b = ' has weight %d' % e c = ' with award %d' % p print a + b + c def main(): """ Simulates playing on a slot machine. """ print 'welcome to our slot machine' R = input('-> how many reels (2, 3, 4, 5, or 6) ? ') t = input('-> give the number of tokens : ') while (t > 0): g = generate_number(R) e = equal_digits(g) p = payout(R,e) show_spin(g,e,p) t = t - 1 + p s = '-> your current balance : %d, ' % t if t == 0: break ans = raw_input(s + 'continue ? (y/n) ') if ans != 'y': break print 'ending balance :', t main()