Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Blackjack Game For ComputerCraft --
- -- Created by iiAmPanda --
- -- pastebin get 2Z9Y9XvU blackjack --
- -- 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, playerHands, dealerHand = {}, {}, {}
- local playerTurn = true -- Track if it's the player's turn
- local playerBlackjack = false -- Track if player has blackjack
- local dealerBlackjack = false -- Track if dealer has blackjack
- local currentHandIndex = 1 -- Track which hand is currently being played
- -- 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 to a specific hand
- local function dealCardToHand(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(resultMessage)
- 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 hands
- for i, hand in ipairs(playerHands) do
- mon.setCursorPos(1, 1 + (i - 1) * 7)
- mon.write("Player Hand " .. i .. ": ")
- for j, card in ipairs(hand) do
- drawCard(j * 7, 2 + (i - 1) * 7, card) -- Spacing for skinnier cards
- end
- local handTotal = calculateHandValue(hand)
- mon.setCursorPos(1, 8 + (i - 1) * 7)
- mon.write("P Total: " .. handTotal)
- -- Display separate Hit button for each hand
- mon.setCursorPos(1, 9 + (i - 1) * 7)
- mon.write("[HIT]") -- Hit button for this hand
- -- Display separate Stand button for each hand
- mon.setCursorPos(8, 9 + (i - 1) * 7)
- mon.write("[STAND]") -- Stand button for this hand
- end
- -- Display dealer hand
- mon.setCursorPos(1, 10 + #playerHands * 7)
- mon.write("Dealer: ")
- for i, card in ipairs(dealerHand) do
- if i == 1 or not playerTurn then
- drawCard(i * 7, 11 + #playerHands * 7, card) -- Show the dealer's first card
- else
- mon.setCursorPos(i * 7, 11 + #playerHands * 7)
- mon.write(" ? ") -- Hide the second card
- end
- end
- -- Display action buttons below the dealer cards
- mon.setCursorPos(1, 17 + #playerHands * 7) -- Adjusted position for action buttons
- mon.write("[SPLIT]") -- Split button
- mon.setCursorPos(8, 17 + #playerHands * 7) -- Adjusted position for action buttons
- mon.write("[DOUBLE]") -- Double button
- -- Display result message if present
- if resultMessage then
- mon.setBackgroundColor(colors.white) -- Set background to white for result message
- mon.setTextColor(colors.black) -- Set text color to black
- mon.setCursorPos(1, 5 + (#playerHands * 7)) -- Position for win/lose messages
- mon.setTextScale(0.95) -- Set text scale for a slightly bigger size
- mon.write(resultMessage) -- Display the result message
- mon.setTextScale(0.5) -- Reset text scale back to normal
- mon.setBackgroundColor(colors.black) -- Reset background color after drawing
- mon.setTextColor(colors.white) -- Reset text color after drawing
- end
- end
- -- Function to handle the player's turn
- local function playerTurnAction(action)
- local currentHand = playerHands[currentHandIndex]
- if action == "hit" then
- dealCardToHand(currentHand)
- local handTotal = calculateHandValue(currentHand) -- Calculate the total after hitting
- -- Check if player busts
- if handTotal > 21 then
- playerTurn = false
- displayGame("Bust! You lose.")
- return true -- Indicate game end
- end
- elseif action == "stand" then
- playerTurn = false
- elseif action == "split" then
- if #currentHand == 2 and currentHand[1].rank == currentHand[2].rank then
- -- Split the hand
- local newHand = {table.remove(currentHand)} -- Move first card to new hand
- table.insert(playerHands, newHand) -- Add new hand to player's hands
- dealCardToHand(currentHand) -- Deal a new card to the original hand
- dealCardToHand(newHand) -- Deal a new card to the new hand
- displayGame() -- Update display
- return false -- Continue the game
- end
- elseif action == "double" then
- if #currentHand == 2 then
- dealCardToHand(currentHand) -- Deal one more card
- local handTotal = calculateHandValue(currentHand)
- -- Check if player busts
- if handTotal > 21 then
- playerTurn = false
- displayGame("Bust! You lose.")
- return true -- Indicate game end
- end
- playerTurn = false -- End player's turn
- end
- end
- displayGame() -- Refresh display after action
- return false -- Indicate game continues
- end
- -- Dealer's turn logic
- local function dealerTurn()
- while calculateHandValue(dealerHand) < 17 do
- dealCardToHand(dealerHand)
- displayGame()
- end
- -- Determine winner for each player hand
- for i, hand in ipairs(playerHands) do
- local playerTotal = calculateHandValue(hand)
- local dealerTotal = calculateHandValue(dealerHand)
- mon.setCursorPos(1, 17 + (#playerHands * 7) + (i - 1) * 3) -- Position for results of each hand
- -- Determine and display result for each hand
- if dealerTotal > 21 then
- mon.write("Dealer busts! Hand " .. i .. ": You win!")
- elseif playerTotal > 21 then
- mon.write("Hand " .. i .. ": Bust! You lose!")
- elseif playerBlackjack then
- mon.write("Hand " .. i .. ": Blackjack! You win!")
- elseif dealerBlackjack then
- mon.write("Hand " .. i .. ": Dealer has Blackjack! You lose!")
- elseif playerTotal > dealerTotal then
- mon.write("Hand " .. i .. ": You win!")
- elseif playerTotal < dealerTotal then
- mon.write("Hand " .. i .. ": You lose!")
- else
- mon.write("Hand " .. i .. ": Push! It's a tie!") -- Push scenario
- end
- end
- -- Delay before starting a new game
- sleep(3) -- Pause for 3 seconds to show the result
- return true -- Indicate game end
- end
- -- Main game function
- local function playBlackjack()
- initializeDeck()
- playerHands = {{}}
- dealerHand = {}
- playerTurn = true
- playerBlackjack, dealerBlackjack = false, false -- Reset blackjack flags
- -- Initial deal
- dealCardToHand(playerHands[1])
- dealCardToHand(dealerHand)
- dealCardToHand(playerHands[1])
- dealCardToHand(dealerHand)
- -- Check for initial blackjack
- if calculateHandValue(playerHands[1]) == 21 then
- playerBlackjack = true
- end
- displayGame()
- -- Event loop for touch handling
- while playerTurn do
- local event, side, x, y = os.pullEvent("monitor_touch")
- -- Check if Hit button for the current hand was touched
- if y == 9 + (currentHandIndex - 1) * 7 and x >= 1 and x <= 5 then
- if playerTurnAction("hit") then
- break -- End game after bust or blackjack
- end
- -- Check if Stand button for the current hand was touched
- elseif y == 9 + (currentHandIndex - 1) * 7 and x >= 8 and x <= 13 then
- playerTurnAction("stand")
- break -- End game after standing
- -- Check if Hit button for the second hand was touched
- elseif #playerHands > 1 and y == 9 + currentHandIndex * 7 and x >= 1 and x <= 5 then
- currentHandIndex = 2 -- Move to the second hand
- if playerTurnAction("hit") then
- break -- End game after bust or blackjack
- end
- -- Check if Stand button for the second hand was touched
- elseif #playerHands > 1 and y == 9 + currentHandIndex * 7 and x >= 8 and x <= 13 then
- playerTurnAction("stand")
- break -- End game after standing
- -- Check if "Split" button was touched
- elseif y == 17 + #playerHands * 7 and x >= 1 and x <= 5 then
- playerTurnAction("split")
- -- Check if "Double" button was touched
- elseif y == 17 + #playerHands * 7 and x >= 8 and x <= 13 then
- playerTurnAction("double")
- end
- end
- -- Dealer's turn
- if dealerTurn() then
- -- Automatically start a new game after dealer turn
- playBlackjack() -- Restart the game
- end
- end
- -- Start the game
- playBlackjack()
Add Comment
Please, Sign In to add comment