Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- INITIALISATION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- local paintutils = require("//pup")
- -- Locate and wrap monitor
- mon = peripheral.find("monitor")
- -- Change the colour green to a deep green
- mon.setPaletteColor(colors.green,0,176,0)
- -- Change the colour lime to a slightly lighter deep green
- mon.setPaletteColor(colors.lime,0,174,0)
- -- Minimise text scale
- mon.setTextScale(0.5)
- -- Get the size of the monitor
- maxX,maxY = mon.getSize()
- -- Reset the monitor
- mon.clear()
- -- Create a window for the background
- local background = window.create(mon, 1, 1, maxX, maxY, true)
- term.redirect(background)
- -- Draw the background as # in every pixel
- -- fg/bg are the replaced shades of lime/green
- for y = 1,maxY do
- term.setCursorPos(1,y)
- term.blit(string.rep("#",maxX),string.rep("5",maxX),string.rep("d",maxX))
- end
- term.redirect(term.native())
- -- Initialise each suit icon in a table
- suits = {"h","d","s","c"}
- -- Initialise each card value in a table
- values = {"a","2","3","4","5","6","7","8","9","j","q","k"}
- -- Create empty table for storing a deck of cards
- -- Each pack has a card for every suit/value combo
- -- Add set number of packs together to make a deck
- function shuffleDeck(packsUsed)
- deck = {}
- if not packsUsed then packsUsed = 1 end
- for decks = 1,packsUsed do
- for suit = 1,#suits do
- for value = 1,#values do
- table.insert(deck,tostring(values[value]..suits[suit]))
- end
- end
- end
- end
- -- Create empty tables for storing player and dealer hands
- hand = {
- ["d"] = {},
- ["p"] = {}
- }
- -- Set a score the dealer will stop drawing at
- dealerLimit = 17
- -- FUNCTIONS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- -- Clear the monitor by redrawing the 'table'
- function clearTable()
- background.setVisible(true)
- background.redraw()
- background.setVisible(false)
- end
- -- Draw a card object at a given location on the monitor (point is top left of card)
- -- Take given suit and value to write on card
- function drawCard(target,value,suit)
- local clr = nil
- if suit == "h" or suit == "d" then
- clr = "red"
- else
- clr = "black"
- end
- term.redirect(target)
- if suit == "?" then
- paintutils.loadImage("cards/back.nfp"):upscale():drawImage(1,1)
- else
- paintutils.loadImage("cards/blank.nfp"):upscale():drawImage(1,1)
- paintutils.loadImage("cards/values/"..value..".nfp"):upscale(clr):drawImage(3,2)
- paintutils.loadImage("cards/values/"..value..".nfp"):mirror("x","y"):upscale(clr):drawImage(7,8)
- end
- if value == "q" then
- paintutils.loadImage("cards/faces/q.nfp"):upscale(clr):drawImage(5,2)
- paintutils.loadImage("cards/faces/q.nfp"):mirror("x","y"):upscale(clr):drawImage(2,7)
- paintutils.loadImage("cards/suits/s"..suit..".nfp"):upscale(1):drawImage(3,4)
- paintutils.loadImage("cards/suits/s"..suit..".nfp"):mirror("x","y"):upscale(1):drawImage(7,6)
- elseif value == "k" or value == "j" then
- paintutils.loadImage("cards/faces/"..value..".nfp"):upscale(clr):drawImage(5,2)
- paintutils.loadImage("cards/faces/"..value..".nfp"):mirror("x","y"):upscale(clr):drawImage(3,7)
- paintutils.loadImage("cards/suits/s"..suit..".nfp"):upscale(1):drawImage(3,4)
- paintutils.loadImage("cards/suits/s"..suit..".nfp"):mirror("x","y"):upscale(1):drawImage(7,6)
- elseif value ~= "?" then
- paintutils.loadImage("cards/suits/"..suit..".nfp"):upscale(1):drawImage(3,4)
- end
- term.redirect(term.native())
- end
- -- Add one card to given hand table, remove card from deck and return new table
- function dealCard(target)
- drawnCard = math.random(1,#deck)
- table.insert(target, {["v"] = deck[drawnCard], ["i"] = window.create(mon,1,1,10,10,false)})
- table.remove(deck, drawnCard)
- drawCard(target[#target].i,target[#target].v:sub(1,1),target[#target].v:sub(2,2))
- return target
- end
- -- Calculate the total value of a hand and return the score
- function getScore(target)
- score = 0
- aceCount = 0
- for c = 1,#target do
- v = target[c].v:sub(1,1)
- if tonumber(v) then
- score = score + v
- elseif v ~= "a" then
- score = score + 10
- else
- aceCount = aceCount + 1
- end
- end
- if aceCount > 0 then
- for i = 1,aceCount do
- if score + 11 > 21 then
- score = score + 1
- else
- score = score + 11
- end
- end
- end
- return score
- end
- -- Display the player hand and score
- function showHandP()
- for c = 1,#hand.p do
- hand.p[c].i.reposition((math.floor((maxX - ((#hand.p * 11) - 1)) / 2) + 1) + ((c - 1) * 11),(maxY/2)+6)
- hand.p[c].i.setVisible(true)
- hand.p[c].i.redraw()
- hand.p[c].i.setVisible(false)
- end
- mon.setBackgroundColour(colors.green)
- mon.setTextColour(colors.black)
- mon.setCursorPos((maxX-10)/2,(maxY/2)+4)
- if getScore(hand.p) > 9 then
- mon.write("Player: "..getScore(hand.p))
- else
- mon.write("Player: 0"..getScore(hand.p))
- end
- end
- -- Display the dealer hand and score
- function showHandD()
- for c = 1,#hand.d do
- hand.d[c].i.reposition((math.floor((maxX - ((#hand.d * 11) - 1)) / 2) + 1) + ((c - 1) * 11),(maxY/2)-13)
- hand.d[c].i.setVisible(true)
- hand.d[c].i.redraw()
- hand.d[c].i.setVisible(false)
- end
- mon.setBackgroundColour(colors.green)
- mon.setTextColour(colors.black)
- mon.setCursorPos((maxX-10)/2,(maxY/2)-2)
- if getScore(hand.d) > 9 then
- mon.write("Dealer: "..getScore(hand.d))
- else
- mon.write("Dealer: 0"..getScore(hand.d))
- end
- end
- function hideDealer()
- term.redirect(mon)
- paintutils.loadImage("cards/back.nfp"):upscale():drawImage(((maxX-20)/2)+1,(maxY/2)-13)
- term.redirect(term.native())
- mon.setCursorPos((maxX-10)/2,(maxY/2)-2)
- mon.write("Dealer: ??")
- end
- -- RUNTIME~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- while true do
- clearTable()
- shuffleDeck(2)
- hand.p = {}
- hand.d = {}
- -- Deal 2 cards to player and dealer respectively
- for i = 1,2 do
- hand.p = dealCard(hand.p)
- hand.d = dealCard(hand.d)
- end
- -- Show 1 card from dealer hand, leave other hidden
- --[[
- mon.setCursorPos(4,13)
- mon.write("Dealer: ??")
- mon.setCursorPos(4,maxY-12)
- mon.write("Player: ??")
- sleep(0.3)
- drawCard(4,maxY-10,hand.p[1]:sub(1,1),hand.p[1]:sub(2,2))
- sleep(0.3)
- drawCard(maxX-11,4,"?","?")
- sleep(0.3)
- drawCard(15,maxY-10,hand.p[2]:sub(1,1),hand.p[2]:sub(2,2))
- sleep(0.3)
- drawCard(maxX-22,4,hand.d[2]:sub(1,1),hand.d[2]:sub(2,2))
- ]]--
- showHandP()
- showHandD()
- hideDealer()
- if getScore(hand.p) ~= 21 then
- -- Loop gameplay until player is bust or decides to stand
- -- Show new hand after each card drawn
- playing = true
- while playing do
- if getScore(hand.p) > 21 or #hand.p == 5 then
- playing = false
- else
- choice = {os.pullEvent("monitor_touch")}
- if choice[3] > maxX/2 then
- playing = false
- print("Player Stands!")
- else
- print("Player Hit!")
- hand.p = dealCard(hand.p)
- clearTable()
- showHandP()
- showHandD()
- hideDealer()
- sleep(0.5)
- end
- end
- end
- end
- -- If player is not bust, dealer draws cards
- -- Until dealer reaches their set limit, goes bust, or has higher score than player
- -- Only happens if player doesn't have BJ
- showHandD()
- if not (getScore(hand.p) == 21 and #hand.p == 2) then
- if getScore(hand.p) < 22 and #hand.p < 5 then
- while (getScore(hand.d) < dealerLimit) do
- print("Dealer Hit!")
- hand.d = dealCard(hand.d)
- sleep(0.5)
- clearTable()
- showHandD()
- showHandP()
- end
- if getScore(hand.d) < 22 then
- print("Dealer Stands!")
- end
- end
- end
- -- Determine winner based on hand scores
- outcome = ""
- if getScore(hand.p) > 21 then
- outcome = "House Win - Player Bust!"
- elseif #hand.p == 5 then
- outcome = "Player Win - 5 Card Charlie!"
- elseif getScore(hand.d) > 21 then
- outcome = "Player Win - Dealer Bust!"
- elseif getScore(hand.p) < 21 and getScore(hand.p) == getScore(hand.d) then
- outcome = "Push - Scores Tied!"
- elseif getScore(hand.p) == 21 and getScore(hand.d) == 21 then
- if #hand.p == 2 then
- if #hand.d ~= #hand.p then
- outcome = "Player Win - Natural Blackjack!"
- else
- outcome = "Push - Double Blackjack!"
- end
- elseif #hand.d == 2 then
- outcome = "House Win - Natural Blackjack!"
- end
- elseif getScore(hand.p) > getScore(hand.d) then
- outcome = "Player Win - High Hand!"
- else
- outcome = "House Win - High Hand!"
- end
- mon.setCursorPos((maxX-#outcome)/2,(maxY-1)/2+2)
- mon.write(outcome)
- -- Wait for key press to end game
- os.pullEvent("monitor_touch")
- end
Add Comment
Please, Sign In to add comment