ravneravn

blackjack test

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