Advertisement
JereTheJuggler

videoPoker.lua

Nov 26th, 2021
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.60 KB | None | 0 0
  1. --Config Variables
  2.  
  3. operatorName = " " --this will be used
  4.                    --to tell people who owns the
  5.                    --machine, so if a problem
  6.                    --occurs the user can get you.
  7.  
  8. payment = "diamond"
  9. costToPlay = 2
  10. volume = 1 --you can add a note block to any side
  11.            --of the computer and have sounds. You
  12.            --can even have the note block anywhere
  13.            --you want, connected with wired modems
  14.  
  15. moneyColor = colors.cyan --just a little aesthetic
  16.              --you can change so the text color
  17.              --the amount of money is displayed
  18.              --with matches the item set as payment
  19.  
  20. --for side variables, use either "top","left",
  21. --"right","back","front", or "bottom"
  22.  
  23. --for dir variables, use either "up","east",
  24. --"south","west","north", or "down"
  25.  
  26. bankSide = nil --this is for where the bank
  27.                --inventory (the one that takes
  28.                --and stores payment) is in
  29.                --relation to the computer
  30.  
  31. inputPushDir = nil --this is for what direction
  32.                    --the input inventory pushes
  33.                    --out items.
  34.  
  35. inputSide = nil --this is for where the input
  36.                 --inventory (the one players have
  37.                 --access to) is in relation to
  38.                 --the computer.
  39.                
  40. bankPushDir = nil --this is for what direction
  41.                   --the output inventory pushes
  42.                   --out items                
  43.  
  44. sounds = {            --contains the names of sound
  45. dealCard="dig.gravel",--clips to use. must be valid
  46. select="random.click",--names from minecraft's
  47. win="random.levelup", --sounds.json
  48. discard="dig.cloth",
  49. chaching="random.orb"}
  50. --
  51.  
  52.  
  53. --OK! this is where the actual code for the program
  54. --starts! You probably shouldn't mess with it...
  55. hands = {{name="Junk",payout=0},{name="High Pair",payout=1},{name="2 Pairs",payout=4},{name="3 of a Kind",payout=8},{name="Straight",payout=12},{name="Flush",payout=20},{name="Full House",payout=30},{name="4 of a Kind",payout=60},{name="Straight Flush",payout=150},{name="Royal Flush",payout=500}}
  56. suitDisplays = {{" () ","()()"," /\\ ",color=colors.black},{" /\\ ","(__)"," /\\ ",color=colors.black},{"/\\/\\","\\  /"," \\/ ",color=colors.red},{" /\\ ","<  >"," \\/ ",color=colors.red}}
  57. valueDisplays = {{",^,","i-i","i i"},{",-."," _/","l__"},{",-."," _/","._)"},{"i i","l_i","  i"},{",--","l_ ","._)"},{",-.","i-,","l_i"},{"--."," / ","/  "},{".-.",">-<","\\_/"},{",-,","l_i","._i"},{", ,-,","i i i","i l_i"},{"  ,","  i","l_i"},{",-,","i i","l_\\"},{"i i","i< ","i i"}}
  58. readyForNextRound = true
  59. gameInProgress = false
  60. winnings = 0
  61. if inputSide == nil or bankSide == nil or inputPushDir == nil or bankPushDir == nil then
  62.   term.clear()
  63.   term.setCursorPos(1,1)
  64.   term.write("Config variables have not been set")
  65.   term.setCursorPos(1,2)
  66.   term.write("Press any key to exit")
  67.   os.pullEvent("key")
  68.   term.clear()
  69.   term.setCursorPos(1,1)
  70.   return
  71. end
  72. width,height = term.getSize()
  73. infoPanel = window.create(term.current(),width-19,4,20,10)
  74. input = peripheral.wrap(inputSide)
  75. dealButton = window.create(term.current(),width-13,18,8,1)
  76. moneyLabel = window.create(term.current(),width-12,15,12,2)
  77. bank = peripheral.wrap(bankSide)
  78. deck = {}
  79. currentHand = {}
  80. --set up currentHand
  81. for c=1,5 do
  82.   table.insert(currentHand,c,{card=nil,keep=false,win=window.create(term.current(),1,1+4*(c-1),15,4)})
  83. end
  84. --construct deck
  85. for s=1,4 do
  86.   for v=1,13 do
  87.     table.insert(deck,1,{suit=s,value=v})
  88.   end
  89. end
  90. --renders all the 1 time only windows
  91. function initGui ()
  92.   term.setBackgroundColor(colors.black)
  93.   term.clear()
  94.   text = "Made by JereTheJuggler"
  95.   term.setTextColor(colors.white)
  96.   term.setCursorPos(17,height)
  97.   term.write(text)
  98.   --top payout chart
  99.   term.setCursorPos(width-19,1)
  100.   term.setTextColor(colors.yellow)
  101.   term.write(",------------------,")
  102.   term.setCursorPos(width-19,2)
  103.   term.write("|                  |")
  104.   term.setCursorPos(width-19,3)
  105.   term.write("|__________________|")
  106.   term.setCursorPos(width-18,2)
  107.   term.setTextColor(moneyColor)
  108.   term.write("   PAYOUT TABLE   ")
  109.   --surrounding money label
  110.   term.setTextColor(colors.yellow)
  111.   term.setCursorPos(width-19,14)
  112.   term.write("|------------------|")
  113.   term.setCursorPos(width-19,15)
  114.   term.write("|                  |")
  115.   term.setCursorPos(width-19,16)
  116.   term.write("|                  |")
  117.   term.setCursorPos(width-18,17)
  118.   term.write("------------------")
  119.   term.setCursorPos(width-18,16)
  120.   term.write("Money:")
  121.   --deal button
  122.   dealButton.setBackgroundColor(colors.red)
  123.   dealButton.clear()
  124.   dealButton.setCursorPos(3,1)
  125.   dealButton.setTextColor(colors.black)
  126.   dealButton.write("Deal")
  127. end
  128. --method that plays sounds
  129. function playSound (sound,freq)
  130.   note = peripheral.find("note_block")
  131.   if note == nil then
  132.     return
  133.   end
  134.   if freq == nil then
  135.     freq = .9
  136.   end
  137.   if volume == nil then
  138.     vol = 1
  139.   else
  140.     vol = volume
  141.   end
  142.   note.playSound(sound,freq,vol)
  143. end
  144. --method that draws the entire gui
  145. function drawGui ()
  146.   renderCurrentHand()
  147.   renderInfoPanel()
  148. end
  149. --renders the window with the specified index of
  150. --the current hand. if index is nil it renders all
  151. function renderCurrentHand (index)
  152.   if index == nil then
  153.     startIndex=1
  154.     endIndex=5
  155.   else
  156.     startIndex=index
  157.     endIndex=index
  158.   end
  159.   for c=startIndex,endIndex do
  160.     if currentHand[c].card==nil then
  161.       currentHand[c].win.setBackgroundColor(colors.black)
  162.       currentHand[c].win.clear()
  163.     else
  164.       if currentHand[c].keep then
  165.         currentHand[c].win.setBackgroundColor(colors.gray)
  166.         currentHand[c].win.clear()
  167.         currentHand[c].win.setBackgroundColor(colors.black)
  168.         currentHand[c].win.setCursorPos(1,4)
  169.         currentHand[c].win.write("                 ")
  170.         currentHand[c].win.setBackgroundColor(colors.gray)
  171.         currentHand[c].win.setCursorPos(currentHand[c].win.getSize()-3,1)
  172.         currentHand[c].win.setTextColor(colors.green)
  173.         currentHand[c].win.write("KEEP")
  174.       else
  175.         currentHand[c].win.setBackgroundColor(colors.white)
  176.         currentHand[c].win.clear()
  177.         currentHand[c].win.setBackgroundColor(colors.lightGray)
  178.         currentHand[c].win.setTextColor(colors.gray)
  179.         currentHand[c].win.setCursorPos(1,4)
  180.         currentHand[c].win.write("___________________")
  181.         currentHand[c].win.setBackgroundColor(colors.white)
  182.       end
  183.       currentHand[c].win.setTextColor(suitDisplays[currentHand[c].card.suit].color)
  184.       for s=1,3 do
  185.         currentHand[c].win.setCursorPos(1,s)
  186.         currentHand[c].win.write(valueDisplays[currentHand[c].card.value][s].." "..suitDisplays[currentHand[c].card.suit][s])
  187.       end
  188.     end
  189.   end
  190. end
  191. --renders the info panel showing hands and payouts
  192. --if index isn't nil it hilights the specified hand
  193. function renderInfoPanel (index)
  194.   panelWidth,panelHeight = infoPanel.getSize()
  195.   infoPanel.clear()
  196.   for i=1,table.getn(hands) do
  197.     infoPanel.setCursorPos(1,panelHeight-i+1)
  198.     infoPanel.setBackgroundColor(colors.black)
  199.     infoPanel.setTextColor(colors.yellow)
  200.     infoPanel.write("|              |   |")
  201.     infoPanel.setCursorPos(2,panelHeight-i+1)
  202.     infoPanel.setTextColor(colors.white)
  203.     if i==index then
  204.       infoPanel.setBackgroundColor(colors.lightBlue)
  205.       infoPanel.write("              ")
  206.       infoPanel.setCursorPos(17,panelHeight-i+1)
  207.       infoPanel.write("   ")
  208.       infoPanel.setCursorPos(2,panelHeight-i+1)
  209.     end
  210.     infoPanel.write(hands[i].name)
  211.     infoPanel.setCursorPos(20-string.len(tostring(hands[i].payout)),panelHeight-i+1)
  212.     infoPanel.write(tostring(hands[i].payout))
  213.   end
  214. end
  215. --deal first cards to the player
  216. usedCards = {}
  217. function dealFirstCards ()
  218.   usedCards = {}
  219.   for c=1,5 do
  220.     currentHand[c]["card"] = nil
  221.     renderCurrentHand(c)
  222.   end
  223.   playSound("note.harp",1.32)
  224.   input.condenseItems()
  225.   slot = 1
  226.   items = input.getAllStacks()
  227.   while slot <= input.getInventorySize() do
  228.     if items[slot] ~= nil then
  229.       if items[slot].basic().name == payment then
  230.         playSound("note.harp",1.8)
  231.         input.pushItem(inputPushDir,slot,costToPlay)
  232.         break
  233.       else
  234.         slot = slot + 1
  235.       end
  236.     else
  237.       slot = slot+1
  238.     end
  239.   end
  240.   for i=1,5 do
  241.     currentHand[i]["card"] = nil
  242.     currentHand[i]["keep"] = false
  243.   end
  244.   usedCards = {}
  245.   for c=1,5 do
  246.     pickACard(c)
  247.     sleep(.25)
  248.   end
  249. end
  250. function dealSecondCards ()
  251.   keeps = {}
  252.   for c=1,5 do
  253.     table.insert(keeps,c,currentHand[c].keep)
  254.   end
  255.   for c=1,5 do
  256.     if not keeps[c] then
  257.       currentHand[c]["card"] = nil
  258.       renderCurrentHand(c)
  259.       playSound(sounds.discard)
  260.       sleep(.25)
  261.     end
  262.   end
  263.   for c=1,5 do
  264.     if not keeps[c] then
  265.       pickACard(c)
  266.       sleep(.25)
  267.     end
  268.   end
  269.   for c=1,5 do
  270.     if keeps[c] then
  271.       currentHand[c]["keep"] = false
  272.       renderCurrentHand(c)
  273.     end
  274.   end
  275.   gameInProgress = false
  276. end
  277. --returns a card object that hasn't been used
  278. function pickACard (index)
  279.   repeat
  280.     continue = true
  281.     card=deck[math.floor(1+math.random()*52)]
  282.     if table.getn(usedCards) > 0 then
  283.       for i=1,table.getn(usedCards) do
  284.         if usedCards[i] == card.value..card.suit then
  285.           continue = false
  286.         end
  287.       end
  288.     end
  289.   until continue
  290.   table.insert(usedCards,card.value..card.suit)
  291.   currentHand[index]["card"]=card
  292.   playSound(sounds.dealCard,1.32)
  293.   renderCurrentHand(index)
  294. end
  295. --renders the money label
  296. function renderMoneyLabel ()
  297.   moneyLabel.clear()
  298.   if winnings ~= 0 then
  299.     moneyLabel.setCursorPos(moneyLabel.getSize()-string.len("+"..tostring(winnings))+1,1)
  300.     moneyLabel.setTextColor(colors.lime)
  301.     moneyLabel.write("+"..tostring(winnings))
  302.   end
  303.   moneyLabel.setCursorPos(moneyLabel.getSize()-string.len(tostring(money))+1,2)
  304.   moneyLabel.setTextColor(moneyColor)
  305.   moneyLabel.write(tostring(money))
  306. end
  307. winnings = 0
  308. gameInProgress = false
  309. readyForNextRound = true
  310. function playGame ()
  311.   gameInProgress = false
  312.   while true do
  313.     if winnings == 0 then
  314.       event,arg1,arg2,arg3,arg4 = os.pullEvent()
  315.       if event == "mouse_click" then
  316.         xPos = arg2
  317.         yPos = arg3
  318.         if isWithinWindow(dealButton) then
  319.           if not gameInProgress and readyForNextRound then
  320.             if money >= costToPlay and getAvailableMoney(bank) >= 500 then
  321.               renderInfoPanel()
  322.               playSound("note.harp",.9)
  323.               gameInProgress = true
  324.               dealFirstCards()
  325.             else
  326.               if getAvailableMoney(bank) < 500 then
  327.                 term.setTextColor(colors.red)
  328.                 term.setCursorPos(16,1)
  329.                 term.write("Bank is short on")
  330.                 term.setCursorPos(16,2)
  331.                 term.write("funds. Please")
  332.                 term.setCursorPos(16,3)
  333.                 term.write("contact operator")
  334.                 term.setCursorPos(16,4)
  335.                 term.write(operatorName)
  336.                 sleep(5)
  337.                 for n=1,4 do
  338.                   term.setCursorPos(16,n)
  339.                   term.write("                ")
  340.                 end  
  341.               end
  342.             end
  343.           elseif gameInProgress then
  344.             dealButton.setVisible(false)
  345.             term.setCursorPos(dealButton.getPosition())
  346.             term.write("         ")
  347.             dealSecondCards()
  348.             winnings = analyzeCards()
  349.             if winnings == 0 then
  350.               dealButton.setVisible(true)
  351.               dealButton.redraw()
  352.             end
  353.             gameInProgress = false
  354.           end
  355.         elseif gameInProgress then
  356.           for c=1,5 do
  357.             if isWithinWindow(currentHand[c].win) then
  358.               if currentHand[c].keep then
  359.                 currentHand[c]["keep"] = false
  360.                 playSound(sounds.select)
  361.               else
  362.                 playSound(sounds.select)
  363.                 currentHand[c]["keep"] = true
  364.               end
  365.               renderCurrentHand(c)
  366.               break
  367.             end
  368.           end        
  369.         end
  370.       elseif event == "key" then
  371.         if arg1 == keys.backspace then
  372.           return
  373.         elseif arg1 == keys.leftShift then
  374.           --winnings = 20
  375.         end
  376.       end
  377.     else
  378.       sleep(.5)
  379.     end
  380.   end
  381. end
  382.  
  383. function analyzeCards()
  384.   suits = {}
  385.   values = {}
  386.   for c=1,5 do
  387.     table.insert(suits,c,currentHand[c].card.suit)
  388.     table.insert(values,c,currentHand[c].card.value)
  389.   end
  390.   values = sort(values)
  391.   flush = false
  392.   if suits[1] == suits[2] and suits[2] == suits[3] and suits[3] == suits[4] and suits[4] == suits[5] then
  393.     flush = true
  394.   end
  395.   if (values[5] == values[4]+1 and values[4] == values[3]+1 and values[3] == values[2]+1 and values[2] == values[1]+1) or
  396.      (values[1] == 1 and values[2] == 10 and values[3] == 11 and values[4] == 12 and values[5] == 13) then
  397.     if flush then
  398.       if values[1] == 1 and values[5] == 13 then
  399.         return getWinningsFor("Royal Flush")
  400.       else
  401.         return getWinningsFor("Straight Flush")
  402.       end
  403.     else
  404.       return getWinningsFor("Straight")
  405.     end
  406.   end
  407.   if (values[1] == values[2] and values[2] == values[3] and values[3] == values[4]) or
  408.      (values[2] == values[3] and values[3] == values[4] and values[4] == values[5]) then
  409.     return getWinningsFor("4 of a Kind")
  410.   end
  411.   if (values[1] == values[2] and values[3] == values[4] and values[4] == values[5]) or
  412.      (values[1] == values[2] and values[2] == values[3] and values[4] == values[5]) then
  413.     return getWinningsFor("Full House")
  414.   end
  415.   if flush then
  416.     return getWinningsFor("Flush")
  417.   end
  418.   if values[1] == values[3] or values[2] == values[4] or values[3] == values[5] then
  419.     return getWinningsFor("3 of a Kind")
  420.   end
  421.   if (values[1] == values[2] and values[3] == values[4]) or
  422.      (values[1] == values[2] and values[4] == values[5]) or
  423.      (values[2] == values[3] and values[4] == values[5]) then
  424.     return getWinningsFor("2 Pairs")
  425.   end
  426.   for i=1,4 do
  427.     if values[i] == values[i+1] and (values[i] == 1 or values[i] >= 11) then
  428.       return getWinningsFor("High Pair")
  429.     end
  430.   end
  431.   return getWinningsFor("Junk")
  432. end
  433. function sort (values)
  434.   sortedValues = {}
  435.   table.insert(sortedValues,1,values[1])
  436.   for c=2,5 do
  437.     alreadyPlaced = false
  438.     for d=1,table.getn(sortedValues)+1 do
  439.       if alreadyPlaced == false then
  440.         if d==table.getn(sortedValues)+1 then
  441.           table.insert(sortedValues,d,values[c])
  442.         elseif sortedValues[d] >= values[c] then
  443.           table.insert(sortedValues,d,values[c])
  444.           alreadyPlaced = true
  445.         end
  446.       end
  447.     end
  448.   end  
  449.   return sortedValues
  450. end
  451. function getWinningsFor (hand)
  452.   for c=1,table.getn(hands) do
  453.     if hands[c].name == hand then
  454.       renderInfoPanel(c)
  455.       return hands[c].payout
  456.     end
  457.   end
  458. end
  459. function moneyListener ()
  460.   prevMoney = -1
  461.   while true do
  462.     money = getAvailableMoney(input)
  463.     if prevMoney ~= money or winnings ~= 0 then
  464.       if winnings ~= 0 then
  465.         readyForNextRound = false
  466.         renderMoneyLabel()
  467.         playSound(sounds.win,1)
  468.         sleep(.65)
  469.         repeat
  470.           bank.condenseItems()
  471.           bank.pushItem(bankPushDir,1,1)
  472.           winnings = winnings - 1
  473.           money = money + 1
  474.           playSound(sounds.chaching,1.8)
  475.           renderMoneyLabel()
  476.         until winnings == 0
  477.         loopCounter=0
  478.         repeat
  479.           actualMoney = getAvailableMoney(input)
  480.           loopCounter = loopCounter + 1
  481.         until actualMoney == money or loopCounter >= 100
  482.         dealButton.setVisible(true)
  483.         dealButton.redraw()
  484.         readyForNextRound = true
  485.       end
  486.       renderMoneyLabel()
  487.       prevMoney = money
  488.     else
  489.       prevMoney = money
  490.       sleep(.125)
  491.     end
  492.   end
  493. end
  494. --returns whether or not a click was on a window
  495. function isWithinWindow (win)
  496.   xMin,yMin=win.getPosition()
  497.   winWidth,winHeight = win.getSize()
  498.   xMax = xMin+winWidth-1
  499.   yMax = yMin+winHeight-1
  500.   return (xPos <= xMax and xPos >= xMin and yPos <= yMax and yPos >= yMin)
  501. end
  502. --returns how much money is in the source inv.
  503. function getAvailableMoney (source)
  504.   source.condenseItems()
  505.   items = source.getAllStacks()
  506.   if table.getn(items) == 0 then
  507.     return 0
  508.   end
  509.   total = 0
  510.   for i=1,source.getInventorySize() do
  511.     if items[i] ~= nil then
  512.       if items[i].basic().name == payment then
  513.         total = total + items[i].basic().qty
  514.       end
  515.     else
  516.       break
  517.     end
  518.   end
  519.   return total
  520. end
  521.  
  522. initGui()
  523. drawGui()
  524. parallel.waitForAny(playGame,moneyListener)
  525. term.setBackgroundColor(colors.black)
  526. term.setTextColor(colors.white)
  527. term.clear()
  528. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement