Advertisement
NeonJ

Slot Machine v2

Nov 12th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.38 KB | None | 0 0
  1. credits = 0
  2. creditswin = 0
  3. prizeA = 1      --  X|X|X
  4. prizeBBB = 10   --  O|O|O
  5. prizeBB = 2     --  O|O|?
  6. prizeC = 22     --  $|$|$
  7. prizeD = 64     --  7|7|7
  8. prizeE = 192    --  J|J|J
  9. detectside = "back"         -- This is which side each machine connects to.
  10. monside = "left"            -- Back, Left, Top and Right
  11. dispenserside = "top"       -- Relative to the sides of a computer, obviously.
  12. stopperside = "right"       -- DetectSide is based on bundled cables. Yellow = insert, Orange = Payout, Red = Play.
  13.  
  14. mon = peripheral.wrap(monside)
  15. redstone.setOutput(stopperside,true)    -- Connect the monitor, clear the screen, go go go!
  16. shell.run("clear",1)
  17.  
  18. mon.setCursorPos(1,1)
  19. mon.write("  000  ")
  20. mon.setCursorPos(1,2)       -- Display things onto the screen.
  21. mon.write("*******")
  22. mon.setCursorPos(1,3)       -- |  000  |
  23. mon.write("INSERT ")        -- |*******|
  24. mon.setCursorPos(1,4)       -- |INSERT |
  25. mon.write(">X|X|X<")        -- |>X|X|X<|
  26. mon.setCursorPos(1,5)       -- | CREDIT|
  27. mon.write(" CREDIT")
  28.  
  29. function winner()
  30.     mon.setCursorPos(1,3)
  31.     mon.write("  YOU  ")
  32.     mon.setCursorPos(1,5)
  33.     mon.write("  WIN  ")        -- Obviously.
  34.     sleep(2)
  35.     if creditswin == prizeE then    -- Did they get the jackpot?
  36.         mon.setCursorPos(1,3)
  37.         mon.write("JACKPOT")        -- Tell them.
  38.     end
  39.     while creditswin > 0 do     -- Slowly increment the credits, as the player has won.
  40.         credits = credits + 1
  41.         creditswin = creditswin - 1
  42.  
  43.         str = string.format("%03d", credits)
  44.         mon.setCursorPos(1,1)   -- Increment the credits counter, tell them they're winning!
  45.         mon.write("  "..str.."  ")
  46.  
  47.         sleep(0.2)      -- Little delay so it doesn't all go at once.
  48.     end
  49.     sleep(2)
  50.     if credits > 256 then payout() end      -- Limit 256 credits, so that players cannot drain the entire dispenser in one go.
  51.     mon.setCursorPos(1,3)
  52.     mon.write("PRESS  ")
  53.     mon.setCursorPos(1,5)                   -- I know the player has a credit now, so he can just press the button!
  54.     mon.write(" BUTTON")
  55.     redstone.setOutput(stopperside, true)
  56. end
  57.  
  58. function roll()
  59.     credits = credits - 1           -- It costs 1 credit to play the slots, you know!
  60.     rollsdone = 0
  61.    
  62.     str = string.format("%03d", credits)
  63.     mon.setCursorPos(1,1)
  64.     mon.write("  "..str.."  ")      -- Show the user that the computer took away a credit.
  65.    
  66.     result4 = math.random(1,100)    -- Start off with some numbers so that some reels aren't just blank.
  67.     result4 = result4 - ".0"
  68.     result5 = math.random(1,100)
  69.     result5 = result5 - ".0"
  70.     result6 = math.random(1,100)
  71.     result6 = result6 - ".0"
  72.     result7 = math.random(1,100)
  73.     result7 = result7 - ".0"
  74.     result8 = math.random(1,100)
  75.     result8 = result8 - ".0"
  76.     result9 = math.random(1,100)
  77.     result9 = result9 - ".0"        -- These are the very first numbers, so yay.
  78.    
  79.     if result4 < 31 then result4 = "X"          -- Turn them into results.
  80.     elseif result4 < 56 then result4 = "O"      -- This saves unneeded code later, saving CPU power.
  81.     elseif result4 < 76 then result4 = "$"
  82.     elseif result4 < 91 then result4 = "7"
  83.     else result4 = "J" end
  84.     if result5 < 31 then result5 = "X"
  85.     elseif result5 < 56 then result5 = "O"
  86.     elseif result5 < 76 then result5 = "$"
  87.     elseif result5 < 91 then result5 = "7"
  88.     else result5 = "J" end
  89.     if result6 < 31 then result6 = "X"
  90.     elseif result6 < 56 then result6 = "O"
  91.     elseif result6 < 76 then result6 = "$"
  92.     elseif result6 < 91 then result6 = "7"
  93.     else result6 = "J" end
  94.     if result7 < 31 then result7 = "X"
  95.     elseif result7 < 56 then result7 = "O"
  96.     elseif result7 < 76 then result7 = "$"
  97.     elseif result7 < 91 then result7 = "7"
  98.     else result7 = "J" end
  99.     if result8 < 31 then result8 = "X"
  100.     elseif result8 < 56 then result8 = "O"
  101.     elseif result8 < 76 then result8 = "$"
  102.     elseif result8 < 91 then result8 = "7"
  103.     else result8 = "J" end
  104.     if result9 < 31 then result9 = "X"
  105.     elseif result9 < 56 then result9 = "O"
  106.     elseif result9 < 76 then result9 = "$"
  107.     elseif result9 < 91 then result9 = "7"
  108.     else result9 = "J" end
  109.    
  110.     while rollsdone < 40 do
  111.    
  112.         result1 = result4           -- They're rolling now! Go, go, go!
  113.         result2 = result5
  114.         result3 = result6
  115.         result4 = result7
  116.         result5 = result8
  117.         result6 = result9
  118.         result7 = math.random(1,100)
  119.         result7 = result7 - ".0"
  120.         result8 = math.random(1,100)
  121.         result8 = result8 - ".0"
  122.         result9 = math.random(1,100)
  123.         result9 = result9 - ".0"
  124.        
  125.         if result7 < 31 then result7 = "X"          -- Turn the last numbers into proper results.
  126.         elseif result7 < 56 then result7 = "O"      -- Being derived from these, the first 6 are already proper results.
  127.         elseif result7 < 76 then result7 = "$"
  128.         elseif result7 < 91 then result7 = "7"
  129.         else result7 = "J" end
  130.         if result8 < 31 then result8 = "X"
  131.         elseif result8 < 56 then result8 = "O"
  132.         elseif result8 < 76 then result8 = "$"
  133.         elseif result8 < 91 then result8 = "7"
  134.         else result8 = "J" end
  135.         if result9 < 31 then result9 = "X"
  136.         elseif result9 < 56 then result9 = "O"
  137.         elseif result9 < 76 then result9 = "$"
  138.         elseif result9 < 91 then result9 = "7"
  139.         else result9 = "J" end                      -- End proper result transformification.
  140.        
  141.         mon.setCursorPos(1,3)                                           -- |  001  | <- Basically, it looks like this.
  142.         mon.write(" "..result1.."|"..result2.."|"..result3.." ")                  -- |*******|
  143.         mon.setCursorPos(1,4)                                           -- | O|X|$ |
  144.         mon.write(">"..result4.."|"..result5.."|"..result6.."<")        -- |>J|X|$<|
  145.         mon.setCursorPos(1,5)                                           -- | $|O|7 |
  146.         mon.write(" "..result7.."|"..result8.."|"..result9.." ")
  147.        
  148.         rollsdone = rollsdone + 1
  149.         sleep(rollsdone / 150)
  150.     end
  151.        
  152.     if result4 == result5 and result5 == result6 then
  153.         print("Detected win in middle row!")
  154.         if result4 == "O" then creditswin = prizeBBB end        -- Basically, check what the player won by looking at the first.
  155.         if result4 == "X" then creditswin = prizeA end          -- We know they're all matching, no need to check them all.
  156.         if result4 == "$" then creditswin = prizeC end
  157.         if result4 == "7" then creditswin = prizeD end
  158.         if result4 == "J" then creditswin = prizeE end
  159.         winner()
  160.     elseif result4 == "O" and result5 == "O" then
  161.         creditswin = prizeBB                                    -- Well, this is obviously the 2 cherry prize, isn't it.
  162.         print("Detected win - cherry win!")                     -- We know exactly what prize they're getting.
  163.         winner()
  164.     else
  165.         print("There was No Match")                             -- Poor loser.
  166.         sleep(1)
  167.         mon.setCursorPos(1,3)
  168.         mon.write("  TRY  ")
  169.         mon.setCursorPos(1,5)
  170.         mon.write(" AGAIN ")
  171.         sleep(2)
  172.         if credits > 0 then
  173.             mon.setCursorPos(1,3)
  174.             mon.write("PRESS  ")
  175.             mon.setCursorPos(1,5)
  176.             mon.write(" BUTTON")
  177.         else
  178.             mon.setCursorPos(1,3)
  179.             mon.write("INSERT ")
  180.             mon.setCursorPos(1,5)
  181.             mon.write(" CREDIT")
  182.         end
  183.     end
  184.     redstone.setOutput(stopperside, true)
  185. end
  186.  
  187. function payout()                                   -- Isn't this exactly the same, no matter how I do it?
  188.     sleep(2)
  189.     mon.setCursorPos(1,3)
  190.     mon.write("  PAY  ")
  191.     mon.setCursorPos(1,5)
  192.     mon.write("  OUT  ")
  193.     while credits > 0 do
  194.         redstone.setOutput(dispenserside, true)
  195.         sleep(0.2)
  196.         redstone.setOutput(dispenserside, false)
  197.         credits = credits - 1
  198.  
  199.         str = string.format("%03d", credits)            -- Always inform the user what you're doing. Makes it easier to debug AND play.
  200.         mon.setCursorPos(1,1)
  201.         mon.write("  "..str.."  ")
  202.     end
  203.  
  204.     mon.setCursorPos(1,3)
  205.     mon.write("INSERT ")
  206.     mon.setCursorPos(1,5)                               -- Well, there are no credits left now, are there. It dispensed them all.
  207.     mon.write(" CREDIT")
  208.     redstone.setOutput(stopperside, true)
  209. end
  210.  
  211. redstone.setOutput(stopperside, true)       -- Failsafe.
  212.  
  213. rRunning = true     -- This never changes, it's just to keep the infinite loop going.
  214.  
  215. while rRunning do
  216.     sleep(0.05)     -- Zzzz...
  217.     local rEvent, param = os.pullEvent()
  218.     redstone.setOutput(stopperside, true)
  219.     if rEvent == "redstone" then            -- Redstone detected! Activate the code!
  220.         if redstone.getBundledInput(detectside) then
  221.             if redstone.testBundledInput(detectside, colors.yellow) == true then    -- Yellow = Item Detector. It detects items.
  222.                 credits = credits + 1                                               -- +1 Credit.
  223.  
  224.                 str = string.format("%03d", credits)
  225.                 mon.setCursorPos(1,1)
  226.                 mon.write("  "..str.."  ")      -- Show the user that the computer gave him a credit.
  227.  
  228.                 mon.setCursorPos(1,3)           -- Well, there's now some cash, so play the machine!
  229.                 mon.write("PRESS  ")
  230.                 mon.setCursorPos(1,5)        
  231.                 mon.write(" BUTTON")
  232.                 if credits > 256 then payout() end      -- Hardcoded limit so we don't drain the dispenser too much.
  233.             elseif redstone.testBundledInput(detectside, colors.red) == true then       -- Red = PLAY button!
  234.                 redstone.setOutput(stopperside, false)
  235.                 if credits > 0 then                     -- Well, you can't play if there's no money in.
  236.                     roll()
  237.                 else
  238.                     mon.setCursorPos(1,3)               -- Tell the user he's pretty much broke.
  239.                     mon.write("1 COIN ")
  240.                     mon.setCursorPos(1,5)
  241.                     mon.write(" NEEDED")
  242.                     sleep(2)
  243.  
  244.                     mon.setCursorPos(1,3)               -- And tell him to come back with more cash, cheapskate!
  245.                     mon.write("INSERT ")
  246.                     mon.setCursorPos(1,5)
  247.                     mon.write(" CREDIT")
  248.                     redstone.setOutput(stopperside, true)
  249.                 end
  250.             elseif redstone.testBundledInput(detectside, colors.orange) == true then    -- Orange = PAYOUT button!
  251.                 redstone.setOutput(stopperside, false)
  252.                 if credits > 0 then                     -- Can't exactly pay out money if there's nothing to pay out.
  253.                     payout()
  254.                 else
  255.                     mon.setCursorPos(1,3)               -- Tell the user he hasn't won anything.
  256.                     mon.write("NONE   ")
  257.                     mon.setCursorPos(1,5)
  258.                     mon.write("   LEFT")
  259.                     sleep(2)
  260.  
  261.                     mon.setCursorPos(1,3)               -- Then tell him to come back with more cash!
  262.                     mon.write("INSERT ")
  263.                     mon.setCursorPos(1,5)
  264.                     mon.write(" CREDIT")
  265.                     redstone.setOutput(stopperside, true)
  266.                 end
  267.             end
  268.         end
  269.     end
  270. end
  271.  
  272. -- Now, to Beta Test!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement