Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Init
- print("Enter your bet here:")
- term.redirect(peripheral.wrap("top"))
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.red)
- term.clear()
- function center(str)
- curX,curY = term.getCursorPos()
- maxX,maxY = term.getSize()
- maxX = maxX / 2
- maxX = maxX - (#str/2)
- term.setCursorPos(maxX,curY)
- print(str)
- end
- deckCount = false
- cardCount = false
- analysis = {}
- -- Start Game
- paintutils.drawFilledBox(1,7,51,11,colors.lightGray)
- term.setCursorPos(1,8)
- term.setTextColor(colors.red)
- center("Blackjack")
- term.setCursorPos(1,10)
- term.setTextColor(colors.yellow)
- center("Deluxe")
- sleep(3)
- faceCount = 0
- for i=1,19 do
- paintutils.drawFilledBox(1,i,51,i,colors.green)
- sleep(0)
- end
- function shuffle()
- faceCount = 0
- term.setBackgroundColor(colors.green)
- term.clear()
- term.setCursorPos(1,9)
- term.setTextColor(colors.red)
- center("Shuffling deck...")
- cards = {
- "A", "A", "A", "A",
- "J", "J", "J", "J",
- "K", "K", "K", "K",
- "Q", "Q", "Q", "Q",
- "2", "2", "2", "2",
- "3", "3", "3", "3",
- "4", "4", "4", "4",
- "5", "5", "5", "5",
- "6", "6", "6", "6",
- "7", "7", "7", "7",
- "8", "8", "8", "8",
- "9", "9", "9", "9",
- "10", "10", "10", "10",
- }
- deck = {}
- for i,v in pairs(cards) do
- repeat
- pos = math.random(1,52)
- until deck[pos] == nil
- deck[pos] = v
- sleep(0)
- end
- end
- shuffle()
- playerHand = {}
- dealerHand = {}
- cash = 1000
- function countCard(sCard)
- if sCard == "A" or sCard == "J" or sCard == "K" or sCard == "Q" or sCard == "10" then
- faceCount = faceCount - 1
- elseif sCard == "2" or sCard == "3" or sCard == "4" or sCard == "5" or sCard == "6" then
- faceCount = faceCount + 1
- end
- end
- function dealSelf(hide)
- dealerHand[#dealerHand+1] = deck[#deck]
- if not hide then
- countCard(deck[#deck])
- end
- deck[#deck] = nil
- end
- function dealPlayer()
- playerHand[#playerHand+1] = deck[#deck]
- countCard(deck[#deck])
- deck[#deck] = nil
- end
- function drawCard(card,x,y) do
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.green)
- if card == "flipped" then
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.green)
- term.setCursorPos(x,y)
- write("+-+")
- term.setCursorPos(x,y+1)
- write("|*|")
- term.setCursorPos(x,y+2)
- write("+-+")
- elseif card == "10" then
- term.setCursorPos(x,y)
- write("10 ")
- term.setCursorPos(x,y+1)
- write(" ")
- term.setCursorPos(x,y+2)
- write(" 10")
- else
- term.setCursorPos(x,y)
- write(card.." ")
- term.setCursorPos(x,y+1)
- write(" ")
- term.setCursorPos(x,y+2)
- write(" "..card)
- end
- end
- end
- function getHandValue(tHand)
- nValue = 0
- nAces = 0
- for i,v in pairs(tHand) do
- if v == "A" then
- nAces = nAces + 1
- nValue = nValue + 11
- elseif v == "2" then
- nValue = nValue + 2
- elseif v == "3" then
- nValue = nValue + 3
- elseif v == "4" then
- nValue = nValue + 4
- elseif v == "5" then
- nValue = nValue + 5
- elseif v == "6" then
- nValue = nValue + 6
- elseif v == "7" then
- nValue = nValue + 7
- elseif v == "8" then
- nValue = nValue + 8
- elseif v == "9" then
- nValue = nValue + 9
- elseif v == "10" then
- nValue = nValue + 10
- elseif v == "J" then
- nValue = nValue + 10
- elseif v == "K" then
- nValue = nValue + 10
- elseif v == "Q" then
- nValue = nValue + 10
- end
- end
- repeat
- if nValue > 21 and nAces > 0 then
- nAces = nAces - 1
- nValue = nValue - 10
- end
- until nAces <= 0 or nValue <= 21
- if nAces > 0 then
- soft = true
- else
- soft = false
- end
- return nValue,soft
- end
- dealerShowing = false
- buttons = false
- function redraw()
- term.setBackgroundColor(colors.green)
- term.clear()
- term.setTextColor(colors.red)
- spacing = 25-(#dealerHand*2)
- for i,v in pairs(dealerHand) do
- if dealerShowing or i == 1 then
- drawCard(v,spacing,3)
- else
- drawCard("flipped",spacing,3)
- end
- spacing = spacing + 4
- end
- spacing = 25-(#playerHand*2)
- for i,v in pairs(playerHand) do
- if i == #playerHand and doubled and not dealerShowing then
- drawCard("flipped",spacing,13)
- else
- drawCard(v,spacing,13)
- end
- spacing = spacing + 4
- end
- if buttons then
- term.setCursorPos(10,17)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.gray)
- write(" Stand ")
- term.setCursorPos(18,17)
- if doubled then
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- else
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.gray)
- end
- write(" Hit ")
- term.setCursorPos(24,17)
- if cash >= (bet*2) and not doubled then
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.gray)
- else
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- end
- write(" Double ")
- term.setCursorPos(33,17)
- if #playerHand == 2 and playerHand[1] == playerHand[2] then
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.gray)
- else
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- end
- write(" Split ")
- end
- term.setCursorPos(2,16)
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.red)
- write("Cash:")
- term.setCursorPos(2,17)
- term.setTextColor(colors.lime)
- write("$"..tostring(cash))
- term.setCursorPos(45,16)
- term.setTextColor(colors.red)
- if deckCount then
- write("Deck:")
- term.setCursorPos(46,17)
- term.setTextColor(colors.lightGray)
- write(tostring(#deck))
- end
- if cardCount then
- write("Count:")
- term.setCursorPos(46,17)
- term.setTextColor(colors.red)
- write(tostring(faceCount))
- end
- if #playerHand > 0 then
- term.setCursorPos(1,11)
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.gray)
- if dealerShowing or not doubled then
- center(tostring(getHandValue(playerHand)))
- else
- center("?")
- end
- term.setCursorPos(1,7)
- if dealerShowing then
- center(tostring(getHandValue(dealerHand)))
- else
- center("?")
- end
- end
- end
- function msg(str)
- paintutils.drawFilledBox(1,8,51,10,colors.gray)
- term.setCursorPos(1,9)
- term.setTextColor(colors.red)
- center(str)
- end
- function winAnim()
- dollars = {}
- for i=1,51 do
- dollars[i] = math.random(-5,0)
- end
- term.setTextColor(colors.yellow)
- term.setBackgroundColor(colors.green)
- for i=1,40 do
- for x,v in pairs(dollars) do
- if v >= 1 and v <= 51 then
- term.setCursorPos(x,v)
- write(" ")
- end
- dollars[x] = dollars[x]+1
- if (v+1) >= 1 and (v+1) <= 51 then
- term.setCursorPos(x,v+1)
- write("$")
- end
- end
- sleep(0.1)
- end
- for i=1,19 do
- paintutils.drawLine(1,i,51,i,colors.green)
- sleep(0)
- end
- end
- function log(str)
- f = fs.open("/log","a")
- f.writeLine(str)
- f.close()
- end
- function playHand()
- if cash == 0 then
- optA = true
- optB = true
- optC = true
- while true do
- term.setBackgroundColor(colors.green)
- term.clear()
- paintutils.drawImage(paintutils.loadImage("/Blackjack/dollar"),1,1)
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.red)
- term.setCursorPos(1,11)
- center("You're ruined!")
- term.setCursorPos(1,18)
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.gray)
- if optA then
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.gray)
- else
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- end
- center(" Move in with mom ")
- term.setCursorPos(1,16)
- if optB then
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.gray)
- else
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- end
- center(" Resort to crime ")
- term.setCursorPos(1,14)
- if optC then
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.gray)
- else
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.lightGray)
- end
- center(" Pray for money ")
- if true then
- e,c,x,y = os.pullEvent('mouse_click')
- if y == 18 and optA then
- optA = false
- if true then
- msg("She bails you out!")
- sleep(3)
- cash = 500
- break
- end
- elseif y == 16 and optB then
- optB = false
- if math.random(1,4) == 4 then
- msg("You start a drug operation!")
- sleep(3)
- cash = 15000
- break
- else
- msg("You'll get caught!")
- sleep(3)
- end
- elseif y == 14 and optC then
- optC = false
- if math.random(1,2) == 2 then
- msg("Your wish is granted!")
- sleep(3)
- cash = 1000
- break
- else
- msg("You live in sin!")
- sleep(3)
- end
- end
- end
- end
- end
- bet = 0
- buttons = false
- Blackjack = false
- playerBust = false
- dealerBust = false
- dealerShowing = false
- playerHand = {}
- dealerHand = {}
- if #deck < 12 then
- shuffle()
- end
- doubled = false
- redraw()
- if not splitCard then
- repeat
- paintutils.drawFilledBox(10,17,39,17,colors.green)
- term.setCursorPos(18,17)
- term.setTextColor(colors.red)
- write("Bet: ")
- bet = tonumber(read())
- if not bet then
- bet = cash+1
- end
- until bet <= cash
- end
- if not splitCard then
- dealPlayer()
- redraw()
- sleep(0.5)
- dealSelf()
- redraw()
- sleep(0.5)
- dealPlayer()
- redraw()
- sleep(0.5)
- dealSelf(true)
- redraw()
- else
- playerHand[#playerHand+1] = splitCard
- bet = splitBet
- splitCard = nil
- splitBet = nil
- dealSelf()
- redraw()
- sleep(0.5)
- dealPlayer()
- redraw()
- sleep(0.5)
- dealSelf(true)
- redraw()
- sleep(0.5)
- msg("Playing Split Hand")
- sleep(3)
- end
- continue = true
- if getHandValue(playerHand) == 21 then
- continue = false
- Blackjack = true
- end
- while continue do
- buttons = true
- redraw()
- if getHandValue(playerHand) > 21 then
- playerBust = true
- continue = false
- break
- end
- e,c,x,y = os.pullEvent("monitor_touch")
- if y == 17 then
- if x >= 10 and x <= 16 then
- -- Stand
- break
- end
- if x >= 18 and x <= 22 then
- -- Hit
- if not doubled then
- dealPlayer()
- end
- end
- if x >= 24 and x <= 31 then
- -- Double
- if cash >= (bet*2) and not doubled then
- doubled = true
- dealPlayer()
- bet = bet*2
- end
- end
- if x >= 33 and x <= 39 then
- -- Split
- if playerHand[1] == playerHand[2] then
- if #playerHand == 2 then
- splitBet = bet
- splitCard = playerHand[2]
- playerHand[2] = nil
- dealPlayer()
- end
- end
- end
- end
- end
- buttons = false
- dealerShowing = true
- countCard(dealerHand[2])
- if continue then
- for i=1,8 do
- redraw()
- value,soft = getHandValue(dealerHand)
- if value < 17 then
- dealSelf()
- sleep(0.5)
- elseif value == 17 and soft then
- dealSelf()
- sleep(0.5)
- end
- end
- end
- redraw()
- if Blackjack then
- cash = cash + (bet*1.5)
- msg("Blackjack!")
- analysis[#analysis+1] = "Blackjack"
- sleep(2)
- winAnim()
- return
- end
- if playerBust then
- cash = cash - bet
- msg("You Bust!")
- analysis[#analysis+1] = "bust"
- sleep(3)
- return
- end
- if getHandValue(dealerHand) > 21 then
- cash = cash + bet
- msg("Dealer Busts!")
- analysis[#analysis+1] = "dealerbust"
- sleep(2)
- winAnim()
- return
- end
- if getHandValue(dealerHand) > getHandValue(playerHand) then
- cash = cash - bet
- msg("You Lose!")
- analysis[#analysis+1] = "lose"
- sleep(3)
- return
- end
- if getHandValue(dealerHand) == getHandValue(playerHand) then
- msg("You Push!")
- analysis[#analysis+1] = "push"
- sleep(3)
- return
- end
- if getHandValue(playerHand) > getHandValue(dealerHand) then
- cash = cash + bet
- msg("You Win!")
- analysis[#analysis+1] = "win"
- sleep(2)
- winAnim()
- return
- end
- end
- while true do
- playHand()
- if #deck < 12 then
- term.setBackgroundColor(colors.gray)
- term.clear()
- term.setTextColor(colors.red)
- cBlackjack = 0
- cDealerbust = 0
- cBust = 0
- cLose = 0
- cPush = 0
- cWin = 0
- for i,v in pairs(analysis) do
- if v == "Blackjack" then
- cBlackjack = cBlackjack + 1
- elseif v == "dealerbust" then
- cDealerbust = cDealerbust + 1
- elseif v == "bust" then
- cBust = cBust + 1
- elseif v == "lose" then
- cLose = cLose + 1
- elseif v == "push" then
- cPush = cPush + 1
- elseif v == "win" then
- cWin = cWin + 1
- end
- end
- gameCount = cBlackjack + cDealerbust + cBust + cLose + cPush + cWin
- startPoint = 2
- colorz = {
- [6] = colors.purple,
- [4] = colors.lime,
- [1] = colors.red,
- [2] = colors.orange,
- [5] = colors.green,
- [3] = colors.yellow,
- }
- output = {
- [1] = cBust,
- [2] = cLose,
- [3] = cPush,
- [4] = cWin,
- [5] = cDealerbust,
- [6] = cBlackjack,
- }
- for i,v in pairs(output) do
- paintutils.drawLine(startPoint,4,(startPoint+((v/gameCount)*49)-1),4,colorz[i])
- startPoint = startPoint + ((v/gameCount)*49)
- end
- paintutils.drawPixel(1,4,colors.gray)
- paintutils.drawPixel(51,4,colors.gray)
- analysis = {}
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.red)
- term.setCursorPos(1,2)
- center("Game Analysis")
- term.setCursorPos(4,6)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.red)
- write("Bust ")
- term.setTextColor(colors.orange)
- write("Lose ")
- term.setTextColor(colors.yellow)
- write("Push ")
- term.setTextColor(colors.lime)
- write("Win ")
- term.setTextColor(colors.green)
- write("Dealer Bust ")
- term.setTextColor(colors.purple)
- write("Blackjack")
- term.setTextColor(colors.gray)
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(42,18)
- write(" Close ")
- repeat
- e,c,x,y = os.pullEvent("mouse_click")
- until x >= 42 and x <= 50 and y == 18
- end
- end
Add Comment
Please, Sign In to add comment