ravneravn

Untitled

May 9th, 2013
68
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. disk = peripheral.wrap("left")
  2. local 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"}
  3. local 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}
  4. local playerCards = {}
  5. local dealerVisibleCards = {}
  6. local dealerHiddenCards = {}
  7. local restricted = {}
  8. local minCards = 1
  9. local maxCards = 52
  10. stayPlayer = false
  11. stayDealer = false
  12. stay = false
  13.  
  14.  
  15.  
  16. function activate()
  17. os.pullEvent("disk")
  18. end
  19.  
  20. function waitClick()
  21. event, side, x, y = os.pullEvent("mouse_click")
  22. end
  23.  
  24. function mWrite(message, x, y)
  25. term.setCursorPos(x,y)
  26. term.write(message)
  27. end
  28.  
  29. function startScreen()
  30. drawImage("startScreen") -- skal ændres til koden for startScreen
  31. mWrite("Your hand", 4,2)
  32. mWrite("Dealer hand", 22,2)
  33. mWrite("Credits: "..credits, 22,1)
  34. mWrite("Pot", 13,2)
  35. mWrite(pot, 13,4)
  36. end
  37.  
  38. function getCredits()
  39. f = fs.open("/disk/credits", "r")
  40. credits = f.readLine()
  41. end
  42.  
  43. function generateNumber()
  44. randomNum = math.random(1, 52)
  45. end
  46.  
  47. function restrictCard()
  48. restricted = {}
  49. for i = 1, #playerCards do
  50. table.insert(restricted, playerCards[i])
  51. end
  52. for i = 1, #dealerHiddenCards do
  53. table.insert(restricted, dealerHiddenCards[i])
  54. end
  55. for i = 1, #dealerVisibleCards do
  56. table.insert(restricted,dealerVisibleCards[i])
  57. end
  58. end
  59.  
  60. function checkRestrict()
  61. isRestricted = true
  62. for key, value in ipairs(restricted) do
  63. if randomNum ~= tonumber(value) then
  64. isRestricted = false
  65. break
  66. end
  67. end
  68. end
  69.  
  70.  
  71. function drawCard()
  72. generateNumber()
  73. checkRestrict()
  74. return randomNum
  75. end
  76.  
  77.  
  78.  
  79. function playerCard()
  80. drawCard()
  81. playerCard1 = cardName[randomNum]
  82. drawCard()
  83. playerCard2 = cardName[randomNum]
  84. table.insert(playerCards, playerCard1)
  85. table.insert(playerCards, playerCard2)
  86. restrictCard()
  87. mWrite(playerCards[1], 4,8)
  88. mWrite(playerCards[2], 4,9)
  89. mWrite(playerHand, 5,4)
  90. end
  91.  
  92. function hitCard(receiver)
  93. yPlayer = 10
  94. yDealer = 10
  95. drawCard()
  96. card = cardName[randomNum]
  97. table.insert(receiver, card)
  98. if receiver == playerCards then
  99. mWrite(card, 4,yPlayer)
  100. mWrite(playerHand, 5,4)
  101. yPlayer = yPlayer +1
  102. elseif receiver == dealerCards then
  103. mWrite(card, 17, yDealer)
  104. mWrite(dealerHand, 33,4)
  105. yDealer = yDealer +1
  106. end
  107. end
  108.  
  109. function dealerCard()
  110. drawCard()
  111. dealerVisibleCard1 = cardName[randomNum]
  112. drawCard()
  113. dealerHiddenCard = cardName[randomNum]
  114. table.insert(dealerVisibleCards, dealerVisibleCard1)
  115. table.insert(dealerHiddenCards, dealerHiddenCard)
  116. restrictCard()
  117. mWrite(dealerHand, 33,4)
  118. mWrite("Hidden", 17,8)
  119. mWrite(dealerVisibleCards[1], 17,9)
  120. mWrite(dealerHand, 33,4)
  121. end
  122.  
  123.  
  124. function doubleDown()
  125. pot = stake * 2
  126. hitPlayer()
  127. stayPlayer = true
  128. if stayDealer == true then
  129. stay = true
  130. end
  131. end
  132.  
  133.  
  134. function hitPlayer()
  135. hitCard(playerCards)
  136. end
  137.  
  138. function hitDealer()
  139. hitCard(dealerVisibleCards)
  140. end
  141.  
  142.  
  143. function dealerMove()
  144. if dealerHand < 17 then
  145. hitDealer()
  146. elseif dealerHand > 17 then
  147. stayDealer = true
  148. if stayPlayer == true then
  149. stay = true
  150. end
  151. end
  152. end
  153.  
  154. function checkBust()
  155. if playerHand > 21 then
  156. playerBust()
  157. elseif dealerHand > 21 then
  158. dealerBust()
  159. end
  160. end
  161.  
  162. function playerBust()
  163. drawImage("messageScreen")
  164. mWrite("You busted!", 6,15)
  165. dealerWin()
  166. end
  167.  
  168. function dealerBust()
  169. drawImage("messageScreen")
  170. mWrite("Dealer busted!", 6,15)
  171. playerWin()
  172. end
  173.  
  174. function resolve()
  175. if playerHand > 21 then
  176. playerBust()
  177. elseif dealerHand > 21 then
  178. dealerBust()
  179. elseif dealerHand == 21 then
  180. dealerBlackjack()
  181. elseif dealerHand > playerHand then
  182. dealerWin()
  183. elseif dealerHand == playerHand then
  184. draw()
  185. elseif playerHand == 21 and dealerHand ~= 21 then
  186. playerBlackjack()
  187. elseif dealerHand < playerHand then
  188. playerWin()
  189. end
  190. checkBroke()
  191. end
  192.  
  193. function draw()
  194. drawImage("messageScreen")
  195. mWrite("Draw", 6,15)
  196. end
  197.  
  198. function dealerWin()
  199. drawImage("messageScreen")
  200. mWrite("Dealer wins", 6,15)
  201. credits = credits - stake
  202. mWrite("Credits: "..credits, 22,1)
  203. end
  204.  
  205. function playerWin()
  206. drawImage("messageScreen") -- message image
  207. mWrite("You win!", 6,15) -- player vinder
  208. credits = credits + stake
  209. mWrite("Credits: "..credits, 22,1)
  210. end
  211.  
  212. function dealerBlackjack()
  213. drawImage("messageScreen")
  214. mWrite("Dealer got Blackjack, you loose", 6,15)
  215. credits = credits - stake
  216. mWrite("Credits: "..credits, 22,1)
  217. end
  218.  
  219. function playerBlackjack()
  220. drawImage("messageScreen")
  221. mWrite("You got Blackjack, you win!", 6,15)
  222. credits = credits + (math.floor(stake * 2))
  223. mWrite("Credits: "..credits, 22,1)
  224. end
  225.  
  226. function checkBroke()
  227. if credits < 1 then
  228. broke()
  229. end
  230. end
  231.  
  232. function broke()
  233. drawImage("messageScreen")
  234. mWrite("No more credits", 6,15)
  235. sleep(10)
  236. os.restart()
  237. end
  238.  
  239. function checkCredit()
  240. if credits < 1 then
  241. mWrite("No credits", 11,12)
  242. end
  243. end
  244.  
  245.  
  246.  
  247.  
  248.  
  249. function placeBet()
  250. stake = 0
  251. pot = stake
  252. drawImage("betScreen")
  253. mWrite("Place your bet", 11,11)
  254. while true do
  255. waitClick()
  256. if x >9 and x < 12 and y == 13 then
  257. stake = stake + 1
  258. credits = credits - 1
  259. mWrite("Credits: "..credits, 22,1)
  260. mWrite(stake, 12,12)
  261. checkCredit()
  262.  
  263. elseif x >9 and x < 12 and y == 14 then
  264. stake = stake + 10
  265. credits = credits - 10
  266. mWrite("Credits: "..credits, 22,1)
  267. mWrite(stake, 12,12)
  268. checkCredit()
  269.  
  270. elseif x >9 and x < 12 and y == 15 then
  271. stake = stake + 100
  272. credits = credits - 100
  273. mWrite("Credits: "..credits, 22,1)
  274. mWrite(stake, 12,12)
  275. checkCredit()
  276.  
  277. elseif x >17 and x < 21 and y == 13 then
  278. stake = stake - 1
  279. credits = credits + 1
  280. mWrite("Credits: "..credits, 22,1)
  281. mWrite(stake, 12,12)
  282. checkCredit()
  283.  
  284. elseif x >17 and x < 21 and y == 14 then
  285. stake = stake - 10
  286. credits = credits + 10
  287. mWrite("Credits: "..credits, 22,1)
  288. mWrite(stake, 12,12)
  289. checkCredit()
  290.  
  291. elseif x >17 and x < 21 and y == 15 then
  292. stake = stake - 100
  293. credits = credits + 100
  294. mWrite("Credits: "..credits, 22,1)
  295. mWrite(stake, 12,12)
  296. checkCredit()
  297.  
  298. elseif x >12 and x < 18 and y == 14 then
  299. break
  300. end
  301. end
  302. end
  303.  
  304. function hitOrStay()
  305. waitClick() -- venter på stay, hit eller double down
  306. if x >2 and x < 10 and y > 13 and y < 17 then -- hit
  307. hitPlayer()
  308. if stayDealer == false then
  309. dealerMove()
  310. end
  311.  
  312. elseif x >11 and x < 19 and y > 13 and y < 17 then -- double down
  313. doubleDown()
  314. while stayDealer == false do
  315. dealerMove()
  316. end
  317.  
  318.  
  319. elseif x >20 and x < 28 and y > 13 and y < 17 then -- stay
  320. stayPlayer = true
  321. while stayDealer == false do
  322. dealerMove()
  323. end
  324. end
  325. end
  326.  
  327. function writeCredits()
  328. file = fs.open("/disk/credits", "w")
  329. file.write(credits)
  330. end
  331.  
  332. function playAgain()
  333. drawImage("newGameScreen") -- tegn boks med spørgsmål
  334. mWrite("New game", 6, 10) -- nyt spil knap
  335. mWrite("Cash out", 18, 10) -- cash out
  336. waitClick()
  337. if x >5 and x < 13 and y > 8 and y < 12 then -- new game
  338. newGame()
  339. elseif x >17 and x < 25 and y > 8 and y < 12 then -- Cash out
  340. cashOut()
  341. end
  342. end
  343.  
  344.  
  345. function newGame()
  346. stay = false
  347. stayDealer = false
  348. satyPlayer = false
  349. local playerCards = {}
  350. local dealerVisibleCards = {}
  351. local dealerHiddenCards = {}
  352. end
  353.  
  354. function cashOut()
  355. drawImage("messageScreen") -- draw message image
  356. writeCredits()
  357. mWrite("Thanks for playing", 6,15) -- message screen
  358. sleep(10)
  359. os.restart()
  360. end
  361.  
  362. function drawImage(image)
  363. myImage = paintutils.loadImage(image)
  364. paintutils.drawImage(myImage, 1, 1)
  365. end
  366.  
  367.  
  368.  
  369. --################Loop##############
  370. while true do
  371. activate()
  372. getCredits()
  373.  
  374. while true do
  375. drawImage("betScreen")
  376. placeBet()
  377.  
  378. playerCard()
  379. dealerCard()
  380.  
  381. mWrite("Hit", 5,15)
  382. mWrite("Stay", 22,15)
  383. mWrite("Double Down", 13,15)
  384.  
  385. while true do
  386. hitOrStay()
  387. checkBust()
  388. if stay == true then
  389. break
  390. end
  391. resolve()
  392. end
  393. sleep(5)
  394. playAgain()
  395.  
  396. end
  397. end
RAW Paste Data