Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Blackjack Game For ComputerCraft --
- -- Created by iiAmPanda --
- -- pastebin get b7NvUZxu blackjack(wip) --
- -- Attach the monitor
- local mon = peripheral.wrap("right") -- Using the correct monitor name
- mon.setTextScale(0.5) -- Adjust text size to fit on the screen
- -- Initialize variables
- local deck, playerHand, dealerHand = {}, {}, {}
- local playerTurn = true -- Track if it's the player's turn
- local credits = 0
- local bet = 0
- -- Function to check for a Casino Card
- function checkForCasinoCard()
- return fs.exists("disk/creds.lua")
- end
- -- Function to create and shuffle the deck
- local function initializeDeck()
- deck = {}
- local suits = {"H", "D", "C", "S"} -- Suit identifiers
- local ranks = {
- {name = "2", value = 2}, {name = "3", value = 3}, {name = "4", value = 4},
- {name = "5", value = 5}, {name = "6", value = 6}, {name = "7", value = 7},
- {name = "8", value = 8}, {name = "9", value = 9}, {name = "10", value = 10},
- {name = "J", value = 10}, {name = "Q", value = 10}, {name = "K", value = 10},
- {name = "A", value = 11}
- }
- for _, suit in ipairs(suits) do
- for _, rank in ipairs(ranks) do
- table.insert(deck, {suit = suit, rank = rank.name, value = rank.value})
- end
- end
- -- Shuffle the deck
- for i = #deck, 2, -1 do
- local j = math.random(i)
- deck[i], deck[j] = deck[j], deck[i]
- end
- end
- -- Function to deal a card
- local function dealCard(hand)
- local card = table.remove(deck)
- table.insert(hand, card)
- end
- -- Function to calculate hand value (with Ace adjustment)
- local function calculateHandValue(hand)
- local value = 0
- local numAces = 0
- for _, card in ipairs(hand) do
- value = value + card.value
- if card.rank == "A" then
- numAces = numAces + 1
- end
- end
- -- Adjust for Aces if value > 21
- while value > 21 and numAces > 0 do
- value = value - 10
- numAces = numAces - 1
- end
- return value
- end
- -- Function to draw a suit symbol on the monitor using ASCII codes
- local function drawSuit(x, y, suit)
- mon.setCursorPos(x, y)
- if suit == "H" then
- mon.write(string.char(3)) -- Heart (ASCII Code 3)
- elseif suit == "D" then
- mon.write(string.char(4)) -- Diamond (ASCII Code 4)
- elseif suit == "C" then
- mon.write(string.char(5)) -- Club (ASCII Code 5)
- elseif suit == "S" then
- mon.write(string.char(6)) -- Spade (ASCII Code 6)
- end
- end
- -- Function to draw a card on the monitor
- local function drawCard(x, y, card)
- mon.setCursorPos(x, y)
- mon.write("+---+")
- mon.setCursorPos(x, y + 1)
- mon.write("| " .. card.rank .. " |") -- Center rank within card
- mon.setCursorPos(x, y + 2)
- drawSuit(x + 1, y + 2, card.suit) -- Draw suit symbol
- mon.setCursorPos(x, y + 3)
- mon.write("| |") -- Reduced space for bottom
- mon.setCursorPos(x, y + 4)
- mon.write("+---+")
- end
- -- Function to display hands and buttons on the monitor
- local function displayGame()
- mon.setBackgroundColor(colors.black) -- Set background color to black
- mon.setTextColor(colors.white) -- Set text color to white
- mon.clear() -- Clear the monitor
- -- Display player hand
- mon.setCursorPos(1, 1)
- mon.write("Player: ")
- for i, card in ipairs(playerHand) do
- drawCard(i * 7, 2, card) -- Spacing for skinnier cards
- end
- local playerTotal = calculateHandValue(playerHand)
- mon.setCursorPos(1, 8)
- mon.write("P Total: " .. playerTotal)
- -- Display dealer hand
- mon.setCursorPos(1, 9)
- mon.write("Dealer: ")
- for i, card in ipairs(dealerHand) do
- if i == 1 or not playerTurn then
- drawCard(i * 7, 10, card) -- Show the dealer's first card
- else
- mon.setCursorPos(i * 7, 10)
- mon.write(" ? ") -- Hide the second card
- end
- end
- -- Display action buttons below the dealer cards
- mon.setCursorPos(1, 16) -- Moved down to line 16
- mon.write("[H] Hit") -- Hit button
- mon.setCursorPos(15, 16) -- Spaced out
- mon.write("[S] Stand") -- Stand button
- end
- -- Function to display betting screen
- local function displayBettingScreen()
- mon.setBackgroundColor(colors.black) -- Set background color to green
- mon.setTextColor(colors.white) -- Set text color to black
- mon.clear()
- -- Draw a border
- mon.setCursorPos(1, 1)
- mon.write("+----------------------+")
- mon.setCursorPos(1, 2)
- mon.write("| Place Your Bet |")
- mon.setCursorPos(1, 3)
- mon.write("| Max Bet: " .. credits .. " |") -- Show max bet dynamically
- mon.setCursorPos(1, 4)
- mon.write("+----------------------+")
- mon.setCursorPos(2, 6)
- mon.write("Current Bet: " .. bet) -- Display current bet
- -- Draw buttons for adjusting the bet
- mon.setCursorPos(5, 8)
- mon.write("[+]") -- Plus button
- mon.setCursorPos(12, 8)
- mon.write("[-]") -- Minus button
- mon.setCursorPos(1, 10)
- mon.write("+----------------------+")
- mon.setCursorPos(1, 11)
- mon.write("| [C] Confirm Bet |") -- Confirmation button
- mon.setCursorPos(1, 12)
- mon.write("+----------------------+")
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- if y == 8 and x >= 5 and x <= 7 then -- Plus button clicked
- if bet < credits then
- bet = bet + 1
- end
- elseif y == 8 and x >= 12 and x <= 14 then -- Minus button clicked
- if bet > 0 then
- bet = bet - 1
- end
- elseif y == 11 and x >= 1 and x <= 19 then -- Confirm button clicked
- if bet > 0 then
- break -- Exit betting screen
- end
- end
- -- Update the displayed bet amount
- mon.setCursorPos(2, 6)
- mon.write("Current Bet: " .. bet) -- Refresh the displayed bet
- sleep(0.05) -- Yield slightly to prevent long execution
- end
- end
- -- Function to read credits from the Casino Card
- function readCard()
- if not fs.exists("disk/creds.lua") then
- return false
- end
- local Card = fs.open("disk/creds.lua", "r")
- if not Card then
- displayResults("Error reading from disk!") -- Better error display
- return false
- end
- local data = Card.readAll()
- Card.close()
- local a, b = string.find(data, "11066011")
- local c, d = string.find(data, "11077011")
- credits = tonumber(string.sub(data, b + 1, c - 1))
- return true
- end
- -- Function to display results
- local function displayResults(message)
- mon.setBackgroundColor(colors.black) -- Change background for results
- mon.setTextColor(colors.white) -- Change text color for visibility
- mon.clear()
- mon.setCursorPos(1, 1)
- mon.write(message)
- sleep(3) -- Pause before clearing the screen
- end
- -- Function to handle player's turn
- local function playerTurnAction(action)
- if action == "hit" then
- dealCard(playerHand)
- local playerTotal = calculateHandValue(playerHand)
- displayGame() -- Update the display after hitting
- if playerTotal > 21 then
- playerTurn = false -- End player's turn if bust
- displayResults("BUST! Dealer Wins!")
- credits = credits - bet -- Deduct bet on loss
- end
- elseif action == "stand" then
- playerTurn = false -- End player's turn
- end
- end
- -- Function to handle dealer's turn
- local function dealerTurn()
- while calculateHandValue(dealerHand) < 17 do
- dealCard(dealerHand)
- sleep(0.05) -- Yield slightly to prevent long execution
- end
- end
- -- Main game function
- local function playBlackjack()
- if not checkForCasinoCard() then
- displayResults("Insert Casino Card to Play!")
- return
- end
- if not readCard() then
- return
- end
- displayBettingScreen() -- Show betting screen
- -- Game starts here
- initializeDeck() -- Prepare a new deck
- playerHand, dealerHand = {}, {}
- dealCard(playerHand)
- dealCard(dealerHand)
- dealCard(playerHand)
- dealCard(dealerHand)
- -- Player's turn
- while playerTurn do
- displayGame() -- Update display with hands
- local event, key = os.pullEvent("char")
- if key == "h" then
- playerTurnAction("hit")
- elseif key == "s" then
- playerTurnAction("stand")
- end
- end
- -- Dealer's turn
- dealerTurn()
- -- Determine winner
- local playerTotal = calculateHandValue(playerHand)
- local dealerTotal = calculateHandValue(dealerHand)
- if playerTotal > 21 then
- displayResults("BUST! Dealer Wins!")
- credits = credits - bet -- Deduct bet on loss
- elseif dealerTotal > 21 then
- displayResults("Dealer BUSTS! You Win!")
- credits = credits + bet -- Add bet to credits on win
- elseif playerTotal > dealerTotal then
- displayResults("You Win!")
- credits = credits + bet -- Add bet to credits on win
- elseif playerTotal < dealerTotal then
- displayResults("Dealer Wins!")
- credits = credits - bet -- Deduct bet on loss
- else
- displayResults("It's a Tie!")
- end
- sleep(3)
- end
- -- Start the game loop
- while true do
- playBlackjack() -- Play the game
- end
Add Comment
Please, Sign In to add comment