Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. package {
  2. import flash.display.*;
  3. import flash.events.*;
  4. import flash.text.*;
  5. import flash.utils.Timer;
  6.  
  7. public class Blackjack extends MovieClip {
  8. // game objects
  9. private var cash:int; // keep track of money
  10. private var bet:int; // this bet
  11. private var deck:Array; // starts with all card values
  12. private var playerHand:Array; // player’s card values
  13. private var dealerHand:Array; // dealer’s card values
  14. private var dealerCard:Cards; // reference to face-down card
  15. private var cards:Array; // all cards, for clean-up
  16.  
  17. // timer for future events
  18. private var timedEvents:Timer;
  19. private var timedEventsList:Array;
  20. public function startBlackjack() {
  21.  
  22. // initial cash
  23. cash = 100;
  24. showCash();
  25. cards = new Array();
  26.  
  27. // start game
  28. createDeck();
  29.  
  30. // set up timed events list
  31. timedEventsList = new Array();
  32.  
  33. // remove all buttons
  34. removeChild(hitButton);
  35. removeChild(stayButton);
  36. removeChild(continueButton);
  37. removeChild(dealButton);
  38. removeChild(addBetButton);
  39.  
  40. // start first hand
  41. startHand();
  42. }
  43. private function createDeck() {
  44. // create six ordered decks in an array
  45. // using strings to represent card values
  46. var suits:Array = ["c","d","h","s"];
  47. var temp = new Array();
  48. for (var i:int=0;i<5;i++) {
  49. for(var suit:int=0;suit<4;suit++) {
  50. for(var num:int=1;num<14;num++) {
  51. temp.push(suits[suit]+num);
  52. }
  53. }
  54.  
  55. }
  56. // pick random cards until deck has been shuffled
  57. deck = new Array();
  58. while (temp.length > 0) {
  59. var r:int = Math.floor(Math.random()*temp.length);
  60. deck.push(temp[r]);
  61. temp.splice(r,1);
  62. }
  63. }
  64. // start checking every .25 seconds for an event to play out
  65. private function startTimedEvents() {
  66. timedEvents = new Timer(1000);
  67. timedEvents.addEventListener(TimerEvent.TIMER, playTimedEvents);
  68. timedEvents.start();
  69. }
  70. // done with events for now
  71. private function stopTimedEvents() {
  72. timedEvents.stop();
  73. timedEvents.removeEventListener(TimerEvent.TIMER, playTimedEvents);
  74. timedEvents = null;
  75. }
  76. // add an event to the list to be played out soon
  77. private function addTimedEvent(eventString) {
  78. timedEventsList.push(eventString);
  79. }
  80. // init hand arrays and bet
  81. private function startHand() {
  82. // empty player and dealer hands
  83. playerHand = new Array();
  84. dealerHand = new Array();
  85. playerValueDisplay.text = "";
  86. dealerValueDisplay.text = "";
  87. // start off each hand with smallest bet and deal card hidden
  88. bet = 5;
  89. showBet();
  90. // show buttons
  91. addChild(addBetButton);
  92. addChild(dealButton);
  93. resultDisplay.text = "Add to your bet if you wish then press Deal Cards.";
  94. }
  95. // allow the player to increase her bet up to $25
  96. private function addToBet(e:MouseEvent) {
  97. bet += 5;
  98. if (bet > 25) bet = 25; // limit bet
  99. showBet();
  100. }
  101. // see if there is a new event in the list and do it
  102. private function playTimedEvents(e:TimerEvent) {
  103. var thisEvent = timedEventsList.shift();
  104. if (thisEvent == "deal card to dealer") {
  105. dealCard("dealer");
  106. } else if (thisEvent == "deal card to player") {
  107. dealCard("player");
  108. showPlayerHandValue();
  109. } else if (thisEvent == "end deal") {
  110. if (!checkForBlackjack()) {
  111. waitForHitOrStay();
  112. }
  113. } else if (thisEvent == "show dealer card") {
  114. showDealerCard();
  115. } else if (thisEvent == "dealer move") {
  116. dealerMove();
  117. }
  118. }
  119. private function dealCards(e:MouseEvent) {
  120. // take bet away from player
  121. cash -= bet;
  122. showCash();
  123. // add events to deal first cards
  124. addTimedEvent("deal card to dealer");
  125. addTimedEvent("deal card to player");
  126. addTimedEvent("deal card to dealer");
  127. addTimedEvent("deal card to player");
  128. addTimedEvent("end deal");
  129. startTimedEvents();
  130. // switch buttons
  131. removeChild(addBetButton);
  132. removeChild(dealButton);
  133. }
  134. private function dealCard(toWho) {
  135. // get the next card from the deck
  136. var newCardVal:String = deck.pop();
  137. if (toWho == "player") {
  138. // if it goes to the player, then show it and update hand value
  139. playerHand.push(newCardVal);
  140. showCard(newCardVal, "player");
  141. } else {
  142. // if it goes to the dealer, then show it, but only update hand value later
  143. dealerHand.push(newCardVal);
  144. showCard(newCardVal, "dealer");
  145. }
  146. }
  147. // add a card object to the display
  148. private function showCard(cardVal, whichHand) {
  149. // get a new card
  150. var newCard:Cards = new Cards();
  151. newCard.gotoAndStop(cardVal);
  152. // set the position of the new card
  153. if (whichHand == "dealer"){
  154. newCard.y = 100;
  155. if (dealerHand.length == 1) {
  156. // show back for first dealer card
  157. newCard.gotoAndStop("back");
  158. dealerCard = newCard;
  159. }
  160. var whichCard:int = dealerHand.length;
  161. } else if (whichHand == "player") {
  162. newCard.y = 200;
  163. whichCard = playerHand.length;
  164. }
  165. newCard.x = 70*whichCard;
  166. // add the card
  167. addChild(newCard);
  168. cards.push(newCard);
  169. }
  170. // time for player to make a decision
  171. private function waitForHitOrStay() {
  172. addChild(hitButton);
  173. addChild(stayButton);
  174. timedEvents.stop();
  175. }
  176. // player draws another card
  177. private function hit(e:MouseEvent=null) {
  178. dealCard("player");
  179. showPlayerHandValue();
  180. // if player gets 21 or more, go to dealer
  181. if (handValue(playerHand) >= 21) stay();
  182. }
  183. // player done, so show dealer's first card and continue
  184. private function stay(e:MouseEvent=null) {
  185. removeChild(hitButton);
  186. removeChild(stayButton);
  187. addTimedEvent("show dealer card");
  188. addTimedEvent("dealer move");
  189. startTimedEvents();
  190. }
  191. // player stays, so time to show dealer card and hand value so far
  192. private function showDealerCard() {
  193. dealerCard.gotoAndStop(dealerHand[0]);
  194. showDealerHandValue();
  195. }
  196. // dealer gets a card
  197. private function dealerMove() {
  198. if (handValue(dealerHand) < 17) {
  199. // dealer still doesn’t have 17, so must continue to draw
  200. dealCard("dealer");
  201. showDealerHandValue();
  202. addTimedEvent("dealer move");
  203. } else {
  204. // dealer is done
  205. decideWinner();
  206. stopTimedEvents();
  207. showCash();
  208. addChild(continueButton);
  209. }
  210. }
  211. // calculate hand value
  212. private function handValue(hand) {
  213. var total:int = 0;
  214. var ace:Boolean = false;
  215. for(var i:int=0;i<hand.length;i++) {
  216. // add value of card
  217. var val:int = parseInt(hand[i].substr(1,2));
  218. // jack, queen, and king = 10
  219. if (val > 10) val = 10;
  220. total += val;
  221. // remember if an ace is found
  222. if (val == 1) ace = true;
  223. }
  224. // ace can = 11 if it doesn't bust player
  225. if ((ace) && (total <= 11)) total += 10;
  226. return total;
  227. }
  228. // check to see whether either has blackjack
  229. private function checkForBlackjack():Boolean {
  230. // if player has blackjack
  231. if ((playerHand.length == 2) && (handValue(playerHand) == 21)) {
  232. // award 150 percent winnings
  233. cash += bet*2.5;
  234. resultDisplay.text = "Blackjack!";
  235. stopTimedEvents();
  236. showCash();
  237. addChild(continueButton);
  238. return true;
  239. } else {
  240. return false;
  241. }
  242. }
  243. // see who won, or if there is a tie
  244. private function decideWinner() {
  245. var playerValue:int = handValue(playerHand);
  246. var dealerValue:int = handValue(dealerHand)
  247. if (playerValue > 21) {
  248. resultDisplay.text = "You Busted!";
  249. } else if (dealerValue > 21) {
  250. cash += bet*2;
  251. resultDisplay.text = "Dealer Busts. You Win!";
  252. } else if (dealerValue > playerValue) {
  253. resultDisplay.text = "You Lose!";
  254. } else if (dealerValue == playerValue) {
  255. cash += bet;
  256. resultDisplay.text = "Tie!";
  257. } else if (dealerValue < playerValue) {
  258. cash += bet*2;
  259. resultDisplay.text = "You Win!";
  260. }
  261. }
  262. // start next hand
  263. function newDeal(e:MouseEvent) {
  264. removeChild(continueButton);
  265. resetCards();
  266. // if deck has less than 26 cards, reshuffle
  267. if (deck.length < 26) {
  268. createDeck();
  269. } else {
  270. startHand();
  271. }
  272. }
  273. private function showPlayerHandValue() {
  274. playerValueDisplay.text = handValue(playerHand);
  275. }
  276. private function showDealerHandValue() {
  277. dealerValueDisplay.text = handValue(dealerHand);
  278. }
  279. // display cash with $
  280. private function showCash() {
  281. cashDisplay.text = "Cash: $"+cash;
  282. }
  283. // display bet with $
  284. private function showBet() {
  285. betDisplay.text = "Bet: $"+bet;
  286. }
  287. // remove cards from table
  288. function resetCards() {
  289. while(cards.length > 0) {
  290. removeChild(cards.pop());
  291. }
  292. }
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement