ravneravn

blackjack terminal

May 9th, 2013
86
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Blackjack
  2.  
  3. disk = peripheral.wrap("bottom")
  4. cardName = {"Ace of Spades", "Ace Of Clubs", "Ace Of Hearts", "Ace Of Diamonds", "2 of Spades", "2 of Clubs", "2 of Hearts", "2 of Diamonds", "3 of Spades", "3 of Clubs", "3 of Hearts", "3 of Diamonds", "4 of Spades", "4 of Clubs", "4 of Hearts", "4 of Diamonds", "5 of Spades", "5 of Clubs", "5 of Hearts", "5 of Diamonds", "6 of Spades", "6 of Clubs", "6 of Hearts", "6 of Diamonds", "7 of Spades", "7 of Clubs", "7 of Hearts", "7 of Diamonds", "8 of Spades", "8 of Clubs", "8 of Hearts", "8 of Diamonds", "9 of Spades", "9 of Clubs", "9 of Hearts", "9 of Diamonds", "10 of Clubs", "10 of Spades", "10 of Hearts", "10 of Diamonds", "Jack of Clubs", "Jack of Spades", "Jack of Hearts", "Jack of Diamonds", "Queen of Clubs", "Queen of Spades", "Queen of Hearts", "Queen of Diamonds", "King of Clubs", "King of Spades", "King of Hearts", "King of Diamonds"}
  5. cardValue = {11, 11, 11, 11, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}
  6. playerCards = {}
  7. dealerVisibleCards = {}
  8. dealerHiddenCards = {}
  9. restricted = {}
  10. minCards = 1
  11. maxCards = 52
  12. stayPlayer = false
  13. stayDealer = false
  14. stay = false
  15. doubleDown = 0
  16. playerValue = 0
  17. dealerValue = 0
  18. y = 0
  19. pot = 0
  20. deleteTable = table
  21.  
  22.  
  23. function activate()
  24. os.pullEvent("disk")
  25. end
  26.  
  27. function waitClick()
  28. event, side, x, y = os.pullEvent("mouse_click")
  29. end
  30.  
  31. function mWrite(message, x, y)
  32. term.setCursorPos(x,y)
  33. write(message)
  34. end
  35.  
  36. function getCredits()
  37. f = fs.open("/disk/credits", "r")
  38. credits = f.readLine()
  39. f.close()
  40.  
  41. f = fs.open("startCredits", "w")
  42. f.write(credits)
  43. f.close()
  44. credits = tonumber(credits)
  45. end
  46.  
  47. function startingCredits()
  48. f = fs.open("startCredits", "r")
  49. startCredits = f.readLine()
  50. f.close()
  51. startCredits = tonumber(startCredits)
  52. end
  53.  
  54. function generateNumber()
  55. randomNum = math.random(1, 52)
  56. end
  57.  
  58. function restrictCard()
  59. restricted = {}
  60. for i = 1, #playerCards do
  61. table.insert(restricted, playerCards[i])
  62. end
  63. for i = 1, #dealerHiddenCards do
  64. table.insert(restricted, dealerHiddenCards[i])
  65. end
  66. for i = 1, #dealerVisibleCards do
  67. table.insert(restricted,dealerVisibleCards[i])
  68. end
  69. end
  70.  
  71. function checkRestrict()
  72. isRestricted = true
  73. for key, value in ipairs(restricted) do
  74. if randomNum ~= tonumber(value) then
  75. isRestricted = false
  76. break
  77. end
  78. end
  79. end
  80.  
  81.  
  82. function drawCard()
  83. generateNumber()
  84. checkRestrict()
  85. return randomNum
  86. end
  87.  
  88.  
  89.  
  90. function playerCard()
  91. drawCard()
  92. playerValue = playerValue + cardValue[randomNum]
  93. playerCard1 = cardName[randomNum]
  94. table.insert(playerCards, playerCard1)
  95. restrictCard()
  96. drawCard()
  97. playerValue = playerValue + cardValue[randomNum]
  98. playerCard2 = cardName[randomNum]
  99. table.insert(playerCards, playerCard2)
  100. restrictCard()
  101. end
  102.  
  103. function dealerCard()
  104. drawCard()
  105. dealerValue = dealerValue + cardValue[randomNum]
  106. dealerVisibleCard1 = cardName[randomNum]
  107. table.insert(dealerVisibleCards, dealerVisibleCard1)
  108. restrictCard()
  109. drawCard()
  110. dealerValue = dealerValue + cardValue[randomNum]
  111. dealerHiddenCard = cardName[randomNum]
  112. table.insert(dealerHiddenCards, dealerHiddenCard)
  113. restrictCard()
  114. end
  115.  
  116. function hitCard(receiver)
  117. drawCard()
  118. card = cardName[randomNum]
  119. table.insert(receiver, card)
  120. restrictCard()
  121. end
  122.  
  123. function hitPlayer()
  124. hitCard(playerCards)
  125. playerValue = playerValue + cardValue[randomNum]
  126. normalScreen()
  127. end
  128.  
  129. function hitDealer()
  130. hitCard(dealerVisibleCards)
  131. dealerValue = dealerValue + cardValue[randomNum]
  132. normalScreen()
  133. sleep(0.5)
  134. end
  135.  
  136. function doubleDownMove()
  137. pot = pot * 2
  138. hitPlayer()
  139. stayPlayer = true
  140. if stayDealer == true then
  141. stay = true
  142. else
  143. while stayDealer == false do
  144. dealerMove()
  145. end
  146. end
  147. sleep(0.5)
  148. end
  149.  
  150.  
  151. function dealerMove()
  152. if dealerValue < 17 then
  153. hitDealer()
  154. elseif dealerValue >= 17 then
  155. stayDealer = true
  156. if stayPlayer == true then
  157. stay = true
  158. end
  159. end
  160. sleep(0.5)
  161. end
  162.  
  163. function hitOrStay()
  164. waitClick() -- venter på stay, hit eller double down
  165. if x >2 and x < 16 and y > 14 and y < 18 then -- hit
  166. doubleDown = 1
  167. hitPlayer()
  168. --if stayDealer == false then
  169. --dealerMove()
  170. -- end
  171.  
  172. elseif x >20 and x < 33 and y > 14 and y < 18 and doubleDown == 0 then -- double down
  173. doubleDownMove()
  174. while stayDealer == false do
  175. dealerMove()
  176. end
  177. doubleDown = 1
  178.  
  179. elseif x >37 and x < 50 and y > 14 and y < 18 then -- stay
  180. stayPlayer = true
  181. doubleDown = 1
  182. while stayDealer == false do
  183. dealerMove()
  184. end
  185. end
  186. end
  187.  
  188. function checkBust()
  189. if playerValue > 21 then
  190. playerBust()
  191. elseif dealerValue > 21 then
  192. dealerBust()
  193. end
  194. end
  195.  
  196. function playerBust()
  197. isPlayerBust = true
  198. messageScreen("You busted!")
  199. dealerWin()
  200. end
  201.  
  202. function dealerBust()
  203. isDealerBust = true
  204. messageScreen("Dealer busted!", 6,15)
  205. playerWin()
  206. end
  207.  
  208. function resolve()
  209. --if playerValue > 21 then
  210. --playerBust()
  211. --elseif dealerValue > 21 then
  212. --dealerBust()
  213. if isPlayerBust or isDealerBust == true then
  214. isPlayerBust = false
  215. isDealerBust = false
  216. elseif dealerValue == 21 then
  217. dealerBlackjack()
  218. elseif dealerValue > playerValue then
  219. dealerWin()
  220. elseif dealerValue == playerValue then
  221. draw()
  222. elseif playerValue == 21 and dealerValue ~= 21 then
  223. playerBlackjack()
  224. elseif dealerValue < playerValue then
  225. playerWin()
  226. end
  227. checkBroke()
  228. end
  229.  
  230. function draw()
  231. messageScreen("Draw")
  232. stay = true
  233. end
  234.  
  235. function dealerWin()
  236. messageScreen("Dealer wins")
  237. stay = true
  238. end
  239.  
  240. function playerWin()
  241. credits = credits + pot
  242. messageScreen("You win!") -- player vinder
  243. stay = true
  244. end
  245.  
  246. function dealerBlackjack()
  247. sleep(1)
  248. messageScreen("Dealer got Blackjack!")
  249. stay = true
  250. end
  251.  
  252. function playerBlackjack()
  253. credits = credits + (math.floor(pot * 2))
  254. messageScreen("You got Blackjack!")
  255. stay = true
  256. end
  257.  
  258. function checkBroke()
  259. if credits < 1 then
  260. broke()
  261. end
  262. end
  263.  
  264. function broke()
  265. messageScreen("No more credits")
  266. writeCredits()
  267. sleep(10)
  268. os.reboot()
  269. end
  270.  
  271. function checkCredit()
  272. if credits < 1 then
  273. setColor(colors.black, colors.lime)
  274. mWrite("No credits", 21,16)
  275. credits = 0
  276. pot = startCredits
  277. sleep(2)
  278. end
  279. if credits > startCredits then
  280. setColor(colors.black, colors.lime)
  281. --mWrite(startCredits, 24,16)
  282. credits = startCredits
  283. pot = 0
  284. end
  285. setColor(colors.white, colors.black)
  286. end
  287.  
  288.  
  289.  
  290.  
  291.  
  292. function placeBet()
  293. pot = 0
  294.  
  295. while true do
  296. waitClick()
  297. if x >15 and x < 20 and y == 15 then -- +1
  298. pot = pot +1
  299. credits = credits - 1
  300. checkCredit()
  301. betScreen()
  302.  
  303. elseif x >15 and x < 20 and y == 16 then -- +10
  304. pot = pot +10
  305. credits = credits - 10
  306. checkCredit()
  307. betScreen()
  308.  
  309. elseif x >15 and x < 20 and y == 17 then -- +100
  310. pot = pot +100
  311. credits = credits - 100
  312. checkCredit()
  313. betScreen()
  314.  
  315. elseif x >32 and x < 37 and y == 15 then -- -1
  316. pot = pot -1
  317. credits = credits + 1
  318. checkCredit()
  319. betScreen()
  320.  
  321. elseif x >32 and x < 37 and y == 16 then -- -10
  322. pot = pot -10
  323. credits = credits + 10
  324. checkCredit()
  325. betScreen()
  326.  
  327. elseif x >32 and x < 37 and y == 17 then -- -100
  328. pot = pot -100
  329. credits = credits + 100
  330. checkCredit()
  331. betScreen()
  332.  
  333. elseif x >23 and x < 29 and y == 17 then
  334. pot = pot * 2
  335. break
  336. end
  337. end
  338. end
  339.  
  340. function writeCredits()
  341. file = fs.open("/disk/credits", "w")
  342. file.write(credits)
  343. end
  344.  
  345. function playAgain()
  346. waitClick()
  347. if x >6 and x < 20 and y > 6 and y < 12 then -- new game
  348. newGame()
  349. elseif x >32 and x < 46 and y > 6 and y < 12 then -- Cash out
  350. cashOut()
  351. end
  352. end
  353.  
  354. function deleteLine(table, line)
  355. deleteTable.remove(table, line)
  356. end
  357.  
  358.  
  359. function newGame()
  360. stay = false
  361. stayDealer = false
  362. satyPlayer = false
  363. playerCards = 0
  364. dealerVisibleCards = 0
  365. dealerHiddenCards = 0
  366. doubleDown = 0
  367. playerValue = 0
  368. dealerValue = 0
  369. y = 0
  370. pot = 0
  371. isPlayerBust = false
  372. isDealerBust = false
  373. startCredits = credits
  374. end
  375.  
  376. function cashOut()
  377. writeCredits()
  378. sleep(10)
  379. os.reboot()
  380. end
  381.  
  382.  
  383. function setColor(text, background)
  384. term.setTextColor(text)
  385. term.setBackgroundColor(background)
  386. end
  387.  
  388. function drawImage(image)
  389. myImage = paintutils.loadImage(image)
  390. paintutils.drawImage(myImage, 1, 1)
  391. end
  392.  
  393. function writePlayerHand()
  394. y = 7
  395. for i = 1, #playerCards do
  396. mWrite(playerCards[i], 4,y)
  397. y = y+1
  398. end
  399. end
  400.  
  401. function writeDealerHand()
  402. y = 8
  403. for i = 1, #dealerVisibleCards do
  404. mWrite(dealerVisibleCards[i], 29,y)
  405. y = y+1
  406. end
  407. end
  408.  
  409.  
  410.  
  411. function normalScreen()
  412. term.clear()
  413. drawImage("normalScreen")
  414. setColor(colors.black, colors.lightGray)
  415. mWrite("Your hand", 4,2)
  416. mWrite("Dealer hand", 39,2)
  417. mWrite("Credits", 16,2)
  418. mWrite("Pot", 31,2)
  419.  
  420. setColor(colors.black, colors.white)
  421. mWrite(credits, 17,3)
  422. mWrite(pot, 30,3)
  423. mWrite("Hit", 8,16)
  424. mWrite("DoubleDown", 22,16)
  425. mWrite("Stay", 42,16)
  426.  
  427. setColor(colors.white, colors.gray)
  428. mWrite(playerValue, 6,4)
  429. mWrite(dealerValue, 41,4)
  430.  
  431. writePlayerHand()
  432. mWrite("Hidden", 29,7)
  433. writeDealerHand()
  434. setColor(colors.white, colors.black)
  435. end
  436.  
  437. function betScreen()
  438. term.clear()
  439. drawImage("betScreen")
  440. setColor(colors.black, colors.green)
  441. mWrite("Place bet", 21,15)
  442. mWrite("place", 24,17)
  443. setColor(colors.black, colors.lime)
  444. mWrite(pot, 24,16)
  445. setColor(colors.black, colors.lightGray)
  446. mWrite("+1", 16,15)
  447. mWrite("+10", 16,16)
  448. mWrite("+100", 16,17)
  449. mWrite("-1", 33,15)
  450. mWrite("-10", 33,16)
  451. mWrite("-100", 33,17)
  452. mWrite("Credits", 16,2)
  453. setColor(colors.black, colors.white)
  454. mWrite(credits, 17,3)
  455. setColor(colors.white, colors.black)
  456. end
  457.  
  458. function messageScreen(message)
  459. term.clear()
  460. drawImage("messageScreen")
  461. setColor(colors.black, colors.white)
  462. mWrite(message, 21,16)
  463.  
  464. setColor(colors.black, colors.lightGray)
  465. mWrite("Your hand", 4,2)
  466. mWrite("Dealer hand", 39,2)
  467. mWrite("Credits", 16,2)
  468. mWrite("Pot", 31,2)
  469.  
  470. setColor(colors.black, colors.white)
  471. mWrite(credits, 17,3)
  472. mWrite(pot, 30,3)
  473.  
  474. setColor(colors.white, colors.gray)
  475. mWrite(playerValue, 6,4)
  476. mWrite(dealerValue, 41,4)
  477.  
  478. mWrite(dealerHiddenCards[1], 29,7)
  479. writePlayerHand()
  480. writeDealerHand()
  481. setColor(colors.white, colors.black)
  482. sleep(3)
  483. end
  484.  
  485. function newGameScreen()
  486. term.clear()
  487. drawImage("newGameScreen")
  488. setColor(colors.white, colors.black)
  489. mWrite("New game?", 22,9)
  490. setColor(colors.black, colors.white)
  491. mWrite("New game", 9,9)
  492. mWrite("Cash out", 35,9)
  493. setColor(colors.white, colors.black)
  494. end
  495.  
  496. function startScreen()
  497. term.clear()
  498. startScreen = paintutils.loadImage("startScreenBlack")
  499. paintutils.drawImage(startScreen, 4, 2)
  500. setColor(colors.white, colors.black)
  501. mWrite("Insert Credit Disk to play", 13, 19)
  502. end
  503.  
  504.  
  505.  
  506.  
  507.  
  508. --################Loop##############
  509. while true do
  510. startScreen()
  511. activate()
  512. getCredits()
  513. startingCredits()
  514.  
  515. while true do
  516. playerCards = {}
  517. dealerVisibleCards = {}
  518. dealerHiddenCards = {}
  519. betScreen()
  520. placeBet()
  521.  
  522. normalScreen()
  523. sleep(0.5)
  524. playerCard()
  525. sleep(0.5)
  526. dealerCard()
  527. sleep(0.5)
  528. normalScreen()
  529.  
  530. while stay == false do
  531. hitOrStay()
  532. checkBust()
  533. end
  534.  
  535. resolve()
  536. newGameScreen()
  537. playAgain()
  538.  
  539. end
  540. end
RAW Paste Data