oGoOgO

game_Blackjack.lua

Jun 3rd, 2024 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.13 KB | None | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local gpu = component.gpu
  4. local event = require("event")
  5. local casino = require("casino")
  6.  
  7. local login, blackjack, player, value, players_cards, dialer_cards, time_sleep, time_sleep_end = false, false, 'p', 1, {}, {}, 0.2, 3
  8.  
  9. local consoleLines = {}
  10. for i = 1, 13 do
  11. consoleLines[i] = ""
  12. end
  13.  
  14. local function drawRightMenu()
  15. gpu.setBackground(login and 0x613C3C or 0x990000)
  16. gpu.setForeground(0xFFFFFF)
  17. gpu.fill(41, 17, 28, 3, " ")
  18. gpu.set(52, 18, "Выход")
  19.  
  20. gpu.setBackground(0x000000)
  21. gpu.setForeground(0xAAAAAA)
  22. gpu.fill(41, 2, 28, 14, " ")
  23. gpu.set(42, 2, "Вывод:")
  24. for i = 1, #consoleLines do
  25. gpu.setForeground((15 - #consoleLines + i) * 0x111111)
  26. gpu.set(42, 16 - i, consoleLines[i])
  27. end
  28. end
  29.  
  30. local function message(msg)
  31. table.remove(consoleLines, 1)
  32. table.insert(consoleLines, msg)
  33. drawRightMenu()
  34. end
  35.  
  36. local Deck = {}
  37.  
  38. function Deck:new()
  39. local obj = {}
  40. obj.cards = { { card = "2", suit = "♥" }, { card = "2", suit = "♦" }, { card = "2", suit = "♣" },
  41. { card = "2", suit = "♠" }, { card = "3", suit = "♥" }, { card = "3", suit = "♦" },
  42. { card = "3", suit = "♣" }, { card = "3", suit = "♠" }, { card = "4", suit = "♥" },
  43. { card = "4", suit = "♦" }, { card = "4", suit = "♣" }, { card = "4", suit = "♠" },
  44. { card = "5", suit = "♥" }, { card = "5", suit = "♦" }, { card = "5", suit = "♣" },
  45. { card = "5", suit = "♠" }, { card = "6", suit = "♥" }, { card = "6", suit = "♦" },
  46. { card = "6", suit = "♣" }, { card = "6", suit = "♠" }, { card = "7", suit = "♥" },
  47. { card = "7", suit = "♦" }, { card = "7", suit = "♣" }, { card = "7", suit = "♠" },
  48. { card = "8", suit = "♥" }, { card = "8", suit = "♦" }, { card = "8", suit = "♣" },
  49. { card = "8", suit = "♠" }, { card = "9", suit = "♥" }, { card = "9", suit = "♦" },
  50. { card = "9", suit = "♣" }, { card = "9", suit = "♠" }, { card = "10", suit = "♥" },
  51. { card = "10", suit = "♦" }, { card = "10", suit = "♣" }, { card = "10", suit = "♠" },
  52. { card = "J", suit = "♥" }, { card = "J", suit = "♦" }, { card = "J", suit = "♣" },
  53. { card = "J", suit = "♠" }, { card = "Q", suit = "♥" }, { card = "Q", suit = "♦" },
  54. { card = "Q", suit = "♣" }, { card = "Q", suit = "♠" }, { card = "K", suit = "♥" },
  55. { card = "K", suit = "♦" }, { card = "K", suit = "♣" }, { card = "K", suit = "♠" },
  56. { card = "T", suit = "♥" }, { card = "T", suit = "♦" }, { card = "T", suit = "♣" },
  57. { card = "T", suit = "♠" } }
  58. obj.index = 1
  59. obj.pod = true
  60.  
  61. function obj:get()
  62. local temp = self.cards[self.index]
  63. self.index = self.index + 1
  64. return temp
  65. end
  66.  
  67. function obj:hinder()
  68. for first = 1, 52 do
  69. local second, firstCard = math.random(1, 52), self.cards[first]
  70. self.cards[first] = self.cards[second]
  71. self.cards[second] = firstCard
  72. end
  73. self.index = 1
  74. end
  75.  
  76. setmetatable(obj, self)
  77. self.__index = self;
  78. return obj
  79. end
  80.  
  81. local deck = Deck:new()
  82.  
  83. local function drawDisplayForOneHand()
  84. gpu.setBackground(0x006400)
  85. term.clear()
  86. gpu.setBackground(0x00aa00)
  87. gpu.fill(3, 2, 36, 18, " ")
  88.  
  89. gpu.setBackground(0x229922)
  90. for i = 0, 4 do
  91. gpu.fill(9 + i * 5, 6, 4, 4, ' ')
  92. end
  93. for i = 0, 4 do
  94. gpu.fill(9 + i * 5, 15, 4, 4, ' ')
  95. end
  96.  
  97. gpu.setBackground(0x20B2AA)
  98. gpu.fill(9, 11, 11, 1, ' ')
  99. gpu.set(13, 11, 'Ещё')
  100. gpu.fill(22, 11, 11, 1, ' ')
  101. gpu.set(25, 11, 'Хватит')
  102.  
  103. gpu.fill(9, 13, 11, 1, ' ')
  104. gpu.set(11, 13, 'Удвоить')
  105.  
  106. gpu.setBackground(0x00aa00)
  107. gpu.setForeground(0xffffff)
  108. gpu.set(13, 4, "Ставка: " .. value)
  109. end
  110.  
  111. local function countCards(temp_cards, boolean)
  112. local Tcount = 0
  113. local count = 0
  114. for i = 1, #temp_cards do
  115. if temp_cards[i].card == '2' then
  116. count = count + 2
  117. elseif temp_cards[i].card == '3' then
  118. count = count + 3
  119. elseif temp_cards[i].card == '4' then
  120. count = count + 4
  121. elseif temp_cards[i].card == '5' then
  122. count = count + 5
  123. elseif temp_cards[i].card == '6' then
  124. count = count + 6
  125. elseif temp_cards[i].card == '7' then
  126. count = count + 7
  127. elseif temp_cards[i].card == '8' then
  128. count = count + 8
  129. elseif temp_cards[i].card == '9' then
  130. count = count + 9
  131. elseif temp_cards[i].card == '10' then
  132. count = count + 10
  133. elseif temp_cards[i].card == 'J' then
  134. count = count + 10
  135. elseif temp_cards[i].card == 'Q' then
  136. count = count + 10
  137. elseif temp_cards[i].card == 'K' then
  138. count = count + 10
  139. elseif temp_cards[i].card == 'T' then
  140. Tcount = Tcount + 1
  141. end
  142. end
  143.  
  144. if (boolean) then
  145. if Tcount > 0 then
  146. if count + Tcount * 11 > 21 then
  147. count = count + Tcount
  148. else
  149. count = count + Tcount * 11
  150. end
  151. end
  152. return count
  153. else
  154. local temp_count = count
  155. if Tcount > 0 then
  156. if (count + Tcount * 11 <= 21) then
  157. temp_count = temp_count + Tcount
  158. temp_count = temp_count .. "/"
  159. count = count + Tcount * 11
  160. temp_count = temp_count .. count
  161. return temp_count
  162. else
  163. temp_count = temp_count + Tcount
  164. temp_count = temp_count .. ""
  165. return temp_count
  166. end
  167. end
  168. return temp_count .. ''
  169. end
  170. end
  171.  
  172. --setDefaultColor(22,5,10)
  173. local function setDefaultColor(left, top, bet)
  174. gpu.setForeground(0xffffff)
  175. gpu.setBackground(0x888888)
  176. gpu.set(20, 5, '1')
  177. gpu.set(20, 7, '5')
  178. gpu.set(22, 5, '10')
  179. gpu.set(22, 7, '25')
  180. gpu.set(25, 5, '50')
  181. gpu.set(25, 7, '75')
  182. gpu.set(28, 5, '100')
  183. gpu.set(28, 7, '250')
  184. gpu.setBackground(0x00aa00)
  185. gpu.set(left, top, tostring(bet))
  186. return bet
  187. end
  188.  
  189. local function drawDisplay()
  190. casino.gameIsOver()
  191. gpu.setBackground(0xe0e0e0)
  192. term.clear()
  193. drawRightMenu()
  194. gpu.setBackground(0x00aa00)
  195. gpu.fill(3, 2, 14, 7, ' ')
  196. gpu.setBackground(0xffffff)
  197. gpu.setForeground(0xaa0000)
  198.  
  199. gpu.fill(5, 3, 4, 4, ' ')
  200. gpu.set(5, 3, 'J')
  201. gpu.set(6, 4, '♥')
  202. gpu.set(7, 5, '♥')
  203. gpu.set(8, 6, 'J')
  204.  
  205. gpu.setForeground(0x000000)
  206. gpu.fill(11, 4, 4, 4, ' ')
  207. gpu.set(11, 4, 'T')
  208. gpu.set(12, 5, '♠')
  209. gpu.set(13, 6, '♠')
  210. gpu.set(14, 7, 'T')
  211.  
  212. gpu.fill(3, 10, 36, 10, ' ')
  213. gpu.fill(19, 2, 20, 7, ' ')
  214. gpu.setForeground(0xffffff)
  215. setDefaultColor(20, 5, 1)
  216.  
  217. gpu.setBackground(0x00aa00)
  218. gpu.fill(32, 5, 6, 3, ' ')
  219. gpu.set(20, 5, '1')
  220. value = 1
  221. gpu.set(32, 6, 'Начать')
  222. gpu.setForeground(0x000000)
  223. gpu.setBackground(0xffffff)
  224.  
  225. gpu.set(5, 11, 'Blackjack')
  226. gpu.set(5, 13, 'Правила:')
  227. gpu.set(5, 14, '1. Нужно набрать больше очков,')
  228. gpu.set(5, 15, 'чем у дилера.')
  229. gpu.set(5, 16, '2. Нельзя набирать больше 21 очка.')
  230. gpu.set(5, 17, '3. Победа удваивает ставку.')
  231. gpu.set(5, 18, '4. Ничья возвращает ставку.')
  232. gpu.set(5, 19, 'Более подробно есть в интернете.')
  233.  
  234. gpu.set(21, 3, 'Выберите ставку')
  235. end
  236.  
  237. local function giveCardPlayer()
  238. gpu.setBackground(0xffffff)
  239. local card = deck:get()
  240. if card.suit == '♥' or card.suit == '♦' then
  241. gpu.setForeground(0xaa0000)
  242. else
  243. gpu.setForeground(0x000000)
  244. end
  245. if #players_cards == 0 then
  246. gpu.fill(9, 15, 4, 4, ' ')
  247. os.sleep(time_sleep)
  248. gpu.set(9, 15, card.card)
  249. gpu.set(10, 16, card.suit)
  250. gpu.set(11, 17, card.suit)
  251. if card.card == '10' then
  252. gpu.set(11, 18, card.card)
  253. else
  254. gpu.set(12, 18, card.card)
  255. end
  256. os.sleep(time_sleep)
  257. elseif #players_cards == 1 then
  258. gpu.fill(14, 15, 4, 4, ' ')
  259. os.sleep(time_sleep)
  260. gpu.set(14, 15, card.card)
  261. gpu.set(15, 16, card.suit)
  262. gpu.set(16, 17, card.suit)
  263. if card.card == '10' then
  264. gpu.set(16, 18, card.card)
  265. else
  266. gpu.set(17, 18, card.card)
  267. end
  268. elseif #players_cards == 2 then
  269. gpu.fill(19, 15, 4, 4, ' ')
  270. os.sleep(time_sleep)
  271. gpu.set(19, 15, card.card)
  272. gpu.set(20, 16, card.suit)
  273. gpu.set(21, 17, card.suit)
  274. if card.card == '10' then
  275. gpu.set(21, 18, card.card)
  276. else
  277. gpu.set(22, 18, card.card)
  278. end
  279. elseif #players_cards == 3 then
  280. gpu.fill(24, 15, 4, 4, ' ')
  281. os.sleep(time_sleep)
  282. gpu.set(24, 15, card.card)
  283. gpu.set(25, 16, card.suit)
  284. gpu.set(26, 17, card.suit)
  285. if card.card == '10' then
  286. gpu.set(26, 18, card.card)
  287. else
  288. gpu.set(27, 18, card.card)
  289. end
  290. elseif #players_cards == 4 then
  291. gpu.fill(29, 15, 4, 4, ' ')
  292. os.sleep(time_sleep)
  293. gpu.set(29, 15, card.card)
  294. gpu.set(30, 16, card.suit)
  295. gpu.set(31, 17, card.suit)
  296. if card.card == '10' then
  297. gpu.set(31, 18, card.card)
  298. else
  299. gpu.set(32, 18, card.card)
  300. end
  301. end
  302. players_cards[#players_cards + 1] = card
  303. gpu.setBackground(0x00aa00)
  304. gpu.setForeground(0xffffff)
  305. gpu.fill(19, 19, 10, 1, ' ')
  306. gpu.set(19, 19, countCards(players_cards, false))
  307. if countCards(players_cards, true) > 21 then
  308. message("Перебор, победа казино!")
  309. os.sleep(time_sleep_end)
  310. login = false
  311. drawDisplay()
  312. end
  313. end
  314.  
  315. local function giveCardDialer()
  316. gpu.setBackground(0xffffff)
  317. local card = deck:get()
  318. if card.suit == '♥' or card.suit == '♦' then
  319. gpu.setForeground(0xaa0000)
  320. else
  321. gpu.setForeground(0x000000)
  322. end
  323. if #dialer_cards == 0 then
  324. gpu.fill(9, 6, 4, 4, ' ')
  325. os.sleep(time_sleep)
  326. gpu.set(9, 6, card.card)
  327. gpu.set(10, 7, card.suit)
  328. gpu.set(11, 8, card.suit)
  329. if card.card == '10' then
  330. gpu.set(11, 9, card.card)
  331. else
  332. gpu.set(12, 9, card.card)
  333. end
  334. os.sleep(time_sleep)
  335. gpu.fill(14, 6, 4, 4, ' ')
  336. elseif #dialer_cards == 1 then
  337. os.sleep(time_sleep)
  338. gpu.set(14, 6, card.card)
  339. gpu.set(15, 7, card.suit)
  340. gpu.set(16, 8, card.suit)
  341. if card.card == '10' then
  342. gpu.set(16, 9, card.card)
  343. else
  344. gpu.set(17, 9, card.card)
  345. end
  346. os.sleep(time_sleep)
  347. elseif #dialer_cards == 2 then
  348. gpu.fill(19, 6, 4, 4, ' ')
  349. os.sleep(time_sleep)
  350. gpu.set(19, 6, card.card)
  351. gpu.set(20, 7, card.suit)
  352. gpu.set(21, 8, card.suit)
  353. if card.card == '10' then
  354. gpu.set(21, 9, card.card)
  355. else
  356. gpu.set(22, 9, card.card)
  357. end
  358. os.sleep(time_sleep)
  359. elseif #dialer_cards == 3 then
  360. gpu.fill(24, 6, 4, 4, ' ')
  361. os.sleep(time_sleep)
  362. gpu.set(24, 6, card.card)
  363. gpu.set(25, 7, card.suit)
  364. gpu.set(26, 8, card.suit)
  365. if card.card == '10' then
  366. gpu.set(26, 9, card.card)
  367. else
  368. gpu.set(27, 9, card.card)
  369. end
  370. os.sleep(time_sleep)
  371. elseif #dialer_cards == 4 then
  372. gpu.fill(29, 6, 4, 4, ' ')
  373. os.sleep(time_sleep)
  374. gpu.set(29, 6, card.card)
  375. gpu.set(30, 7, card.suit)
  376. gpu.set(31, 8, card.suit)
  377. if card.card == '10' then
  378. gpu.set(31, 9, card.card)
  379. else
  380. gpu.set(32, 9, card.card)
  381. end
  382. os.sleep(time_sleep)
  383. end
  384. dialer_cards[#dialer_cards + 1] = card
  385. gpu.setBackground(0x00aa00)
  386. gpu.setForeground(0xffffff)
  387. gpu.fill(19, 10, 10, 1, ' ')
  388. gpu.set(19, 10, countCards(dialer_cards, false))
  389. end
  390.  
  391. local function dialerStartPlay()
  392. giveCardDialer()
  393. if (countCards(dialer_cards, true) == 21) then
  394. message("Blackjack, Победа казино!")
  395. os.sleep(time_sleep_end)
  396. login = false
  397. drawDisplay()
  398. return
  399. end
  400. while countCards(dialer_cards, true) < 17 and #dialer_cards < 5 do
  401. giveCardDialer()
  402. end
  403. if countCards(dialer_cards, true) > 21 then
  404. message("Перебор, победа игрока!")
  405. casino.reward(value * 2)
  406. message("Вы выиграли " .. 2 * value .. "$")
  407. os.sleep(time_sleep_end)
  408. login = false
  409. drawDisplay()
  410. elseif countCards(players_cards, true) > countCards(dialer_cards, true) then
  411. message("Победа игрока!")
  412. casino.reward(value * 2)
  413. message("Вы выиграли " .. 2 * value .. "$")
  414. os.sleep(time_sleep_end)
  415. login = false
  416. drawDisplay()
  417. elseif countCards(players_cards, true) < countCards(dialer_cards, true) then
  418. message("Победа казино!")
  419. os.sleep(time_sleep_end)
  420. login = false
  421. drawDisplay()
  422. else
  423. message("Ничья!")
  424. casino.reward(value)
  425. message("Вы выиграли " .. value .. "$")
  426. os.sleep(time_sleep_end)
  427. login = false
  428. drawDisplay()
  429. end
  430. end
  431.  
  432. local function startGame()
  433. blackjack = false
  434. dialer_cards = {}
  435. players_cards = {}
  436. deck:hinder()
  437.  
  438. drawDisplayForOneHand()
  439. drawRightMenu()
  440. giveCardPlayer()
  441. giveCardPlayer()
  442. giveCardDialer()
  443. if (countCards(players_cards, true) == 21) then
  444. if (countCards(dialer_cards, true) == 11) then
  445. message("Blackjack!")
  446. blackjack = true
  447. gpu.setBackground(0x00aa00)
  448. gpu.fill(9, 11, 26, 3, " ")
  449. gpu.setBackground(0x20B2AA)
  450. gpu.setForeground(0xffffff)
  451. gpu.fill(9, 11, 11, 1, ' ')
  452. gpu.set(12, 11, 'Забрать')
  453. gpu.fill(22, 11, 11, 1, ' ')
  454. gpu.set(22, 11, 'Продолжить')
  455. else
  456. message("Blackjack!")
  457. message("Вы выиграли " .. value * 1.5)
  458. casino.reward(value * 1.5)
  459. os.sleep(time_sleep_end)
  460. login = false
  461. drawDisplay()
  462. end
  463. end
  464. end
  465.  
  466. local function reward(money, msg)
  467. message(msg)
  468. message("Вы выиграли " .. money)
  469. casino.reward(money)
  470. os.sleep(time_sleep_end)
  471. login = false
  472. blackjack = false
  473. drawDisplay()
  474. end
  475.  
  476. gpu.setResolution(70, 20)
  477. drawDisplay()
  478. while true do
  479. local e, _, x, y, _, p = event.pull(3, "touch")
  480. if e and login and p == player then
  481. if blackjack then
  482. if (x >= 9 and y == 11 and x <= 19) then
  483. reward(value, "Blackjack!")
  484. elseif (x >= 22 and y == 11 and x < 33) then
  485. giveCardDialer()
  486. if (countCards(dialer_cards, true) == 21) then
  487. message("Blackjack!, победа казино!")
  488. else
  489. reward(value * 2.5, "Blackjack!")
  490. end
  491. end
  492. elseif x >= 9 and y == 11 and x < 20 then
  493. giveCardPlayer()
  494. elseif x >= 22 and y == 11 and x < 33 then
  495. dialerStartPlay()
  496. elseif x >= 9 and y == 13 and x < 20 then
  497. if (#players_cards > 2) then
  498. message("Только с двумя картами!")
  499. else
  500. local payed, reason = casino.takeMoney(value)
  501. if payed then
  502. message("Начало игры за " .. value)
  503. gpu.setBackground(0x00aa00)
  504. value = value * 2
  505. gpu.setForeground(0xffffff)
  506. gpu.set(13, 4, "Ставка: " .. value)
  507. giveCardPlayer()
  508. if (login) then
  509. dialerStartPlay()
  510. end
  511. else
  512. message(reason)
  513. end
  514. end
  515. elseif x >= 41 and x <= 69 and y >= 17 and y <= 19 then
  516. message("Сначала закончите игру.")
  517. end
  518. elseif e and not login then
  519. if x == 20 and y == 5 then
  520. value = setDefaultColor(20, 5, 1)
  521. elseif x == 20 and y == 7 then
  522. value = setDefaultColor(20, 7, 5)
  523. elseif (x == 22 or x == 23) and y == 5 then
  524. value = setDefaultColor(22, 5, 10)
  525. elseif (x == 22 or x == 23) and y == 7 then
  526. value = setDefaultColor(22, 7, 25)
  527. elseif (x == 25 or x == 26) and y == 5 then
  528. value = setDefaultColor(25, 5, 50)
  529. elseif (x == 25 or x == 26) and y == 7 then
  530. value = setDefaultColor(25, 7, 75)
  531. elseif (x == 28 or x == 29 or x == 30) and y == 5 then
  532. value = setDefaultColor(28, 5, 100)
  533. elseif (x == 28 or x == 29 or x == 30) and y == 7 then
  534. value = setDefaultColor(28, 7, 250)
  535. elseif x >= 41 and x <= 69 and y >= 17 and y <= 19 then
  536. error("Exit by request")
  537. elseif (x >= 32 and x <= 37 and y >= 5 and y <= 7) then
  538. local payed, reason = casino.takeMoney(value)
  539. if payed then
  540. player = p
  541. login = true
  542. startGame()
  543. else
  544. message(reason)
  545. end
  546. end
  547. end
  548. end
Add Comment
Please, Sign In to add comment