sxrgini

CC: Slots

Nov 3rd, 2024 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --------------------------------------------------
  2. --Please install this using the installer (pastebin run RkRVwZTW)
  3. --------------------------------------------------
  4. -- Constants
  5. local limit = 10     -- Minimum credits required to play
  6. local diamondW = 1   -- % chance to land diamond
  7. local dollarW = 2    -- % chance to land dollar
  8. local sevenW = 3     -- % chance to land seven
  9. local bellW = 4      -- % chance to land bell
  10. local orangeW = 5    -- % chance to land orange
  11.  
  12. -- Chances for symbols
  13. local orangech = 30  
  14. local diamondch = 5
  15. local bellch = 20
  16. local sevench = 15
  17. local dollarch = 10
  18.  
  19. -- Credit and bet variables
  20. local credits = 0
  21. local bet = limit
  22. local multi = 0
  23. local price = 0
  24. local playerName = ""
  25.  
  26. -- Load images
  27. local diamond = paintutils.loadImage("images/diamond.nfp")
  28. local bell = paintutils.loadImage("images/bell.nfp")
  29. local seven = paintutils.loadImage("images/7.nfp")
  30. local dollar = paintutils.loadImage("images/dollar.nfp")
  31. local orange = paintutils.loadImage("images/orange.nfp")
  32. local none = paintutils.loadImage("images/none.nfp")
  33.  
  34. -- Center text function
  35. function centerText(y, text)
  36.     local width = term.getSize()
  37.     term.setCursorPos(math.floor((width - #text) / 2), y)
  38.     term.write(text)
  39. end
  40.  
  41. -- Display insert card screen
  42. function insertCard()
  43.     while not fs.exists("disk/") do
  44.         paintutils.drawFilledBox(1, 1, 51, 19, colors.black)
  45.         centerText(8, "[Please Insert Casino Card]")
  46.         sleep(1)
  47.     end
  48.     paintutils.drawFilledBox(1, 1, 51, 19, colors.black)
  49. end
  50.  
  51. -- Load player's name from name.txt on the disk
  52. function loadPlayerName()
  53.     if fs.exists("disk/name.txt") then
  54.         local nameFile = fs.open("disk/name.txt", "r")
  55.         playerName = nameFile.readAll():gsub("\n", "")
  56.         nameFile.close()
  57.     else
  58.         centerText(8, "[Player name file not found!]")
  59.         sleep(2)
  60.         return false
  61.     end
  62.     return true
  63. end
  64.  
  65. -- Read credits from card
  66. function readCard()
  67.     if not fs.exists("disk/creds.lua") then
  68.         centerText(8, "[Casino Card not found!]")
  69.         sleep(2)
  70.         return false
  71.     end
  72.  
  73.     local Card = fs.open("disk/creds.lua", "r")
  74.     local data = Card.readAll()
  75.     Card.close()
  76.  
  77.     -- Extract credits
  78.     local credsStart = string.find(data, "11066011") + 8
  79.     local credsEnd = string.find(data, "11077011", credsStart)
  80.     credits = tonumber(string.sub(data, credsStart, credsEnd - 1))
  81.  
  82.     if credits < limit then
  83.         paintutils.drawFilledBox(1, 1, 51, 19, colors.red)
  84.         centerText(10, "[Insufficient Funds.]")
  85.         while fs.exists("disk/") do
  86.             sleep(0.5)
  87.         end
  88.         return false
  89.     end
  90.  
  91.     return true
  92. end
  93.  
  94. -- Write credits back to card
  95. function writeCard()
  96.     if not fs.exists("disk/creds.lua") then
  97.         centerText(8, "[Card not found!]")
  98.         sleep(2)
  99.         return false
  100.     end
  101.  
  102.     local card = fs.open("disk/creds.lua", "w")
  103.     local data = tostring(math.random(1, 163456)) .. "11066011" .. tostring(credits) .. "11077011" .. tostring(math.random(1, 163456))
  104.     card.write(data)
  105.     card.close()
  106.     disk.setLabel("bottom", playerName .. "'s Casino Card - " .. tostring(credits) .. " Credits")
  107. end
  108.  
  109. -- Insert bet function
  110. function insertBet()
  111.     local validBet = false
  112.     while not validBet do
  113.         paintutils.drawFilledBox(1, 1, 51, 19, colors.black)
  114.         centerText(8, "[Enter Bet Amount]")
  115.         centerText(10, "[Current Credits: " .. credits .. "]")
  116.         term.setCursorPos(15, 12)
  117.         term.write("Bet: ")
  118.         local input = read()
  119.         local betAmount = tonumber(input)
  120.         if betAmount and betAmount > 0 and betAmount <= credits then
  121.             bet = betAmount
  122.             validBet = true
  123.         else
  124.             centerText(14, "[Invalid bet. Try again.]")
  125.             sleep(2)
  126.         end
  127.     end
  128.     credits = credits - bet
  129.     writeCard()
  130. end
  131.  
  132. -- Constants for symbols
  133. local resultMultiplier = {0, 2, 1.75, 1.5, 1.25, 1}
  134. local resultPrice = {0, 1, 2, 3, 4, 5}              
  135.  
  136. -- Slot machine visual setup
  137. function slotMachine()
  138.     paintutils.drawFilledBox(1, 1, 51, 19, colors.green)
  139.     paintutils.drawFilledBox(5, 1, 46, 19, colors.lightGray)
  140.     paintutils.drawLine(5, 1, 5, 19, colors.white)
  141.     paintutils.drawLine(19, 1, 19, 19, colors.white)
  142.     paintutils.drawLine(33, 1, 33, 19, colors.white)
  143.     paintutils.drawLine(47, 1, 47, 19, colors.white)
  144.     paintutils.drawLine(2, 10, 5, 10, colors.gray)
  145.     paintutils.drawLine(47, 10, 50, 10, colors.gray)
  146. end
  147.  
  148. -- Draw random symbols on the slot machine
  149. function drawSymbols(column, row)
  150.     local images = {none, diamond, dollar, seven, bell, orange}
  151.     local symbolIndex = math.random(1, #images)
  152.     paintutils.drawImage(images[symbolIndex], 8 + (column - 1) * 14, row)
  153.     return symbolIndex
  154. end
  155.  
  156. -- Roll the slot machine
  157. function roll()
  158.     local results = {}
  159.     for i = 1, 3 do
  160.         local symbolIndex = drawSymbols(i, 8)
  161.         table.insert(results, symbolIndex)
  162.         sleep(0.8)
  163.     end
  164.     return results
  165. end
  166.  
  167. -- Display the final results
  168. function displayResults(results)
  169.     local totalWin = 0
  170.     for _, symbolIndex in ipairs(results) do
  171.         totalWin = totalWin + (bet * resultMultiplier[symbolIndex])
  172.     end
  173.    
  174.     if totalWin > 0 then
  175.         credits = credits + totalWin
  176.         centerText(15, "[You Win: " .. totalWin .. " Credits!]")
  177.     else
  178.         centerText(15, "[You Lose!]")
  179.     end
  180.     writeCard()
  181.     sleep(2)
  182. end
  183.  
  184. -- Lever pull screen
  185. function lever()
  186.     paintutils.drawFilledBox(1, 1, 51, 19, colors.black)
  187.     centerText(8, "[Pull lever to roll]")
  188.     while not redstone.getInput("right") do
  189.         sleep(0.1)
  190.     end
  191.     while redstone.getInput("right") do
  192.         sleep(0.1)
  193.     end
  194. end
  195.  
  196. -- Main game loop
  197. while true do
  198.     insertCard()
  199.     if readCard() then
  200.         if loadPlayerName() then
  201.             insertBet()
  202.             lever()
  203.             slotMachine()
  204.             local results = roll()
  205.             displayResults(results)
  206.         end
  207.     end
  208. end
Add Comment
Please, Sign In to add comment