mercwear

Slot machine

May 12th, 2015
2,974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. --Should this program create the image files needed for it to run. Change to false after first run
  2. local create_images = "true"
  3.  
  4. --Where to send the terminal output
  5. term.redirect(peripheral.wrap("right"))
  6.  
  7. --Set the background color of the monitor
  8. term.setBackgroundColor(colors.black)
  9.  
  10. --Monitor variable
  11. local mon = peripheral.wrap("right")
  12.  
  13. --Set the text size of the monitor
  14. mon.setTextScale(0.5)
  15.  
  16. --Clear the monitor
  17. mon.clear()
  18.  
  19. --Set the max bet amount
  20. local max_bet = 5
  21.  
  22. --Set the minimum bet amount
  23. local min_bet = 2
  24.  
  25. --Currency this machine will accept
  26. --Display name (what the users will see)
  27. local accepted_currency = "Diamond"
  28.  
  29. --Minecraft name. Note: Most items have 2 names, one can be edited (display_name) with the anvil and the other cannot (id). It's
  30. -- important to
  31. -- use the name that cannot be edited here to prevent cheating. Look at the item in a computer craft terminal to
  32. -- determine what item name to use.
  33. local real_currency_name = "minecraft:diamond"
  34.  
  35. --Image options for the reel (these are files on the computer that controls the slot)
  36. options = {"pig","sheep","creeper","steve","cow"}
  37.  
  38. --Chest that the user will deposit currency into so they can play
  39. deposit_chest = peripheral.wrap("container_chest_0")
  40.  
  41. --Chest where the user money goes once they deposit it
  42. --collections_chest = peripheral.wrap("container_chest_7")
  43.  
  44. --Where to push the users deposit for safe keeping
  45. collections_chest_push_direction = "north"
  46.  
  47. --Chest where winners can collect their winnings and it's push direction
  48. payout_chest = peripheral.wrap("container_chest_1")
  49. payout_chest_push_direction = "west"
  50.  
  51. --All variables that typically will not be changed by the user are defined here
  52. local currency = nil
  53.  
  54. --Variables for detecting a button press. Not currently used
  55. local mouseWidth = 0
  56. local mouseHeight = 0
  57. local w,h=mon.getSize()
  58. local curx, cury
  59. local amnt_due
  60. local winner_payout
  61. local bet_amount
  62. local bet_type
  63. local winning_amount
  64. local left_to_pay
  65. local paid
  66. local slot_one_contents
  67. local winner
  68. local n_one
  69. local n_two
  70. local n_three
  71. local image
  72. local slot_image
  73. local money_image
  74. local slot_one_check_contents
  75. local bet_status
  76. local cheater
  77.  
  78.  
  79. --Function to center text on screen. The Y position (1,Y) of the cursor should be set BEFORE this function is called
  80. local function center_printing (text)
  81. curx, cury = term.getCursorPos()
  82. term.setCursorPos((w-#text)/2,cury)
  83. term.write(text) --write the text
  84. term.setCursorPos(curx,cury+1)
  85. term.setCursorPos(1,math.floor(h/2))
  86.  
  87. end
  88.  
  89. local function outOfCurrency(amnt_due)
  90. while true do
  91. --term.clear()
  92. term.setCursorPos(1,16)
  93. center_printing ("This machine is out of "..accepted_currency..".")
  94. term.setCursorPos(1,17)
  95. center_printing ("Please contact Mercwear.")
  96. term.setCursorPos(1,19)
  97. center_printing ("Amount still due to player:")
  98. term.setCursorPos(1,20)
  99. center_printing (amnt_due.." "..accepted_currency..".")
  100. sleep(60)
  101. end
  102.  
  103. end
  104.  
  105. local function paywinner(winning_amount)
  106. term.setCursorPos(1,13)
  107. winner_payout = winning_amount * bet_amount
  108. center_printing ("You won "..winner_payout.." "..accepted_currency.."!!")
  109. left_to_pay = winner_payout
  110. paid = 0
  111. while left_to_pay > 0 do
  112. --Condense the chest to fill up slot one
  113. payout_chest.condenseItems()
  114.  
  115. --Get the amount if items in slot one of the chest
  116. slot_one_contents = payout_chest.getStackInSlot(1)
  117.  
  118. --Make sure the machine is not out of money and if so, put an error up for the user
  119. if slot_one_contents ~= nil then
  120. slot_one_cnt = slot_one_contents.qty
  121. else
  122. outOfCurrency(left_to_pay)
  123. end
  124.  
  125. --Push the winning amount to the player (or if it's more than 64, all currency in slot one of the payout chest)
  126. paid = payout_chest.pushItem(payout_chest_push_direction,1,left_to_pay)
  127.  
  128. --Update the left_to_pay variable
  129. left_to_pay = left_to_pay - paid
  130. end
  131. sleep(5)
  132. end
  133.  
  134. local function outcome()
  135. --Check for winner and print results
  136. if (n_one == n_two) and (n_one == n_three) then
  137. winner = "1"
  138. elseif n_one == n_two then
  139. winner = "2"
  140. else
  141. winner = "0"
  142. end
  143.  
  144. --Output the results
  145. term.setCursorPos(1,14)
  146. if winner == "1" then
  147. center_printing ("15 to 1 winner!")
  148. paywinner(15)
  149. elseif winner == "2" then
  150. center_printing ("5 to 1 winner!")
  151. paywinner(5)
  152. else
  153. center_printing ("Not a winner.")
  154. term.setCursorPos(1,15)
  155. center_printing ("Sorry. Better luck next time.")
  156. sleep(5)
  157. end
  158. end
  159.  
  160. local function roll()
  161. mon.clear()
  162. options = {"pig","sheep","creeper","steve","cow"}
  163. i = 1
  164. while i < 7 do
  165. image = math.random(1,#options)
  166. slot_image = paintutils.loadImage((options[image]))
  167. paintutils.drawImage(slot_image, 2,1)
  168. n_one = image
  169.  
  170. image = math.random(1,#options)
  171. slot_image = paintutils.loadImage((options[image]))
  172. paintutils.drawImage(slot_image, 22,1)
  173. n_two = image
  174.  
  175. image = math.random(1,#options)
  176. slot_image = paintutils.loadImage((options[image]))
  177. paintutils.drawImage(slot_image, 42,1)
  178. n_three = image
  179.  
  180. sleep(0.5)
  181. term.setBackgroundColor(colors.black)
  182. i = i + 1
  183. end
  184. outcome()
  185. end
  186.  
  187. local function paid()
  188. term.clear()
  189. term.setCursorPos(1,2)
  190. center_printing ("You are betting "..bet_amount.." "..accepted_currency..". Good luck!")
  191. money_image = paintutils.loadImage("money")
  192. paintutils.drawImage(money_image, 21,8)
  193. --paintutils.drawBox(28, 19, 27, 23, colors.lime)
  194. term.setCursorPos(1,4)
  195. --mon.setBackgroundColour((colours.lime))
  196. center_printing (" Right click the screen to spin ")
  197. mon.setBackgroundColour((colours.black))
  198. event,p1,p2,p3 = os.pullEvent()
  199. if event=="monitor_touch" then
  200. mouseWidth = p2 -- sets mouseWidth
  201. mouseHeight = p3 -- and mouseHeight
  202. term.setCursorPos(1,1)
  203. checkClickPosition()
  204. end
  205.  
  206. end
  207.  
  208.  
  209. function checkClickPosition()
  210. --Used for placing a button. I am not using a button, the user can just click anywhere on the screen to spin
  211. --if mouseWidth > 25 and mouseWidth < 30 and mouseHeight == 21 then
  212. if mouseWidth > 1 and mouseWidth < 3000 and mouseHeight > 0 then
  213. term.setCursorPos(1,2)
  214. roll()
  215. else
  216. paid()
  217. end
  218. end
  219.  
  220. local function currencyCheck()
  221. --Condense the chest to fill up slot one
  222. payout_chest.condenseItems()
  223.  
  224. --Get the amount if items in slot one of the chest
  225. slot_one_check_contents = payout_chest.getStackInSlot(1)
  226.  
  227. --Make sure the machine is not out of money and if so, put an error up for the user
  228. if slot_one_check_contents == nil then
  229. outOfCurrency(0)
  230. end
  231. end
  232.  
  233. while true do
  234. --Determine if the image files for the reels need to be created
  235. if create_images == "true" then
  236. --Create images for slot machine
  237. local image_files = {cow = "ccccccccccccccc\nccccccccccccccc\nccccccccccccccc\n0000ccccccc0000\nff00ccccccc00ff\nccccccccccccccc\ncccc0000000cccc\ncc00ff888ff00cc\ncc00888888800cc\ncc00888888800cc", creeper = "555555555555555\n55dffd555dffd55\n55ffff555ffff55\n55ffff555ffff55\n555555fff555555\n5555fffffff5555\n5555fffffff5555\n5555fffffff5555\n5555ff55dff5555\n555555555555555", pig = "222222222222222\n222222222222222\n222222222222222\n222222222222222\nff00222222200ff\n222266666662222\n2222ff666ff2222\n222266666662222\n222222222222222\n222222222222222"
  238. , sheep = "000000000000000\n000000000000000\n00ccccccccccc00\n00ff00ccc00ff00\n00ccccccccccc00\n0000cc666cc0000\n0000cc666cc0000\n000000000000000\n000000000000000\n000000000000000", steve = "ccccccccccccccc\nccccccccccccccc\ncc11111111111cc\ncc11111111111cc\n111111111111111\n1100bb111bb0011\n111111777111111\n1111cc666cc1111\n1111ccccccc1111\n1111ccccccc1111", money = "ffffffffffffff\nfff5fff5fff5fff\nff555f555f555ff\nff5fff5fff5ffff\nff555f555f555ff\nffff5fff5fff5ff\nff555f555f555ff\nfff5fff5fff5fff\nfffffffffffffff\nfffffffffffffff"}
  239. for k,v in pairs (image_files) do
  240. -- Create a new file on the computer craft computer to save the contents of the remote file to
  241. local fHandle = fs.open(k, "w")
  242.  
  243. -- Write the contents of the remote file to the new file we just created
  244. fHandle.writeLine(v)
  245.  
  246. -- Close the new file, saving the contents of the remote file to it
  247. fHandle.close()
  248. end
  249. end
  250.  
  251. --Make sure that there is currency available to payout when a player wins
  252. currencyCheck()
  253.  
  254. term.setCursorPos(1,16)
  255. center_printing ("Please deposit "..accepted_currency.."(s) to play.")
  256.  
  257. term.setCursorPos(1,18)
  258. center_printing ("Payout Information:")
  259.  
  260. term.setCursorPos(1,19)
  261. center_printing ("Match any of the first 2 images and get 1 to 5 payout")
  262.  
  263. term.setCursorPos(1,20)
  264. center_printing ("Match all 3 images and get 1 to 15 payout")
  265.  
  266. term.setCursorPos(1,24)
  267. center_printing ("Min bet is "..min_bet.." "..accepted_currency..". Max bet is "..max_bet.." "..accepted_currency)
  268.  
  269.  
  270. --Have a constant spinning of the reel to draw players in
  271. image = math.random(1,#options)
  272. slot_image = paintutils.loadImage((options[image]))
  273. paintutils.drawImage(slot_image, 2,1)
  274.  
  275. image = math.random(1,#options)
  276. slot_image = paintutils.loadImage((options[image]))
  277. paintutils.drawImage(slot_image, 22,1)
  278.  
  279. image = math.random(1,#options)
  280. slot_image = paintutils.loadImage((options[image]))
  281. paintutils.drawImage(slot_image, 42,1)
  282.  
  283. sleep(2)
  284. mon.setBackgroundColour((colours.black))
  285.  
  286. --Check for deposit
  287. deposit_chest.condenseItems()
  288. currency = deposit_chest.getStackInSlot(1)
  289. if currency ~= nil then
  290. --They paid something!
  291. bet_type = currency.id
  292. bet_amount = currency.qty
  293. --The currency is correct now make sure they are betting within the min / max
  294. --Check if the bet amount is high / low enough
  295. if bet_amount > max_bet then
  296. bet_status = "high"
  297. elseif bet_amount < min_bet then
  298. bet_status = "low"
  299. end
  300.  
  301. --Make sure the currency is something that we accept and they have bet within the min and max settings
  302. if (bet_type == real_currency_name) and (bet_status == nil) then
  303. --Push the item to the collections chest
  304. deposit_chest.pushItem(collections_chest_push_direction,1,64)
  305. paid()
  306. else
  307. term.setCursorPos(1,22)
  308.  
  309. --Set the background of the error message to red
  310. term.setBackgroundColor(colors.red)
  311.  
  312. --Determine what error to display to the user
  313.  
  314. --Cheater! Someone tried renaming an item in an anvil to match the currency used on this slot
  315. if (currency.display_name == accepted_currency) and (currency.id ~= real_currency_name) then
  316. center_printing("CHEATER DETECTED")
  317. sleep(3)
  318. --Wrong currency error
  319. elseif bet_type ~= real_currency_name then
  320. center_printing("Sorry. We do not accept "..currency.display_name.." here.")
  321. --Bet too low error
  322. elseif bet_status == "low" then
  323. center_printing("You did not bet enough "..accepted_currency..".".." The minimum bet is ".. min_bet..".")
  324. --Bet too high error
  325. elseif bet_status == "high" then
  326. center_printing("You bet too much "..accepted_currency..".".." The maximum bet is ".. max_bet..".")
  327. end
  328. term.setBackgroundColor(colors.black)
  329. currency = nil
  330. bet_status = nil
  331. sleep(5)
  332. bet_type = nil
  333. bet_amount = nil
  334. term.clear()
  335. end
  336. end
  337. end
Advertisement
Add Comment
Please, Sign In to add comment