sxrgini

CC: Blackjack (WIP)

Nov 3rd, 2024 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.38 KB | None | 0 0
  1. -- Blackjack Game For ComputerCraft --
  2. -- Created by iiAmPanda --
  3. -- pastebin get b7NvUZxu blackjack(wip) --
  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, playerHand, dealerHand = {}, {}, {}
  11. local playerTurn = true -- Track if it's the player's turn
  12. local credits = 0
  13. local bet = 0
  14.  
  15. -- Function to check for a Casino Card
  16. function checkForCasinoCard()
  17. return fs.exists("disk/creds.lua")
  18. end
  19.  
  20. -- Function to create and shuffle the deck
  21. local function initializeDeck()
  22. deck = {}
  23. local suits = {"H", "D", "C", "S"} -- Suit identifiers
  24. local ranks = {
  25. {name = "2", value = 2}, {name = "3", value = 3}, {name = "4", value = 4},
  26. {name = "5", value = 5}, {name = "6", value = 6}, {name = "7", value = 7},
  27. {name = "8", value = 8}, {name = "9", value = 9}, {name = "10", value = 10},
  28. {name = "J", value = 10}, {name = "Q", value = 10}, {name = "K", value = 10},
  29. {name = "A", value = 11}
  30. }
  31. for _, suit in ipairs(suits) do
  32. for _, rank in ipairs(ranks) do
  33. table.insert(deck, {suit = suit, rank = rank.name, value = rank.value})
  34. end
  35. end
  36.  
  37. -- Shuffle the deck
  38. for i = #deck, 2, -1 do
  39. local j = math.random(i)
  40. deck[i], deck[j] = deck[j], deck[i]
  41. end
  42. end
  43.  
  44. -- Function to deal a card
  45. local function dealCard(hand)
  46. local card = table.remove(deck)
  47. table.insert(hand, card)
  48. end
  49.  
  50. -- Function to calculate hand value (with Ace adjustment)
  51. local function calculateHandValue(hand)
  52. local value = 0
  53. local numAces = 0
  54. for _, card in ipairs(hand) do
  55. value = value + card.value
  56. if card.rank == "A" then
  57. numAces = numAces + 1
  58. end
  59. end
  60. -- Adjust for Aces if value > 21
  61. while value > 21 and numAces > 0 do
  62. value = value - 10
  63. numAces = numAces - 1
  64. end
  65. return value
  66. end
  67.  
  68. -- Function to draw a suit symbol on the monitor using ASCII codes
  69. local function drawSuit(x, y, suit)
  70. mon.setCursorPos(x, y)
  71. if suit == "H" then
  72. mon.write(string.char(3)) -- Heart (ASCII Code 3)
  73. elseif suit == "D" then
  74. mon.write(string.char(4)) -- Diamond (ASCII Code 4)
  75. elseif suit == "C" then
  76. mon.write(string.char(5)) -- Club (ASCII Code 5)
  77. elseif suit == "S" then
  78. mon.write(string.char(6)) -- Spade (ASCII Code 6)
  79. end
  80. end
  81.  
  82. -- Function to draw a card on the monitor
  83. local function drawCard(x, y, card)
  84. mon.setCursorPos(x, y)
  85. mon.write("+---+")
  86. mon.setCursorPos(x, y + 1)
  87. mon.write("| " .. card.rank .. " |") -- Center rank within card
  88. mon.setCursorPos(x, y + 2)
  89. drawSuit(x + 1, y + 2, card.suit) -- Draw suit symbol
  90. mon.setCursorPos(x, y + 3)
  91. mon.write("| |") -- Reduced space for bottom
  92. mon.setCursorPos(x, y + 4)
  93. mon.write("+---+")
  94. end
  95.  
  96. -- Function to display hands and buttons on the monitor
  97. local function displayGame()
  98. mon.setBackgroundColor(colors.black) -- Set background color to black
  99. mon.setTextColor(colors.white) -- Set text color to white
  100. mon.clear() -- Clear the monitor
  101.  
  102. -- Display player hand
  103. mon.setCursorPos(1, 1)
  104. mon.write("Player: ")
  105. for i, card in ipairs(playerHand) do
  106. drawCard(i * 7, 2, card) -- Spacing for skinnier cards
  107. end
  108. local playerTotal = calculateHandValue(playerHand)
  109. mon.setCursorPos(1, 8)
  110. mon.write("P Total: " .. playerTotal)
  111.  
  112. -- Display dealer hand
  113. mon.setCursorPos(1, 9)
  114. mon.write("Dealer: ")
  115. for i, card in ipairs(dealerHand) do
  116. if i == 1 or not playerTurn then
  117. drawCard(i * 7, 10, card) -- Show the dealer's first card
  118. else
  119. mon.setCursorPos(i * 7, 10)
  120. mon.write(" ? ") -- Hide the second card
  121. end
  122. end
  123.  
  124. -- Display action buttons below the dealer cards
  125. mon.setCursorPos(1, 16) -- Moved down to line 16
  126. mon.write("[H] Hit") -- Hit button
  127. mon.setCursorPos(15, 16) -- Spaced out
  128. mon.write("[S] Stand") -- Stand button
  129. end
  130.  
  131. -- Function to display betting screen
  132. local function displayBettingScreen()
  133. mon.setBackgroundColor(colors.black) -- Set background color to green
  134. mon.setTextColor(colors.white) -- Set text color to black
  135. mon.clear()
  136.  
  137. -- Draw a border
  138. mon.setCursorPos(1, 1)
  139. mon.write("+----------------------+")
  140. mon.setCursorPos(1, 2)
  141. mon.write("| Place Your Bet |")
  142. mon.setCursorPos(1, 3)
  143. mon.write("| Max Bet: " .. credits .. " |") -- Show max bet dynamically
  144. mon.setCursorPos(1, 4)
  145. mon.write("+----------------------+")
  146.  
  147. mon.setCursorPos(2, 6)
  148. mon.write("Current Bet: " .. bet) -- Display current bet
  149.  
  150. -- Draw buttons for adjusting the bet
  151. mon.setCursorPos(5, 8)
  152. mon.write("[+]") -- Plus button
  153. mon.setCursorPos(12, 8)
  154. mon.write("[-]") -- Minus button
  155.  
  156. mon.setCursorPos(1, 10)
  157. mon.write("+----------------------+")
  158. mon.setCursorPos(1, 11)
  159. mon.write("| [C] Confirm Bet |") -- Confirmation button
  160. mon.setCursorPos(1, 12)
  161. mon.write("+----------------------+")
  162.  
  163. while true do
  164. local event, button, x, y = os.pullEvent("mouse_click")
  165. if y == 8 and x >= 5 and x <= 7 then -- Plus button clicked
  166. if bet < credits then
  167. bet = bet + 1
  168. end
  169. elseif y == 8 and x >= 12 and x <= 14 then -- Minus button clicked
  170. if bet > 0 then
  171. bet = bet - 1
  172. end
  173. elseif y == 11 and x >= 1 and x <= 19 then -- Confirm button clicked
  174. if bet > 0 then
  175. break -- Exit betting screen
  176. end
  177. end
  178.  
  179. -- Update the displayed bet amount
  180. mon.setCursorPos(2, 6)
  181. mon.write("Current Bet: " .. bet) -- Refresh the displayed bet
  182. sleep(0.05) -- Yield slightly to prevent long execution
  183. end
  184. end
  185.  
  186. -- Function to read credits from the Casino Card
  187. function readCard()
  188. if not fs.exists("disk/creds.lua") then
  189. return false
  190. end
  191.  
  192. local Card = fs.open("disk/creds.lua", "r")
  193. if not Card then
  194. displayResults("Error reading from disk!") -- Better error display
  195. return false
  196. end
  197.  
  198. local data = Card.readAll()
  199. Card.close()
  200.  
  201. local a, b = string.find(data, "11066011")
  202. local c, d = string.find(data, "11077011")
  203. credits = tonumber(string.sub(data, b + 1, c - 1))
  204. return true
  205. end
  206.  
  207. -- Function to display results
  208. local function displayResults(message)
  209. mon.setBackgroundColor(colors.black) -- Change background for results
  210. mon.setTextColor(colors.white) -- Change text color for visibility
  211. mon.clear()
  212. mon.setCursorPos(1, 1)
  213. mon.write(message)
  214. sleep(3) -- Pause before clearing the screen
  215. end
  216.  
  217. -- Function to handle player's turn
  218. local function playerTurnAction(action)
  219. if action == "hit" then
  220. dealCard(playerHand)
  221. local playerTotal = calculateHandValue(playerHand)
  222. displayGame() -- Update the display after hitting
  223. if playerTotal > 21 then
  224. playerTurn = false -- End player's turn if bust
  225. displayResults("BUST! Dealer Wins!")
  226. credits = credits - bet -- Deduct bet on loss
  227. end
  228. elseif action == "stand" then
  229. playerTurn = false -- End player's turn
  230. end
  231. end
  232.  
  233. -- Function to handle dealer's turn
  234. local function dealerTurn()
  235. while calculateHandValue(dealerHand) < 17 do
  236. dealCard(dealerHand)
  237. sleep(0.05) -- Yield slightly to prevent long execution
  238. end
  239. end
  240.  
  241. -- Main game function
  242. local function playBlackjack()
  243. if not checkForCasinoCard() then
  244. displayResults("Insert Casino Card to Play!")
  245. return
  246. end
  247.  
  248. if not readCard() then
  249. return
  250. end
  251.  
  252. displayBettingScreen() -- Show betting screen
  253.  
  254. -- Game starts here
  255. initializeDeck() -- Prepare a new deck
  256. playerHand, dealerHand = {}, {}
  257. dealCard(playerHand)
  258. dealCard(dealerHand)
  259. dealCard(playerHand)
  260. dealCard(dealerHand)
  261.  
  262. -- Player's turn
  263. while playerTurn do
  264. displayGame() -- Update display with hands
  265. local event, key = os.pullEvent("char")
  266. if key == "h" then
  267. playerTurnAction("hit")
  268. elseif key == "s" then
  269. playerTurnAction("stand")
  270. end
  271. end
  272.  
  273. -- Dealer's turn
  274. dealerTurn()
  275.  
  276. -- Determine winner
  277. local playerTotal = calculateHandValue(playerHand)
  278. local dealerTotal = calculateHandValue(dealerHand)
  279.  
  280. if playerTotal > 21 then
  281. displayResults("BUST! Dealer Wins!")
  282. credits = credits - bet -- Deduct bet on loss
  283. elseif dealerTotal > 21 then
  284. displayResults("Dealer BUSTS! You Win!")
  285. credits = credits + bet -- Add bet to credits on win
  286. elseif playerTotal > dealerTotal then
  287. displayResults("You Win!")
  288. credits = credits + bet -- Add bet to credits on win
  289. elseif playerTotal < dealerTotal then
  290. displayResults("Dealer Wins!")
  291. credits = credits - bet -- Deduct bet on loss
  292. else
  293. displayResults("It's a Tie!")
  294. end
  295. sleep(3)
  296. end
  297.  
  298. -- Start the game loop
  299. while true do
  300. playBlackjack() -- Play the game
  301. end
  302.  
Add Comment
Please, Sign In to add comment