|
| 1 | +center = function(s,n) |
| 2 | + h = floor((n - s.len)/2) |
| 3 | + s = " " * h + s + " " * n |
| 4 | + return s[:n] |
| 5 | +end function |
| 6 | + |
| 7 | +Machine = {"symbols": ["Bar", "Bell", "Orange", "Lemon", "Plum ", "Cherry"]} |
| 8 | +Machine.money = 0 |
| 9 | +Machine.bet = 0 |
| 10 | +Machine.playing = true |
| 11 | +Machine.reels = [0,0,0] |
| 12 | +Machine.results = null |
| 13 | + |
| 14 | +Machine.reelsSpin = function(n, msg, row) |
| 15 | + env = [1,.95,.9,.85,.25,.2,.15,.1,.05,0] |
| 16 | + bell = new Sound |
| 17 | + bell.init .1, 800, env, Sound.sineWave |
| 18 | + for i in range(1,2) |
| 19 | + s1 = new Sound |
| 20 | + s1.init .2, i * 800 + 800, env, Sound.sineWave |
| 21 | + bell.mix(s1, 1 / (2 ^ i)) |
| 22 | + end for |
| 23 | + for i in range(1,n) |
| 24 | + bell.play |
| 25 | + ix = 0 |
| 26 | + while bell.isPlaying |
| 27 | + text.row = row |
| 28 | + ix = (ix + 1) % self.symbols.len |
| 29 | + print msg + center(self.symbols[ix],8) |
| 30 | + end while |
| 31 | + end for |
| 32 | +end function |
| 33 | + |
| 34 | +Machine.makeBet = function() |
| 35 | + while true |
| 36 | + bet = floor(input("Your bet? ").val) |
| 37 | + if bet > 100 then |
| 38 | + print "House limits are $100" |
| 39 | + else if bet < 1 then |
| 40 | + print "Minimum bet is $1" |
| 41 | + else |
| 42 | + break |
| 43 | + end if |
| 44 | + end while |
| 45 | + Machine.bet = bet |
| 46 | +end function |
| 47 | + |
| 48 | +Machine.pullHandle = function |
| 49 | + if text.row < 3 then |
| 50 | + print; print |
| 51 | + text.row = text.row + 2 |
| 52 | + end if |
| 53 | + row = text.row |
| 54 | + msg = "" |
| 55 | + for i in range(0,2) |
| 56 | + self.reelsSpin(5 + 5 * (i==0), msg, row) |
| 57 | + wait .1 |
| 58 | + symIx = floor(rnd * self.symbols.len) |
| 59 | + msg += center(self.symbols[symIx],9) |
| 60 | + self.reels[i] = symIx |
| 61 | + text.row = row |
| 62 | + print msg |
| 63 | + end for |
| 64 | +end function |
| 65 | + |
| 66 | +Machine.winnings = function |
| 67 | + bet = Machine.bet |
| 68 | + barIdx = self.symbols.indexOf("Bar") |
| 69 | + numBars = 0 |
| 70 | + multiples = 0 |
| 71 | + for i in range(0,2) |
| 72 | + numBars += (self.reels[i] == barIdx) |
| 73 | + multiples += (self.reels[i] == self.reels[(i + 1) % 3]) |
| 74 | + end for |
| 75 | + |
| 76 | + if numBars == 3 then return {"won": bet * 101, "msg": "***Jackpot***"} |
| 77 | + if numBars == 2 then return {"won": bet * 6, "msg": "*Double Bar*"} |
| 78 | + if multiples == 3 then return {"won": bet * 11, "msg": "**Top Dollar**"} |
| 79 | + if multiples == 1 then return {"won": bet * 3, "msg": "Double!!"} |
| 80 | + return {"won": -bet, "msg": "You lost."} |
| 81 | +end function |
| 82 | + |
| 83 | +Machine.results = function |
| 84 | + result = Machine.winnings |
| 85 | + self.money += result.won |
| 86 | + print result.msg |
| 87 | + if result.won > 0 then |
| 88 | + print "You Won!!" |
| 89 | + end if |
| 90 | + print "Your standings are $" + self.money |
| 91 | + return |
| 92 | +end function |
| 93 | + |
| 94 | +Machine.playAgain = function |
| 95 | + ans = input("Again? ") + " " |
| 96 | + self.playing = (ans[0].lower == "y") |
| 97 | + if self.playing then return |
| 98 | + print |
| 99 | + if self.money < 0 then |
| 100 | + print "Pay up! Please leave your money on the terminal." |
| 101 | + else if self.money == 0 then |
| 102 | + print "Hey, you broke even." |
| 103 | + else |
| 104 | + print "Collect your winnings from the H&M cashier." |
| 105 | + end if |
| 106 | +end function |
| 107 | + |
| 108 | +clear |
| 109 | +print " " * 30 + "Slots" |
| 110 | +print " " * 15 + "Creative Computing Morristown, New Jersey" |
| 111 | +print;print;print |
| 112 | +print "You are in the H&M Casino, in front of one of our" |
| 113 | +print "one-arm bandits. Bet from $1 to $100." |
| 114 | +print "to pull the arm, punch the return key after making your bet." |
| 115 | + |
| 116 | +print |
| 117 | +while Machine.playing |
| 118 | + Machine.makeBet |
| 119 | + print |
| 120 | + Machine.pullHandle |
| 121 | + print |
| 122 | + Machine.results |
| 123 | + Machine.playAgain |
| 124 | + print |
| 125 | +end while |
0 commit comments