sxrgini

CC: Blackjack (No Betting)

Nov 3rd, 2024 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.85 KB | None | 0 0
  1. -- Blackjack Game For ComputerCraft --
  2. -- Created by iiAmPanda --
  3. -- pastebin get 2Z9Y9XvU blackjack --
  4.  
  5. -- Attach the monitor
  6. local mon = peripheral.wrap("right") -- Using the correct monitor name
  7. mon.setTextScale(0.5) -- Adjust text size to fit on the screen
  8.  
  9. -- Initialize variables
  10. local deck, playerHands, dealerHand = {}, {}, {}
  11. local playerTurn = true -- Track if it's the player's turn
  12. local playerBlackjack = false -- Track if player has blackjack
  13. local dealerBlackjack = false -- Track if dealer has blackjack
  14. local currentHandIndex = 1 -- Track which hand is currently being played
  15.  
  16. -- Function to create and shuffle the deck
  17. local function initializeDeck()
  18. deck = {}
  19. local suits = {"H", "D", "C", "S"} -- Suit identifiers
  20. local ranks = {
  21. {name = "2", value = 2}, {name = "3", value = 3}, {name = "4", value = 4},
  22. {name = "5", value = 5}, {name = "6", value = 6}, {name = "7", value = 7},
  23. {name = "8", value = 8}, {name = "9", value = 9}, {name = "10", value = 10},
  24. {name = "J", value = 10}, {name = "Q", value = 10}, {name = "K", value = 10},
  25. {name = "A", value = 11}
  26. }
  27. for _, suit in ipairs(suits) do
  28. for _, rank in ipairs(ranks) do
  29. table.insert(deck, {suit = suit, rank = rank.name, value = rank.value})
  30. end
  31. end
  32.  
  33. -- Shuffle the deck
  34. for i = #deck, 2, -1 do
  35. local j = math.random(i)
  36. deck[i], deck[j] = deck[j], deck[i]
  37. end
  38. end
  39.  
  40. -- Function to deal a card to a specific hand
  41. local function dealCardToHand(hand)
  42. local card = table.remove(deck)
  43. table.insert(hand, card)
  44. end
  45.  
  46. -- Function to calculate hand value (with Ace adjustment)
  47. local function calculateHandValue(hand)
  48. local value = 0
  49. local numAces = 0
  50. for _, card in ipairs(hand) do
  51. value = value + card.value
  52. if card.rank == "A" then
  53. numAces = numAces + 1
  54. end
  55. end
  56. -- Adjust for Aces if value > 21
  57. while value > 21 and numAces > 0 do
  58. value = value - 10
  59. numAces = numAces - 1
  60. end
  61. return value
  62. end
  63.  
  64. -- Function to draw a suit symbol on the monitor using ASCII codes
  65. local function drawSuit(x, y, suit)
  66. mon.setCursorPos(x, y)
  67. if suit == "H" then
  68. mon.write(string.char(3)) -- Heart (ASCII Code 3)
  69. elseif suit == "D" then
  70. mon.write(string.char(4)) -- Diamond (ASCII Code 4)
  71. elseif suit == "C" then
  72. mon.write(string.char(5)) -- Club (ASCII Code 5)
  73. elseif suit == "S" then
  74. mon.write(string.char(6)) -- Spade (ASCII Code 6)
  75. end
  76. end
  77.  
  78. -- Function to draw a card on the monitor
  79. local function drawCard(x, y, card)
  80. mon.setCursorPos(x, y)
  81. mon.write("+---+")
  82. mon.setCursorPos(x, y + 1)
  83. mon.write("| " .. card.rank .. " |") -- Center rank within card
  84. mon.setCursorPos(x, y + 2)
  85. drawSuit(x + 1, y + 2, card.suit) -- Draw suit symbol
  86. mon.setCursorPos(x, y + 3)
  87. mon.write("| |") -- Reduced space for bottom
  88. mon.setCursorPos(x, y + 4)
  89. mon.write("+---+")
  90. end
  91.  
  92. -- Function to display hands and buttons on the monitor
  93. local function displayGame(resultMessage)
  94. mon.setBackgroundColor(colors.black) -- Set background color to black
  95. mon.setTextColor(colors.white) -- Set text color to white
  96. mon.clear() -- Clear the monitor
  97.  
  98. -- Display player hands
  99. for i, hand in ipairs(playerHands) do
  100. mon.setCursorPos(1, 1 + (i - 1) * 7)
  101. mon.write("Player Hand " .. i .. ": ")
  102. for j, card in ipairs(hand) do
  103. drawCard(j * 7, 2 + (i - 1) * 7, card) -- Spacing for skinnier cards
  104. end
  105. local handTotal = calculateHandValue(hand)
  106. mon.setCursorPos(1, 8 + (i - 1) * 7)
  107. mon.write("P Total: " .. handTotal)
  108.  
  109. -- Display separate Hit button for each hand
  110. mon.setCursorPos(1, 9 + (i - 1) * 7)
  111. mon.write("[HIT]") -- Hit button for this hand
  112. -- Display separate Stand button for each hand
  113. mon.setCursorPos(8, 9 + (i - 1) * 7)
  114. mon.write("[STAND]") -- Stand button for this hand
  115. end
  116.  
  117. -- Display dealer hand
  118. mon.setCursorPos(1, 10 + #playerHands * 7)
  119. mon.write("Dealer: ")
  120. for i, card in ipairs(dealerHand) do
  121. if i == 1 or not playerTurn then
  122. drawCard(i * 7, 11 + #playerHands * 7, card) -- Show the dealer's first card
  123. else
  124. mon.setCursorPos(i * 7, 11 + #playerHands * 7)
  125. mon.write(" ? ") -- Hide the second card
  126. end
  127. end
  128.  
  129. -- Display action buttons below the dealer cards
  130. mon.setCursorPos(1, 17 + #playerHands * 7) -- Adjusted position for action buttons
  131. mon.write("[SPLIT]") -- Split button
  132. mon.setCursorPos(8, 17 + #playerHands * 7) -- Adjusted position for action buttons
  133. mon.write("[DOUBLE]") -- Double button
  134.  
  135. -- Display result message if present
  136. if resultMessage then
  137. mon.setBackgroundColor(colors.white) -- Set background to white for result message
  138. mon.setTextColor(colors.black) -- Set text color to black
  139. mon.setCursorPos(1, 5 + (#playerHands * 7)) -- Position for win/lose messages
  140. mon.setTextScale(0.95) -- Set text scale for a slightly bigger size
  141. mon.write(resultMessage) -- Display the result message
  142. mon.setTextScale(0.5) -- Reset text scale back to normal
  143. mon.setBackgroundColor(colors.black) -- Reset background color after drawing
  144. mon.setTextColor(colors.white) -- Reset text color after drawing
  145. end
  146. end
  147.  
  148. -- Function to handle the player's turn
  149. local function playerTurnAction(action)
  150. local currentHand = playerHands[currentHandIndex]
  151. if action == "hit" then
  152. dealCardToHand(currentHand)
  153. local handTotal = calculateHandValue(currentHand) -- Calculate the total after hitting
  154.  
  155. -- Check if player busts
  156. if handTotal > 21 then
  157. playerTurn = false
  158. displayGame("Bust! You lose.")
  159. return true -- Indicate game end
  160. end
  161. elseif action == "stand" then
  162. playerTurn = false
  163. elseif action == "split" then
  164. if #currentHand == 2 and currentHand[1].rank == currentHand[2].rank then
  165. -- Split the hand
  166. local newHand = {table.remove(currentHand)} -- Move first card to new hand
  167. table.insert(playerHands, newHand) -- Add new hand to player's hands
  168. dealCardToHand(currentHand) -- Deal a new card to the original hand
  169. dealCardToHand(newHand) -- Deal a new card to the new hand
  170. displayGame() -- Update display
  171. return false -- Continue the game
  172. end
  173. elseif action == "double" then
  174. if #currentHand == 2 then
  175. dealCardToHand(currentHand) -- Deal one more card
  176. local handTotal = calculateHandValue(currentHand)
  177.  
  178. -- Check if player busts
  179. if handTotal > 21 then
  180. playerTurn = false
  181. displayGame("Bust! You lose.")
  182. return true -- Indicate game end
  183. end
  184. playerTurn = false -- End player's turn
  185. end
  186. end
  187. displayGame() -- Refresh display after action
  188. return false -- Indicate game continues
  189. end
  190.  
  191. -- Dealer's turn logic
  192. local function dealerTurn()
  193. while calculateHandValue(dealerHand) < 17 do
  194. dealCardToHand(dealerHand)
  195. displayGame()
  196. end
  197.  
  198. -- Determine winner for each player hand
  199. for i, hand in ipairs(playerHands) do
  200. local playerTotal = calculateHandValue(hand)
  201. local dealerTotal = calculateHandValue(dealerHand)
  202. mon.setCursorPos(1, 17 + (#playerHands * 7) + (i - 1) * 3) -- Position for results of each hand
  203.  
  204. -- Determine and display result for each hand
  205. if dealerTotal > 21 then
  206. mon.write("Dealer busts! Hand " .. i .. ": You win!")
  207. elseif playerTotal > 21 then
  208. mon.write("Hand " .. i .. ": Bust! You lose!")
  209. elseif playerBlackjack then
  210. mon.write("Hand " .. i .. ": Blackjack! You win!")
  211. elseif dealerBlackjack then
  212. mon.write("Hand " .. i .. ": Dealer has Blackjack! You lose!")
  213. elseif playerTotal > dealerTotal then
  214. mon.write("Hand " .. i .. ": You win!")
  215. elseif playerTotal < dealerTotal then
  216. mon.write("Hand " .. i .. ": You lose!")
  217. else
  218. mon.write("Hand " .. i .. ": Push! It's a tie!") -- Push scenario
  219. end
  220. end
  221.  
  222. -- Delay before starting a new game
  223. sleep(3) -- Pause for 3 seconds to show the result
  224. return true -- Indicate game end
  225. end
  226.  
  227. -- Main game function
  228. local function playBlackjack()
  229. initializeDeck()
  230. playerHands = {{}}
  231. dealerHand = {}
  232. playerTurn = true
  233. playerBlackjack, dealerBlackjack = false, false -- Reset blackjack flags
  234.  
  235. -- Initial deal
  236. dealCardToHand(playerHands[1])
  237. dealCardToHand(dealerHand)
  238. dealCardToHand(playerHands[1])
  239. dealCardToHand(dealerHand)
  240.  
  241. -- Check for initial blackjack
  242. if calculateHandValue(playerHands[1]) == 21 then
  243. playerBlackjack = true
  244. end
  245.  
  246. displayGame()
  247.  
  248. -- Event loop for touch handling
  249. while playerTurn do
  250. local event, side, x, y = os.pullEvent("monitor_touch")
  251.  
  252. -- Check if Hit button for the current hand was touched
  253. if y == 9 + (currentHandIndex - 1) * 7 and x >= 1 and x <= 5 then
  254. if playerTurnAction("hit") then
  255. break -- End game after bust or blackjack
  256. end
  257. -- Check if Stand button for the current hand was touched
  258. elseif y == 9 + (currentHandIndex - 1) * 7 and x >= 8 and x <= 13 then
  259. playerTurnAction("stand")
  260. break -- End game after standing
  261. -- Check if Hit button for the second hand was touched
  262. elseif #playerHands > 1 and y == 9 + currentHandIndex * 7 and x >= 1 and x <= 5 then
  263. currentHandIndex = 2 -- Move to the second hand
  264. if playerTurnAction("hit") then
  265. break -- End game after bust or blackjack
  266. end
  267. -- Check if Stand button for the second hand was touched
  268. elseif #playerHands > 1 and y == 9 + currentHandIndex * 7 and x >= 8 and x <= 13 then
  269. playerTurnAction("stand")
  270. break -- End game after standing
  271. -- Check if "Split" button was touched
  272. elseif y == 17 + #playerHands * 7 and x >= 1 and x <= 5 then
  273. playerTurnAction("split")
  274. -- Check if "Double" button was touched
  275. elseif y == 17 + #playerHands * 7 and x >= 8 and x <= 13 then
  276. playerTurnAction("double")
  277. end
  278. end
  279.  
  280. -- Dealer's turn
  281. if dealerTurn() then
  282. -- Automatically start a new game after dealer turn
  283. playBlackjack() -- Restart the game
  284. end
  285. end
  286.  
  287. -- Start the game
  288. playBlackjack()
  289.  
Add Comment
Please, Sign In to add comment