Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Config Variables
- operatorName = " " --this will be used
- --to tell people who owns the
- --machine, so if a problem
- --occurs the user can get you.
- payment = "diamond"
- costToPlay = 2
- volume = 1 --you can add a note block to any side
- --of the computer and have sounds. You
- --can even have the note block anywhere
- --you want, connected with wired modems
- moneyColor = colors.cyan --just a little aesthetic
- --you can change so the text color
- --the amount of money is displayed
- --with matches the item set as payment
- --for side variables, use either "top","left",
- --"right","back","front", or "bottom"
- --for dir variables, use either "up","east",
- --"south","west","north", or "down"
- bankSide = nil --this is for where the bank
- --inventory (the one that takes
- --and stores payment) is in
- --relation to the computer
- inputPushDir = nil --this is for what direction
- --the input inventory pushes
- --out items.
- inputSide = nil --this is for where the input
- --inventory (the one players have
- --access to) is in relation to
- --the computer.
- bankPushDir = nil --this is for what direction
- --the output inventory pushes
- --out items
- sounds = { --contains the names of sound
- dealCard="dig.gravel",--clips to use. must be valid
- select="random.click",--names from minecraft's
- win="random.levelup", --sounds.json
- discard="dig.cloth",
- chaching="random.orb"}
- --
- --OK! this is where the actual code for the program
- --starts! You probably shouldn't mess with it...
- 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}}
- suitDisplays = {{" () ","()()"," /\\ ",color=colors.black},{" /\\ ","(__)"," /\\ ",color=colors.black},{"/\\/\\","\\ /"," \\/ ",color=colors.red},{" /\\ ","< >"," \\/ ",color=colors.red}}
- 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"}}
- readyForNextRound = true
- gameInProgress = false
- winnings = 0
- if inputSide == nil or bankSide == nil or inputPushDir == nil or bankPushDir == nil then
- term.clear()
- term.setCursorPos(1,1)
- term.write("Config variables have not been set")
- term.setCursorPos(1,2)
- term.write("Press any key to exit")
- os.pullEvent("key")
- term.clear()
- term.setCursorPos(1,1)
- return
- end
- width,height = term.getSize()
- infoPanel = window.create(term.current(),width-19,4,20,10)
- input = peripheral.wrap(inputSide)
- dealButton = window.create(term.current(),width-13,18,8,1)
- moneyLabel = window.create(term.current(),width-12,15,12,2)
- bank = peripheral.wrap(bankSide)
- deck = {}
- currentHand = {}
- --set up currentHand
- for c=1,5 do
- table.insert(currentHand,c,{card=nil,keep=false,win=window.create(term.current(),1,1+4*(c-1),15,4)})
- end
- --construct deck
- for s=1,4 do
- for v=1,13 do
- table.insert(deck,1,{suit=s,value=v})
- end
- end
- --renders all the 1 time only windows
- function initGui ()
- term.setBackgroundColor(colors.black)
- term.clear()
- text = "Made by JereTheJuggler"
- term.setTextColor(colors.white)
- term.setCursorPos(17,height)
- term.write(text)
- --top payout chart
- term.setCursorPos(width-19,1)
- term.setTextColor(colors.yellow)
- term.write(",------------------,")
- term.setCursorPos(width-19,2)
- term.write("| |")
- term.setCursorPos(width-19,3)
- term.write("|__________________|")
- term.setCursorPos(width-18,2)
- term.setTextColor(moneyColor)
- term.write(" PAYOUT TABLE ")
- --surrounding money label
- term.setTextColor(colors.yellow)
- term.setCursorPos(width-19,14)
- term.write("|------------------|")
- term.setCursorPos(width-19,15)
- term.write("| |")
- term.setCursorPos(width-19,16)
- term.write("| |")
- term.setCursorPos(width-18,17)
- term.write("------------------")
- term.setCursorPos(width-18,16)
- term.write("Money:")
- --deal button
- dealButton.setBackgroundColor(colors.red)
- dealButton.clear()
- dealButton.setCursorPos(3,1)
- dealButton.setTextColor(colors.black)
- dealButton.write("Deal")
- end
- --method that plays sounds
- function playSound (sound,freq)
- note = peripheral.find("note_block")
- if note == nil then
- return
- end
- if freq == nil then
- freq = .9
- end
- if volume == nil then
- vol = 1
- else
- vol = volume
- end
- note.playSound(sound,freq,vol)
- end
- --method that draws the entire gui
- function drawGui ()
- renderCurrentHand()
- renderInfoPanel()
- end
- --renders the window with the specified index of
- --the current hand. if index is nil it renders all
- function renderCurrentHand (index)
- if index == nil then
- startIndex=1
- endIndex=5
- else
- startIndex=index
- endIndex=index
- end
- for c=startIndex,endIndex do
- if currentHand[c].card==nil then
- currentHand[c].win.setBackgroundColor(colors.black)
- currentHand[c].win.clear()
- else
- if currentHand[c].keep then
- currentHand[c].win.setBackgroundColor(colors.gray)
- currentHand[c].win.clear()
- currentHand[c].win.setBackgroundColor(colors.black)
- currentHand[c].win.setCursorPos(1,4)
- currentHand[c].win.write(" ")
- currentHand[c].win.setBackgroundColor(colors.gray)
- currentHand[c].win.setCursorPos(currentHand[c].win.getSize()-3,1)
- currentHand[c].win.setTextColor(colors.green)
- currentHand[c].win.write("KEEP")
- else
- currentHand[c].win.setBackgroundColor(colors.white)
- currentHand[c].win.clear()
- currentHand[c].win.setBackgroundColor(colors.lightGray)
- currentHand[c].win.setTextColor(colors.gray)
- currentHand[c].win.setCursorPos(1,4)
- currentHand[c].win.write("___________________")
- currentHand[c].win.setBackgroundColor(colors.white)
- end
- currentHand[c].win.setTextColor(suitDisplays[currentHand[c].card.suit].color)
- for s=1,3 do
- currentHand[c].win.setCursorPos(1,s)
- currentHand[c].win.write(valueDisplays[currentHand[c].card.value][s].." "..suitDisplays[currentHand[c].card.suit][s])
- end
- end
- end
- end
- --renders the info panel showing hands and payouts
- --if index isn't nil it hilights the specified hand
- function renderInfoPanel (index)
- panelWidth,panelHeight = infoPanel.getSize()
- infoPanel.clear()
- for i=1,table.getn(hands) do
- infoPanel.setCursorPos(1,panelHeight-i+1)
- infoPanel.setBackgroundColor(colors.black)
- infoPanel.setTextColor(colors.yellow)
- infoPanel.write("| | |")
- infoPanel.setCursorPos(2,panelHeight-i+1)
- infoPanel.setTextColor(colors.white)
- if i==index then
- infoPanel.setBackgroundColor(colors.lightBlue)
- infoPanel.write(" ")
- infoPanel.setCursorPos(17,panelHeight-i+1)
- infoPanel.write(" ")
- infoPanel.setCursorPos(2,panelHeight-i+1)
- end
- infoPanel.write(hands[i].name)
- infoPanel.setCursorPos(20-string.len(tostring(hands[i].payout)),panelHeight-i+1)
- infoPanel.write(tostring(hands[i].payout))
- end
- end
- --deal first cards to the player
- usedCards = {}
- function dealFirstCards ()
- usedCards = {}
- for c=1,5 do
- currentHand[c]["card"] = nil
- renderCurrentHand(c)
- end
- playSound("note.harp",1.32)
- input.condenseItems()
- slot = 1
- items = input.getAllStacks()
- while slot <= input.getInventorySize() do
- if items[slot] ~= nil then
- if items[slot].basic().name == payment then
- playSound("note.harp",1.8)
- input.pushItem(inputPushDir,slot,costToPlay)
- break
- else
- slot = slot + 1
- end
- else
- slot = slot+1
- end
- end
- for i=1,5 do
- currentHand[i]["card"] = nil
- currentHand[i]["keep"] = false
- end
- usedCards = {}
- for c=1,5 do
- pickACard(c)
- sleep(.25)
- end
- end
- function dealSecondCards ()
- keeps = {}
- for c=1,5 do
- table.insert(keeps,c,currentHand[c].keep)
- end
- for c=1,5 do
- if not keeps[c] then
- currentHand[c]["card"] = nil
- renderCurrentHand(c)
- playSound(sounds.discard)
- sleep(.25)
- end
- end
- for c=1,5 do
- if not keeps[c] then
- pickACard(c)
- sleep(.25)
- end
- end
- for c=1,5 do
- if keeps[c] then
- currentHand[c]["keep"] = false
- renderCurrentHand(c)
- end
- end
- gameInProgress = false
- end
- --returns a card object that hasn't been used
- function pickACard (index)
- repeat
- continue = true
- card=deck[math.floor(1+math.random()*52)]
- if table.getn(usedCards) > 0 then
- for i=1,table.getn(usedCards) do
- if usedCards[i] == card.value..card.suit then
- continue = false
- end
- end
- end
- until continue
- table.insert(usedCards,card.value..card.suit)
- currentHand[index]["card"]=card
- playSound(sounds.dealCard,1.32)
- renderCurrentHand(index)
- end
- --renders the money label
- function renderMoneyLabel ()
- moneyLabel.clear()
- if winnings ~= 0 then
- moneyLabel.setCursorPos(moneyLabel.getSize()-string.len("+"..tostring(winnings))+1,1)
- moneyLabel.setTextColor(colors.lime)
- moneyLabel.write("+"..tostring(winnings))
- end
- moneyLabel.setCursorPos(moneyLabel.getSize()-string.len(tostring(money))+1,2)
- moneyLabel.setTextColor(moneyColor)
- moneyLabel.write(tostring(money))
- end
- winnings = 0
- gameInProgress = false
- readyForNextRound = true
- function playGame ()
- gameInProgress = false
- while true do
- if winnings == 0 then
- event,arg1,arg2,arg3,arg4 = os.pullEvent()
- if event == "mouse_click" then
- xPos = arg2
- yPos = arg3
- if isWithinWindow(dealButton) then
- if not gameInProgress and readyForNextRound then
- if money >= costToPlay and getAvailableMoney(bank) >= 500 then
- renderInfoPanel()
- playSound("note.harp",.9)
- gameInProgress = true
- dealFirstCards()
- else
- if getAvailableMoney(bank) < 500 then
- term.setTextColor(colors.red)
- term.setCursorPos(16,1)
- term.write("Bank is short on")
- term.setCursorPos(16,2)
- term.write("funds. Please")
- term.setCursorPos(16,3)
- term.write("contact operator")
- term.setCursorPos(16,4)
- term.write(operatorName)
- sleep(5)
- for n=1,4 do
- term.setCursorPos(16,n)
- term.write(" ")
- end
- end
- end
- elseif gameInProgress then
- dealButton.setVisible(false)
- term.setCursorPos(dealButton.getPosition())
- term.write(" ")
- dealSecondCards()
- winnings = analyzeCards()
- if winnings == 0 then
- dealButton.setVisible(true)
- dealButton.redraw()
- end
- gameInProgress = false
- end
- elseif gameInProgress then
- for c=1,5 do
- if isWithinWindow(currentHand[c].win) then
- if currentHand[c].keep then
- currentHand[c]["keep"] = false
- playSound(sounds.select)
- else
- playSound(sounds.select)
- currentHand[c]["keep"] = true
- end
- renderCurrentHand(c)
- break
- end
- end
- end
- elseif event == "key" then
- if arg1 == keys.backspace then
- return
- elseif arg1 == keys.leftShift then
- --winnings = 20
- end
- end
- else
- sleep(.5)
- end
- end
- end
- function analyzeCards()
- suits = {}
- values = {}
- for c=1,5 do
- table.insert(suits,c,currentHand[c].card.suit)
- table.insert(values,c,currentHand[c].card.value)
- end
- values = sort(values)
- flush = false
- if suits[1] == suits[2] and suits[2] == suits[3] and suits[3] == suits[4] and suits[4] == suits[5] then
- flush = true
- end
- 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
- (values[1] == 1 and values[2] == 10 and values[3] == 11 and values[4] == 12 and values[5] == 13) then
- if flush then
- if values[1] == 1 and values[5] == 13 then
- return getWinningsFor("Royal Flush")
- else
- return getWinningsFor("Straight Flush")
- end
- else
- return getWinningsFor("Straight")
- end
- end
- if (values[1] == values[2] and values[2] == values[3] and values[3] == values[4]) or
- (values[2] == values[3] and values[3] == values[4] and values[4] == values[5]) then
- return getWinningsFor("4 of a Kind")
- end
- if (values[1] == values[2] and values[3] == values[4] and values[4] == values[5]) or
- (values[1] == values[2] and values[2] == values[3] and values[4] == values[5]) then
- return getWinningsFor("Full House")
- end
- if flush then
- return getWinningsFor("Flush")
- end
- if values[1] == values[3] or values[2] == values[4] or values[3] == values[5] then
- return getWinningsFor("3 of a Kind")
- end
- if (values[1] == values[2] and values[3] == values[4]) or
- (values[1] == values[2] and values[4] == values[5]) or
- (values[2] == values[3] and values[4] == values[5]) then
- return getWinningsFor("2 Pairs")
- end
- for i=1,4 do
- if values[i] == values[i+1] and (values[i] == 1 or values[i] >= 11) then
- return getWinningsFor("High Pair")
- end
- end
- return getWinningsFor("Junk")
- end
- function sort (values)
- sortedValues = {}
- table.insert(sortedValues,1,values[1])
- for c=2,5 do
- alreadyPlaced = false
- for d=1,table.getn(sortedValues)+1 do
- if alreadyPlaced == false then
- if d==table.getn(sortedValues)+1 then
- table.insert(sortedValues,d,values[c])
- elseif sortedValues[d] >= values[c] then
- table.insert(sortedValues,d,values[c])
- alreadyPlaced = true
- end
- end
- end
- end
- return sortedValues
- end
- function getWinningsFor (hand)
- for c=1,table.getn(hands) do
- if hands[c].name == hand then
- renderInfoPanel(c)
- return hands[c].payout
- end
- end
- end
- function moneyListener ()
- prevMoney = -1
- while true do
- money = getAvailableMoney(input)
- if prevMoney ~= money or winnings ~= 0 then
- if winnings ~= 0 then
- readyForNextRound = false
- renderMoneyLabel()
- playSound(sounds.win,1)
- sleep(.65)
- repeat
- bank.condenseItems()
- bank.pushItem(bankPushDir,1,1)
- winnings = winnings - 1
- money = money + 1
- playSound(sounds.chaching,1.8)
- renderMoneyLabel()
- until winnings == 0
- loopCounter=0
- repeat
- actualMoney = getAvailableMoney(input)
- loopCounter = loopCounter + 1
- until actualMoney == money or loopCounter >= 100
- dealButton.setVisible(true)
- dealButton.redraw()
- readyForNextRound = true
- end
- renderMoneyLabel()
- prevMoney = money
- else
- prevMoney = money
- sleep(.125)
- end
- end
- end
- --returns whether or not a click was on a window
- function isWithinWindow (win)
- xMin,yMin=win.getPosition()
- winWidth,winHeight = win.getSize()
- xMax = xMin+winWidth-1
- yMax = yMin+winHeight-1
- return (xPos <= xMax and xPos >= xMin and yPos <= yMax and yPos >= yMin)
- end
- --returns how much money is in the source inv.
- function getAvailableMoney (source)
- source.condenseItems()
- items = source.getAllStacks()
- if table.getn(items) == 0 then
- return 0
- end
- total = 0
- for i=1,source.getInventorySize() do
- if items[i] ~= nil then
- if items[i].basic().name == payment then
- total = total + items[i].basic().qty
- end
- else
- break
- end
- end
- return total
- end
- initGui()
- drawGui()
- parallel.waitForAny(playGame,moneyListener)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement