Examples

Primative Pong Game
Primative Black Jack Game
Basic OO Black Jack Game
Basic Form
Complex Form
JavaScript from ActionScript
Basic Image Animation
Hello World on a Timer
Tads Basic Checker Class
A Simple FLV Player
Simple getter use
A liaison Class
Parent Child Control
Wrapper to server bridge
Primative Space Shooter Game
Simple XML Loader
Primative Speed Simulation
Simple Sound Mixing
Simple BlackJack with AS3
 



Overview:
This black jack game is just a standard deal hit and split game with no betting.
(A bet system can be easily added into the calculateWinner function.)
The main functions are the hit and calculateWinner functions.
The basic logic is to always calculate for player hands that are as close to 21 as possible.
The dealers hand should always aim for 17.
An array of aces is created so that aces used
(changed from one to eleven or vise versa) can be kept track of.

To compile this game you will need to create your own images and sound, and
change the name of the embed folder (../exampimages) to the folder where your images are.
package { import flash.display.Sprite; import flash.display.Graphics; import flash.display.DisplayObject; import flash.events.TimerEvent; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextFieldAutoSize; import flash.text.TextFormatAlign; import flash.utils.Timer; import flash.geom.Rectangle; import flash.media.Sound; /* a (t)a.d example copyright 2007 - 2009 (t)a.d */ [SWF(frameRate = '20', backgroundColor = '0xc8c8c8', width = '433', height = '400')] public class tadsBlackJack extends Sprite { private var playerTXTField:TextField; private var dealerTXTField:TextField; private var TXTFormat:TextFormat; private var playerCardOne:String = "0"; private var playerCardTwo:String = "0"; private var dealerCardOne:String = "0"; private var dealerCardTwo:String = "0"; private var playerHand:Number = 0; private var dealerHand:Number = 0; private var acesUsed:Number = 0; private var splitCardOne:String = "0"; private var splitCardTwo:String = "0"; private var splitCardThree:String = "0"; private var splitCardFour:String = "0"; private var splitCardFive:String = "0"; private var splitCardSix:String = "0"; private var splitHandOne:Number = 0; private var splitHandTwo:Number = 0; private var splitHandThree:Number = 0; private var hitButton:Sprite; private var firstSplitButton:Sprite; private var secondSplitButton:Sprite; private var hitSplitOneButton:Sprite; private var hitSplitTwoButton:Sprite; private var hitSplitThreeButton:Sprite; private var dealTXT:TextField; private var hitTXT:TextField; private var cardSpotOne:Sprite; private var cardSpotTwo:Sprite; private var cardSpotThree:Sprite; private var dealerSpot:Sprite; private var dealerGo:Sprite; private var dealerGoTXT:TextField; private var winField:TextField; [Embed(source="../exampimages/aimg.jpg")] private var AceImage:Class; [Embed(source="../exampimages/img2.jpg")] private var TwoImage:Class; [Embed(source="../exampimages/img3.jpg")] private var ThreeImage:Class; [Embed(source="../exampimages/img4.jpg")] private var FourImage:Class; [Embed(source="../exampimages/img5.jpg")] private var FiveImage:Class; [Embed(source="../exampimages/img6.jpg")] private var SixImage:Class; [Embed(source="../exampimages/img7.jpg")] private var SevenImage:Class; [Embed(source="../exampimages/img8.jpg")] private var EightImage:Class; [Embed(source="../exampimages/img9.jpg")] private var NineImage:Class; [Embed(source="../exampimages/img10.jpg")] private var TenImage:Class; [Embed(source="../exampimages/cardback.png")] private var CardBackImage:Class; [Embed(source="../exampimages/bjback.jpg")] private var GameBack:Class; private var theBackground:DisplayObject = new GameBack(); [Embed(source="../exampimages/playerwins.mp3")] private var WinVoice:Class; private var winSound:Sound = new WinVoice(); public function tadsBlackJack() { addChild(theBackground); TXTFormat = new TextFormat(); TXTFormat.font = "Arial"; TXTFormat.color = 0x000000; TXTFormat.size = 10; TXTFormat.align = TextFormatAlign.LEFT; winField = makeTXTField(winField, " "); /* The playerTXTField and dealerTXTField text fields are used as the brain of the game. The data of each hand is placed as text in these text fields and based on the numbers and phrases held in each one, winning, losing, split needs and draws can be calculated and remebered. Notice that they are never added to the display and yet still exist and can be manipulated. */ playerTXTField = new TextField(); playerTXTField.defaultTextFormat = TXTFormat; playerTXTField.autoSize = TextFieldAutoSize.LEFT; playerTXTField.height = 20; playerTXTField.selectable = false; playerTXTField.text = "Player cards: "+ playerCardOne + " " + playerCardTwo + " your hand totals: " + playerHand; playerTXTField.x = 50; playerTXTField.y = 50; dealerTXTField = new TextField(); dealerTXTField.defaultTextFormat = TXTFormat; dealerTXTField.autoSize = TextFieldAutoSize.LEFT; dealerTXTField.height = 20; dealerTXTField.selectable = false; dealerTXTField.text = "Dealer cards: "+ dealerCardOne + " " + dealerCardTwo + " dealer hand totals: " + dealerHand; dealerTXTField.x = 50; dealerTXTField.y = playerTXTField.y - 27; /* card spots */ cardSpotOne = new Sprite(); cardSpotOne.x = 20; cardSpotOne.y = 400/2 - 40; addChild(cardSpotOne); cardSpotTwo = new Sprite(); cardSpotTwo.x = cardSpotOne.x + 125 + 5 + 5; cardSpotTwo.y = 400/2 - 40; addChild(cardSpotTwo); cardSpotThree = new Sprite(); cardSpotThree.x = cardSpotTwo.x + 125 + 5 + 5; cardSpotThree.y = 400/2 - 40; addChild(cardSpotThree); dealerSpot = new Sprite(); dealerSpot.x = 5; dealerSpot.y = 5; addChild(dealerSpot); /* buttons */ var dealButton:Sprite = new Sprite(); dealButton.graphics.lineStyle(1, 0x000000); dealButton.graphics.beginFill(0x7FFF00); dealTXT = makeTXTField(dealTXT, "Deal"); dealButton.graphics.drawRect(0, 0, dealTXT.width, dealTXT.height); dealButton.addChild(dealTXT); dealButton.addEventListener(MouseEvent.CLICK, deal); dealButton.x = 20;dealButton.y = 330; addChild(dealButton); /* hit button */ hitButton = new Sprite(); var hitButtonGraphics:Sprite = new Sprite(); hitButtonGraphics.graphics.lineStyle(1, 0x000000); hitButtonGraphics.graphics.beginFill(0x0000FF); hitTXT = makeTXTField(hitTXT, "Hit"); hitButtonGraphics.graphics.drawRect(0, 0, hitTXT.width, hitTXT.height); hitButton.addEventListener(MouseEvent.CLICK, hit); hitButtonGraphics.addChild(hitTXT); hitButton.addChild(hitButtonGraphics); hitButton.x = dealButton.x + 55; hitButton.y = dealButton.y; addChild(hitButton); /* split button */ firstSplitButton = new Sprite(); firstSplitButton.graphics.lineStyle(1, 0x000000); firstSplitButton.graphics.beginFill(0xC0C0C0); var splitTXT:TextField = makeTXTField(splitTXT, "Split"); firstSplitButton.graphics.drawRect(0, 0, splitTXT.width, splitTXT.height); firstSplitButton.addEventListener(MouseEvent.CLICK, hit); firstSplitButton.x = hitButton.x + 65; firstSplitButton.y = hitButton.y;firstSplitButton.alpha = 0.5; splitTXT.x = firstSplitButton.x;splitTXT.y = firstSplitButton.y; addChild(splitTXT); addChild(firstSplitButton); /* split again button */ secondSplitButton = new Sprite(); secondSplitButton.graphics.lineStyle(1, 0x000000); secondSplitButton.graphics.beginFill(0xFFD700); var splitAgainTXT:TextField = makeTXTField(splitAgainTXT, "Split again"); secondSplitButton.graphics.drawRect(0, 0, splitAgainTXT.width, splitAgainTXT.height); secondSplitButton.addEventListener(MouseEvent.CLICK, hit); secondSplitButton.x = firstSplitButton.x + 120; secondSplitButton.y = firstSplitButton.y;splitAgainTXT.x = secondSplitButton.x;splitAgainTXT.y=secondSplitButton.y; secondSplitButton.alpha = 0.5; addChild(splitAgainTXT); addChild(secondSplitButton); /* hit first hand after split button */ hitSplitOneButton = new Sprite(); hitSplitOneButton.graphics.lineStyle(1, 0x000000); hitSplitOneButton.graphics.beginFill(0x1E90FF); var hitFirstHandTXT:TextField = makeTXTField(hitFirstHandTXT, "Hit first hand"); hitSplitOneButton.graphics.drawRect(0, 0, hitFirstHandTXT.width, hitFirstHandTXT.height); hitSplitOneButton.addEventListener(MouseEvent.CLICK, hit); hitSplitOneButton.x = firstSplitButton.x;hitSplitOneButton.alpha = 0.5; hitSplitOneButton.y = firstSplitButton.y + hitSplitOneButton.height + 3; hitFirstHandTXT.x = hitSplitOneButton.x;hitFirstHandTXT.y = hitSplitOneButton.y; addChild(hitFirstHandTXT); addChild(hitSplitOneButton); /* hit second hand after split button */ hitSplitTwoButton = new Sprite(); hitSplitTwoButton.graphics.lineStyle(1, 0x000000); hitSplitTwoButton.graphics.beginFill(0x1E90FF); var hitSecondHandTXT:TextField = makeTXTField(hitSecondHandTXT, "Hit second hand"); hitSplitTwoButton.graphics.drawRect(0, 0, hitSecondHandTXT.width, hitSecondHandTXT.height); hitSplitTwoButton.addEventListener(MouseEvent.CLICK, hit); hitSplitTwoButton.x = hitSplitOneButton.x; hitSplitTwoButton.y = hitSplitOneButton.y + hitSplitTwoButton.height + 3; hitSecondHandTXT.x = hitSplitTwoButton.x;hitSecondHandTXT.y = hitSplitTwoButton.y; hitSplitTwoButton.alpha = 0.5; addChild(hitSecondHandTXT); addChild(hitSplitTwoButton); /* hit third hand after second split button */ hitSplitThreeButton = new Sprite(); hitSplitThreeButton.graphics.lineStyle(1, 0x000000); hitSplitThreeButton.graphics.beginFill(0x1E90FF); var hitThirdHandTXT:TextField = makeTXTField(hitThirdHandTXT, "Hit third hand"); hitSplitThreeButton.graphics.drawRect(0, 0, hitThirdHandTXT.width, hitThirdHandTXT.height); hitSplitThreeButton.addEventListener(MouseEvent.CLICK, hit); hitSplitThreeButton.x = secondSplitButton.x; hitSplitThreeButton.y = secondSplitButton.y + hitSplitThreeButton.height + 3; hitThirdHandTXT.x = hitSplitThreeButton.x;hitThirdHandTXT.y = hitSplitThreeButton.y; hitSplitThreeButton.alpha = 0.5; addChild(hitThirdHandTXT); addChild(hitSplitThreeButton); /* done button end hand calculate dealers hand and who wins button */ dealerGo = new Sprite(); dealerGo.graphics.lineStyle(1, 0x000000); dealerGo.graphics.beginFill(0xFC0000); dealerGoTXT = makeTXTField(dealerGoTXT, "Done"); dealerGo.graphics.drawRect(0, 0, dealerGoTXT.width, dealerGoTXT.height); dealerGo.addEventListener(MouseEvent.CLICK, dealerBeginHandler); dealerGo.x = dealButton.x; dealerGo.y = dealButton.y + 45;dealerGoTXT.x = dealerGo.x;dealerGoTXT.y = dealerGo.y; dealerGo.alpha = 0.5; addChild(dealerGoTXT); addChild(dealerGo); } private function calculateWinner():void { if (!stage.contains(winField)) { winField.x = secondSplitButton.x + 30;winField.y = secondSplitButton.y+42; if (dealerHand > playerHand && dealerHand <= 21 && playerHand < 21 && splitHandOne == 0 && splitHandTwo == 0 && splitHandThree == 0) {winField.appendText("dealer wins the hand");} if (playerHand > dealerHand && playerHand <=21 && dealerHand < 21 && splitHandOne == 0 && splitHandTwo == 0 && splitHandThree == 0) {winField.appendText("player wins the hand");winSound.play();} if (playerHand == dealerHand && dealerHand <=21 && playerHand <=21 && splitHandOne == 0 && splitHandTwo == 0 && splitHandThree == 0) {winField.appendText("the hand pushes");} if (playerHand > 21 && splitHandOne == 0 && splitHandTwo == 0 && splitHandThree == 0) {winField.appendText("player busts");} if (playerHand <= 21 && splitHandOne == 0 && splitHandTwo == 0 && splitHandThree == 0 && dealerHand > 21) {winField.appendText("dealer busts");winSound.play();} if (splitHandOne != 0 && splitHandOne <= 21 && splitHandOne > dealerHand && dealerHand < 21) {winField.appendText("first hand wins");} if (splitHandOne != 0 && splitHandOne < 21 && splitHandOne < dealerHand && dealerHand <= 21) {winField.appendText("first hand looses");} if (splitHandOne != 0 && splitHandOne > 21) {winField.appendText("first hand busts");} if (splitHandOne != 0 && splitHandOne <= 21 && dealerHand > 21) {winField.appendText("dealer busts first hand wins");} if (splitHandOne != 0 && splitHandOne <= 21 && splitHandOne == dealerHand && dealerHand <= 21) {winField.appendText("first hand pushes");} if (splitHandTwo != 0 && splitHandTwo <= 21 && splitHandTwo > dealerHand && dealerHand < 21) {winField.appendText("\nsecond hand wins");} if (splitHandTwo != 0 && splitHandTwo < 21 && splitHandTwo < dealerHand && dealerHand <= 21) {winField.appendText("\nsecond hand looses");} if (splitHandTwo != 0 && splitHandTwo > 21) {winField.appendText("\nsecond hand busts");} if (splitHandTwo != 0 && splitHandTwo <= 21 && dealerHand > 21) {winField.appendText("\ndealer busts second hand wins");} if (splitHandTwo != 0 && splitHandTwo <= 21 && splitHandTwo == dealerHand && dealerHand <= 21) {winField.appendText("\nsecond hand pushes");} if (splitHandThree != 0 && splitHandThree <= 21 && splitHandThree > dealerHand && dealerHand < 21) {winField.appendText("\nthird hand wins");} if (splitHandThree != 0 && splitHandThree < 21 && splitHandThree < dealerHand && dealerHand <= 21) {winField.appendText("\nthird hand looses");} if (splitHandThree != 0 && splitHandThree > 21) {winField.appendText("\nthird hand busts");} if (splitHandThree != 0 && splitHandThree <= 21 && dealerHand > 21) {winField.appendText("\ndealer busts third hand wins");} if (splitHandThree != 0 && splitHandThree <= 21 && splitHandThree == dealerHand && dealerHand <= 21) {winField.appendText("\nthird hand pushes");} stage.addChild(winField); } } private function hitThisHand(handtohit:Number, apossibs:Array, hitwith:String):Number { var newcard:String = hitwith; var asofar:String = apossibs.join(""); if (asofar.indexOf("A")==-1) { if (newcard == "A") { if (handtohit + 11 > 21) {handtohit += 1;acesUsed += 1;} else {handtohit += 11;} } else {handtohit += int(newcard);} } else { var howmanyar:Array = asofar.match("A"); var numofas:Number = howmanyar.length; if (newcard != "A") { if (handtohit + int(newcard) > 21 && acesUsed < numofas) {handtohit = handtohit - 10 + int(newcard);acesUsed += 1;} else { if (handtohit + 10 + int(newcard) <=21 && acesUsed < numofas) {handtohit = handtohit + 10 + int(newcard);acesUsed += 1;} else {handtohit = handtohit + int(newcard);} } } if (newcard == "A") { if (handtohit + 1 > 21 && acesUsed < numofas) {handtohit = handtohit - 10 + 1;acesUsed += 1;return(handtohit);} if (handtohit + 1 > 21 && acesUsed > numofas) {handtohit = handtohit + 1;} if (handtohit + 11 >21 && handtohit + 1 <= 21) {handtohit = handtohit +1;} if (handtohit + 11 <= 21) {handtohit = handtohit + 11;} } } return(handtohit); } private function splitHands(firstcard:String, secondcard:String):void { if (firstcard == "A" && secondcard == "A") {acesUsed -= 1;} splitCardOne = firstcard; splitCardTwo = giveCard(); splitHandOne = calculateTwoCardHand(splitCardOne, splitCardTwo); splitCardThree = secondcard; splitCardFour = giveCard(); splitHandTwo = calculateTwoCardHand(splitCardThree, splitCardFour); showSplit(firstcard, splitCardTwo, secondcard, splitCardFour); playerTXTField.appendText("\nsplit and got cards: "+splitCardTwo+" "+splitCardFour+""); playerTXTField.appendText("\nthe new hands are: "+splitHandOne+" "+splitHandTwo+""); } private function splitHandsAgain(firstcard:String, secondcard:String, whichspot:String):void { if (firstcard == "A" && secondcard == "A") {acesUsed -= 1;} var theonesplitre:Number = 0; splitCardFive = giveCard(); splitCardSix = giveCard(); if (whichspot == "one") {splitHandOne = calculateTwoCardHand(splitCardOne, splitCardFive);theonesplitre = splitHandOne; splitHandThree = calculateTwoCardHand(splitCardTwo, splitCardSix); showSplitThree(splitCardOne, splitCardFive, splitCardTwo, splitCardSix);} if (whichspot == "two") {splitHandTwo = calculateTwoCardHand(splitCardThree, splitCardFive);theonesplitre = splitHandTwo; splitHandThree = calculateTwoCardHand(splitCardFour, splitCardSix); showSplitTwo(splitCardThree, splitCardFive, splitCardFour, splitCardSix);} playerTXTField.appendText("\nsplit again and got cards: "+splitCardFive+" "+splitCardSix+""); playerTXTField.appendText("\nthe resplit hand is now: "+theonesplitre+""); playerTXTField.appendText("\nthe new hand is now: "+splitHandThree+""); } private function hit(event:MouseEvent):void { /* whichhit can be more than one type of object */ var whichhit:* = event.target; if (whichhit == firstSplitButton && playerCardOne == playerCardTwo &&
playerTXTField.text.indexOf('new  hands')==-1 && playerTXTField.text.indexOf('hit for')==-1) {splitHands(playerCardOne, playerCardTwo);return(void);} if (whichhit == secondSplitButton && splitCardTwo == splitCardOne &&
playerTXTField.text.indexOf('resplit hand')==-1 && splitCardThree!= "0" && splitCardFour!= "0" &&
playerTXTField.text.indexOf('after hit')==-1 && splitCardOne != "0" || whichhit == secondSplitButton && splitCardThree == splitCardFour &&
playerTXTField.text.indexOf('resplit hand')==-1 && splitCardThree!= "0" && splitCardFour!= "0" &&
playerTXTField.text.indexOf('after hit')==-1 && splitCardOne != "0") { if (splitCardTwo == splitCardOne) {splitHandsAgain(splitCardOne, splitCardTwo, "one");}else{splitHandsAgain(splitCardThree, splitCardFour, "two");} return(void); } if (whichhit == hitSplitOneButton && splitHandOne!=0 && playerTXTField.text.indexOf('first hand after')==-1 && playerTXTField.text.indexOf('split')!=-1 && splitHandOne < 21) { var afhcard:String = giveCard();var possibas:Array = new Array(); if (playerTXTField.text.indexOf('resplit')!=-1) {possibas = [splitCardTwo, splitCardFive];} else {possibas = [playerCardOne, splitCardTwo];} splitHandOne=hitThisHand(splitHandOne, possibas, afhcard); addHit(afhcard); playerTXTField.appendText("\nhit and got card: "+afhcard+""); playerTXTField.appendText("\nfirst hand after hit: "+ splitHandOne+""); return(void); } if (whichhit == hitSplitTwoButton && splitHandTwo!=0 && playerTXTField.text.indexOf('second hand after')==-1 && playerTXTField.text.indexOf('split')!=-1 && splitHandTwo < 21) { var ashcard:String = giveCard(); splitHandTwo=hitThisHand(splitHandTwo, [playerCardTwo, splitCardFour], ashcard); addHitTwo(ashcard); playerTXTField.appendText("\nhit and got card: "+" "+" "+" "+ashcard+""); playerTXTField.appendText("\nsecond hand after hit: "+ splitHandTwo+""); return(void); } if (whichhit == hitSplitThreeButton && splitHandThree!=0 && playerTXTField.text.indexOf('last hand')==-1 && playerTXTField.text.indexOf('resplit')!=-1 && splitHandThree < 21) { var athhcard:String = giveCard(); splitHandThree=hitThisHand(splitHandThree, [splitCardOne, splitCardSix], athhcard); addHitThree(athhcard); playerTXTField.appendText("\nhit and got card: "+" "+" "+" "+athhcard+""); playerTXTField.appendText("\nlast hand is now: "+ splitHandThree+""); return(void); } if (whichhit == hitButton && playerTXTField.text.indexOf('first hand')==-1 || whichhit == hitTXT &&
playerTXTField.text.indexOf('first hand')==-1 && playerHand < 21 && !stage.contains(winField)) { var newcard:String = giveCard(); addHit(newcard); if (playerTXTField.text.indexOf("A")==-1) { if (newcard == "A") { if (playerHand + 11 > 21) {playerHand += 1;acesUsed += 1;} else {playerHand += 11;} } else { playerHand += int(newcard); } } else { var howmanyar:Array = playerTXTField.text.match("A"); var numofas:Number = howmanyar.length; if (newcard != "A") { if (playerHand + int(newcard) > 21 && acesUsed < numofas) {playerHand = playerHand - 10 + int(newcard);acesUsed += 1;} else { if (playerHand + 10 + int(newcard) <=21 && acesUsed < numofas) {playerHand = playerHand + 10 + int(newcard);acesUsed += 1;} else {playerHand = playerHand + int(newcard);} } } if (newcard == "A") { if (playerHand + 1 > 21 && acesUsed < numofas) {playerHand = playerHand - 10 + 1;acesUsed += 1; playerTXTField.appendText("\nhit for: "+newcard+" your hand is now: "+playerHand+""); return(void);} if (playerHand + 1 > 21 && acesUsed > numofas) {playerHand = playerHand + 1;} if (playerHand + 11 >21 && playerHand + 1 <= 21) {playerHand = playerHand +1;} if (playerHand + 11 <= 21) {playerHand = playerHand + 11;} } } playerTXTField.appendText("\nhit for: "+newcard+" your hand is now: "+playerHand+""); } } private function dealerBeginHandler(event:MouseEvent):void { /* now the aces used value is for the dealer so we reset it accordingly */ if (dealerCardOne == "A" && dealerCardTwo == "A") {acesUsed = 1;}else {acesUsed = 0;} dealerSpot.removeChildAt(0); if (dealerCardOne == "A") {dealerSpot.addChildAt(new AceImage(), 0);} if (dealerCardOne == "2") {dealerSpot.addChildAt(new TwoImage(), 0);} if (dealerCardOne == "3") {dealerSpot.addChildAt(new ThreeImage(), 0);} if (dealerCardOne == "4") {dealerSpot.addChildAt(new FourImage(), 0);} if (dealerCardOne == "5") {dealerSpot.addChildAt(new FiveImage(), 0);} if (dealerCardOne == "6") {dealerSpot.addChildAt(new SixImage(), 0);} if (dealerCardOne == "7") {dealerSpot.addChildAt(new SevenImage(), 0);} if (dealerCardOne == "8") {dealerSpot.addChildAt(new EightImage(), 0);} if (dealerCardOne == "9") {dealerSpot.addChildAt(new NineImage(), 0);} if (dealerCardOne == "10") {dealerSpot.addChildAt(new TenImage(), 0);} dealerSpot.getChildAt(0).x = 0; dealerSpot.getChildAt(0).y = 0; dealerSpot.getChildAt(1).x = dealerSpot.getChildAt(0).x + 105; dealerSpot.getChildAt(1).y = dealerSpot.getChildAt(0).y; if (dealerSpot.scaleX == 1.0) {dealerSpot.scaleX /= 1.80;dealerSpot.scaleY /= 1.80;} var givedealer:String; var dealercards:Array = [dealerCardOne, dealerCardTwo]; if (dealerHand < 17) { while (dealerHand < 17) { givedealer = giveCard(); dealerHand=hitThisHand(dealerHand, dealercards, givedealer); dealercards = dealercards.concat(givedealer); addDealerHit(givedealer); if (dealerHand >= 17) {calculateWinner();break;} } } else {calculateWinner();} } private function initializeDealerHand(carderone:String, cardertwo:String):void { dealerSpot.addChild(new CardBackImage()); if (cardertwo == "A") {dealerSpot.addChild(new AceImage());} if (cardertwo == "2") {dealerSpot.addChild(new TwoImage());} if (cardertwo == "3") {dealerSpot.addChild(new ThreeImage());} if (cardertwo == "4") {dealerSpot.addChild(new FourImage());} if (cardertwo == "5") {dealerSpot.addChild(new FiveImage());} if (cardertwo == "6") {dealerSpot.addChild(new SixImage());} if (cardertwo == "7") {dealerSpot.addChild(new SevenImage());} if (cardertwo == "8") {dealerSpot.addChild(new EightImage());} if (cardertwo == "9") {dealerSpot.addChild(new NineImage());} if (cardertwo == "10") {dealerSpot.addChild(new TenImage());} dealerSpot.getChildAt(0).x = 0; dealerSpot.getChildAt(0).y = 0; dealerSpot.getChildAt(1).x = dealerSpot.getChildAt(0).x + 105; dealerSpot.getChildAt(1).y = dealerSpot.getChildAt(0).y; if (dealerSpot.scaleX == 1.0) {dealerSpot.scaleX /= 1.80;dealerSpot.scaleY /= 1.80;} } private function initializePlayerHand(carderone:String, cardertwo:String):void { if (carderone == "A") {cardSpotOne.addChild(new AceImage());} if (carderone == "2") {cardSpotOne.addChild(new TwoImage());} if (carderone == "3") {cardSpotOne.addChild(new ThreeImage());} if (carderone == "4") {cardSpotOne.addChild(new FourImage());} if (carderone == "5") {cardSpotOne.addChild(new FiveImage());} if (carderone == "6") {cardSpotOne.addChild(new SixImage());} if (carderone == "7") {cardSpotOne.addChild(new SevenImage());} if (carderone == "8") {cardSpotOne.addChild(new EightImage());} if (carderone == "9") {cardSpotOne.addChild(new NineImage());} if (carderone == "10") {cardSpotOne.addChild(new TenImage());} if (cardertwo == "A") {cardSpotOne.addChild(new AceImage());} if (cardertwo == "2") {cardSpotOne.addChild(new TwoImage());} if (cardertwo == "3") {cardSpotOne.addChild(new ThreeImage());} if (cardertwo == "4") {cardSpotOne.addChild(new FourImage());} if (cardertwo == "5") {cardSpotOne.addChild(new FiveImage());} if (cardertwo == "6") {cardSpotOne.addChild(new SixImage());} if (cardertwo == "7") {cardSpotOne.addChild(new SevenImage());} if (cardertwo == "8") {cardSpotOne.addChild(new EightImage());} if (cardertwo == "9") {cardSpotOne.addChild(new NineImage());} if (cardertwo == "10") {cardSpotOne.addChild(new TenImage());} cardSpotOne.getChildAt(0).x = 0; cardSpotOne.getChildAt(0).y = 0; cardSpotOne.getChildAt(1).x = cardSpotOne.getChildAt(0).x + 25; cardSpotOne.getChildAt(1).y = cardSpotOne.getChildAt(0).y - 25; if (cardSpotOne.scaleX == 1.0) {cardSpotOne.scaleX /= 1.1;cardSpotOne.scaleY /= 1.1;} } private function addHit(wcard:String):void { if (wcard == "A") {cardSpotOne.addChild(new AceImage());} if (wcard == "2") {cardSpotOne.addChild(new TwoImage());} if (wcard == "3") {cardSpotOne.addChild(new ThreeImage());} if (wcard == "4") {cardSpotOne.addChild(new FourImage());} if (wcard == "5") {cardSpotOne.addChild(new FiveImage());} if (wcard == "6") {cardSpotOne.addChild(new SixImage());} if (wcard == "7") {cardSpotOne.addChild(new SevenImage());} if (wcard == "8") {cardSpotOne.addChild(new EightImage());} if (wcard == "9") {cardSpotOne.addChild(new NineImage());} if (wcard == "10") {cardSpotOne.addChild(new TenImage());} cardSpotOne.getChildAt(cardSpotOne.numChildren-1).x =cardSpotOne.getChildAt(cardSpotOne.numChildren-2).x + 25; cardSpotOne.getChildAt(cardSpotOne.numChildren-1).y =cardSpotOne.getChildAt(cardSpotOne.numChildren-2).y - 25; if (cardSpotOne.scaleX == 1.0) {cardSpotOne.scaleX /= 1.1;cardSpotOne.scaleY /= 1.1;} } private function addHitTwo(wcard:String):void { if (wcard == "A") {cardSpotTwo.addChild(new AceImage());} if (wcard == "2") {cardSpotTwo.addChild(new TwoImage());} if (wcard == "3") {cardSpotTwo.addChild(new ThreeImage());} if (wcard == "4") {cardSpotTwo.addChild(new FourImage());} if (wcard == "5") {cardSpotTwo.addChild(new FiveImage());} if (wcard == "6") {cardSpotTwo.addChild(new SixImage());} if (wcard == "7") {cardSpotTwo.addChild(new SevenImage());} if (wcard == "8") {cardSpotTwo.addChild(new EightImage());} if (wcard == "9") {cardSpotTwo.addChild(new NineImage());} if (wcard == "10") {cardSpotTwo.addChild(new TenImage());} cardSpotTwo.getChildAt(cardSpotTwo.numChildren-1).x =cardSpotTwo.getChildAt(cardSpotTwo.numChildren-2).x + 25; cardSpotTwo.getChildAt(cardSpotTwo.numChildren-1).y =cardSpotTwo.getChildAt(cardSpotTwo.numChildren-2).y - 25; if (cardSpotTwo.scaleX == 1.0) {cardSpotTwo.scaleX /= 1.1;cardSpotTwo.scaleY /= 1.1;} } private function addHitThree(wcard:String):void { if (wcard == "A") {cardSpotThree.addChild(new AceImage());} if (wcard == "2") {cardSpotThree.addChild(new TwoImage());} if (wcard == "3") {cardSpotThree.addChild(new ThreeImage());} if (wcard == "4") {cardSpotThree.addChild(new FourImage());} if (wcard == "5") {cardSpotThree.addChild(new FiveImage());} if (wcard == "6") {cardSpotThree.addChild(new SixImage());} if (wcard == "7") {cardSpotThree.addChild(new SevenImage());} if (wcard == "8") {cardSpotThree.addChild(new EightImage());} if (wcard == "9") {cardSpotThree.addChild(new NineImage());} if (wcard == "10") {cardSpotThree.addChild(new TenImage());} cardSpotThree.getChildAt(cardSpotThree.numChildren-1).x =cardSpotThree.getChildAt(cardSpotThree.numChildren-2).x + 25; cardSpotThree.getChildAt(cardSpotThree.numChildren-1).y =cardSpotThree.getChildAt(cardSpotThree.numChildren-2).y - 25; if (cardSpotThree.scaleX == 1.0) {cardSpotThree.scaleX /= 1.1;cardSpotThree.scaleY /= 1.1;} } private function showSplit(bottc:String, topc:String, bottc2:String, topc2:String):void { cardSpotOne.removeChildAt(cardSpotOne.numChildren - 1); if (topc == "A") {cardSpotOne.addChild(new AceImage());} if (topc == "2") {cardSpotOne.addChild(new TwoImage());} if (topc == "3") {cardSpotOne.addChild(new ThreeImage());} if (topc == "4") {cardSpotOne.addChild(new FourImage());} if (topc == "5") {cardSpotOne.addChild(new FiveImage());} if (topc == "6") {cardSpotOne.addChild(new SixImage());} if (topc == "7") {cardSpotOne.addChild(new SevenImage());} if (topc == "8") {cardSpotOne.addChild(new EightImage());} if (topc == "9") {cardSpotOne.addChild(new NineImage());} if (topc == "10") {cardSpotOne.addChild(new TenImage());} cardSpotOne.getChildAt(cardSpotOne.numChildren-1).x =cardSpotOne.getChildAt(cardSpotOne.numChildren-2).x + 25; cardSpotOne.getChildAt(cardSpotOne.numChildren-1).y =cardSpotOne.getChildAt(cardSpotOne.numChildren-2).y - 25; if (bottc2 == "A") {cardSpotTwo.addChild(new AceImage());} if (bottc2 == "2") {cardSpotTwo.addChild(new TwoImage());} if (bottc2 == "3") {cardSpotTwo.addChild(new ThreeImage());} if (bottc2 == "4") {cardSpotTwo.addChild(new FourImage());} if (bottc2 == "5") {cardSpotTwo.addChild(new FiveImage());} if (bottc2 == "6") {cardSpotTwo.addChild(new SixImage());} if (bottc2 == "7") {cardSpotTwo.addChild(new SevenImage());} if (bottc2 == "8") {cardSpotTwo.addChild(new EightImage());} if (bottc2 == "9") {cardSpotTwo.addChild(new NineImage());} if (bottc2 == "10") {cardSpotTwo.addChild(new TenImage());} if (topc2 == "A") {cardSpotTwo.addChild(new AceImage());} if (topc2 == "2") {cardSpotTwo.addChild(new TwoImage());} if (topc2 == "3") {cardSpotTwo.addChild(new ThreeImage());} if (topc2 == "4") {cardSpotTwo.addChild(new FourImage());} if (topc2 == "5") {cardSpotTwo.addChild(new FiveImage());} if (topc2 == "6") {cardSpotTwo.addChild(new SixImage());} if (topc2 == "7") {cardSpotTwo.addChild(new SevenImage());} if (topc2 == "8") {cardSpotTwo.addChild(new EightImage());} if (topc2 == "9") {cardSpotTwo.addChild(new NineImage());} if (topc2 == "10") {cardSpotTwo.addChild(new TenImage());} cardSpotTwo.getChildAt(0).x = 0; cardSpotTwo.getChildAt(0).y = 0; cardSpotTwo.getChildAt(cardSpotTwo.numChildren-1).x =cardSpotTwo.getChildAt(cardSpotTwo.numChildren-2).x + 25; cardSpotTwo.getChildAt(cardSpotTwo.numChildren-1).y =cardSpotTwo.getChildAt(cardSpotTwo.numChildren-2).y - 25; if (cardSpotTwo.scaleX == 1.0) {cardSpotTwo.scaleX /= 1.1;cardSpotTwo.scaleY /= 1.1;} if (cardSpotThree.scaleX == 1.0) {cardSpotThree.scaleX /= 1.1;cardSpotThree.scaleY /= 1.1;} } private function showSplitTwo(bottc:String, topc:String, bottc2:String, topc2:String):void { cardSpotTwo.removeChildAt(cardSpotTwo.numChildren - 1); if (topc == "A") {cardSpotTwo.addChild(new AceImage());} if (topc == "2") {cardSpotTwo.addChild(new TwoImage());} if (topc == "3") {cardSpotTwo.addChild(new ThreeImage());} if (topc == "4") {cardSpotTwo.addChild(new FourImage());} if (topc == "5") {cardSpotTwo.addChild(new FiveImage());} if (topc == "6") {cardSpotTwo.addChild(new SixImage());} if (topc == "7") {cardSpotTwo.addChild(new SevenImage());} if (topc == "8") {cardSpotTwo.addChild(new EightImage());} if (topc == "9") {cardSpotTwo.addChild(new NineImage());} if (topc == "10") {cardSpotTwo.addChild(new TenImage());} cardSpotTwo.getChildAt(cardSpotTwo.numChildren-1).x =
cardSpotTwo.getChildAt(cardSpotTwo.numChildren-2).x + 25; cardSpotTwo.getChildAt(cardSpotTwo.numChildren-1).y =
cardSpotTwo.getChildAt(cardSpotTwo.numChildren-2).y - 25; if (bottc2 == "A") {cardSpotThree.addChild(new AceImage());} if (bottc2 == "2") {cardSpotThree.addChild(new TwoImage());} if (bottc2 == "3") {cardSpotThree.addChild(new ThreeImage());} if (bottc2 == "4") {cardSpotThree.addChild(new FourImage());} if (bottc2 == "5") {cardSpotThree.addChild(new FiveImage());} if (bottc2 == "6") {cardSpotThree.addChild(new SixImage());} if (bottc2 == "7") {cardSpotThree.addChild(new SevenImage());} if (bottc2 == "8") {cardSpotThree.addChild(new EightImage());} if (bottc2 == "9") {cardSpotThree.addChild(new NineImage());} if (bottc2 == "10") {cardSpotThree.addChild(new TenImage());} if (topc2 == "A") {cardSpotThree.addChild(new AceImage());} if (topc2 == "2") {cardSpotThree.addChild(new TwoImage());} if (topc2 == "3") {cardSpotThree.addChild(new ThreeImage());} if (topc2 == "4") {cardSpotThree.addChild(new FourImage());} if (topc2 == "5") {cardSpotThree.addChild(new FiveImage());} if (topc2 == "6") {cardSpotThree.addChild(new SixImage());} if (topc2 == "7") {cardSpotThree.addChild(new SevenImage());} if (topc2 == "8") {cardSpotThree.addChild(new EightImage());} if (topc2 == "9") {cardSpotThree.addChild(new NineImage());} if (topc2 == "10") {cardSpotThree.addChild(new TenImage());} cardSpotThree.getChildAt(0).x = 0; cardSpotThree.getChildAt(0).y = 0; cardSpotThree.getChildAt(cardSpotThree.numChildren-1).x =cardSpotThree.getChildAt(cardSpotThree.numChildren-2).x + 25; cardSpotThree.getChildAt(cardSpotThree.numChildren-1).y =cardSpotThree.getChildAt(cardSpotThree.numChildren-2).y - 25; if (cardSpotTwo.scaleX == 1.0) {cardSpotTwo.scaleX /= 1.1;cardSpotTwo.scaleY /= 1.1;} if (cardSpotThree.scaleX == 1.0) {cardSpotThree.scaleX /= 1.1;cardSpotThree.scaleY /= 1.1;} } private function showSplitThree(bottc:String, topc:String, bottc2:String, topc2:String):void { cardSpotOne.removeChildAt(cardSpotOne.numChildren - 1); if (topc == "A") {cardSpotOne.addChild(new AceImage());} if (topc == "2") {cardSpotOne.addChild(new TwoImage());} if (topc == "3") {cardSpotOne.addChild(new ThreeImage());} if (topc == "4") {cardSpotOne.addChild(new FourImage());} if (topc == "5") {cardSpotOne.addChild(new FiveImage());} if (topc == "6") {cardSpotOne.addChild(new SixImage());} if (topc == "7") {cardSpotOne.addChild(new SevenImage());} if (topc == "8") {cardSpotOne.addChild(new EightImage());} if (topc == "9") {cardSpotOne.addChild(new NineImage());} if (topc == "10") {cardSpotOne.addChild(new TenImage());} cardSpotOne.getChildAt(cardSpotOne.numChildren-1).x =cardSpotOne.getChildAt(cardSpotOne.numChildren-2).x + 25; cardSpotOne.getChildAt(cardSpotOne.numChildren-1).y =cardSpotOne.getChildAt(cardSpotOne.numChildren-2).y - 25; if (bottc2 == "A") {cardSpotThree.addChild(new AceImage());} if (bottc2 == "2") {cardSpotThree.addChild(new TwoImage());} if (bottc2 == "3") {cardSpotThree.addChild(new ThreeImage());} if (bottc2 == "4") {cardSpotThree.addChild(new FourImage());} if (bottc2 == "5") {cardSpotThree.addChild(new FiveImage());} if (bottc2 == "6") {cardSpotThree.addChild(new SixImage());} if (bottc2 == "7") {cardSpotThree.addChild(new SevenImage());} if (bottc2 == "8") {cardSpotThree.addChild(new EightImage());} if (bottc2 == "9") {cardSpotThree.addChild(new NineImage());} if (bottc2 == "10") {cardSpotThree.addChild(new TenImage());} if (topc2 == "A") {cardSpotThree.addChild(new AceImage());} if (topc2 == "2") {cardSpotThree.addChild(new TwoImage());} if (topc2 == "3") {cardSpotThree.addChild(new ThreeImage());} if (topc2 == "4") {cardSpotThree.addChild(new FourImage());} if (topc2 == "5") {cardSpotThree.addChild(new FiveImage());} if (topc2 == "6") {cardSpotThree.addChild(new SixImage());} if (topc2 == "7") {cardSpotThree.addChild(new SevenImage());} if (topc2 == "8") {cardSpotThree.addChild(new EightImage());} if (topc2 == "9") {cardSpotThree.addChild(new NineImage());} if (topc2 == "10") {cardSpotThree.addChild(new TenImage());} cardSpotThree.getChildAt(0).x = 0; cardSpotThree.getChildAt(0).y = 0; cardSpotThree.getChildAt(cardSpotThree.numChildren-1).x =cardSpotThree.getChildAt(cardSpotThree.numChildren-2).x + 25; cardSpotThree.getChildAt(cardSpotThree.numChildren-1).y =cardSpotThree.getChildAt(cardSpotThree.numChildren-2).y - 25; if (cardSpotTwo.scaleX == 1.0) {cardSpotTwo.scaleX /= 1.1;cardSpotTwo.scaleY /= 1.1;} if (cardSpotThree.scaleX == 1.0) {cardSpotThree.scaleX /= 1.1;cardSpotThree.scaleY /= 1.1;} } private function addDealerHit(card:String):void { if (card == "A") {dealerSpot.addChild(new AceImage());} if (card == "2") {dealerSpot.addChild(new TwoImage());} if (card == "3") {dealerSpot.addChild(new ThreeImage());} if (card == "4") {dealerSpot.addChild(new FourImage());} if (card == "5") {dealerSpot.addChild(new FiveImage());} if (card == "6") {dealerSpot.addChild(new SixImage());} if (card == "7") {dealerSpot.addChild(new SevenImage());} if (card == "8") {dealerSpot.addChild(new EightImage());} if (card == "9") {dealerSpot.addChild(new NineImage());} if (card == "10") {dealerSpot.addChild(new TenImage());} dealerSpot.getChildAt(dealerSpot.numChildren -1).x =
dealerSpot.getChildAt(dealerSpot.numChildren-2).x +105; dealerSpot.getChildAt(dealerSpot.numChildren -1).y =dealerSpot.getChildAt(dealerSpot.numChildren-2).y; } private function makeTXTField(fieldName:TextField, fieldTXT:String):TextField { fieldName = new TextField(); fieldName.defaultTextFormat = TXTFormat; fieldName.autoSize = TextFieldAutoSize.LEFT; fieldName.height = 20; fieldName.selectable = false; fieldName.text = fieldTXT; return(fieldName); } private function deal(event:MouseEvent):void { if (stage.contains(winField)) {winField.text = "";stage.removeChild(winField);} splitHandOne = 0; splitHandTwo = 0; splitHandThree = 0; while (cardSpotOne.numChildren > 0) { cardSpotOne.removeChildAt(cardSpotOne.numChildren-1); if (cardSpotOne == null) {break;} } while (cardSpotTwo.numChildren > 0) { cardSpotTwo.removeChildAt(cardSpotTwo.numChildren-1); if (cardSpotTwo == null) {break;} } while (cardSpotThree.numChildren > 0) { cardSpotThree.removeChildAt(cardSpotThree.numChildren-1); if (cardSpotThree == null) {break;} } while (dealerSpot.numChildren > 0) { dealerSpot.removeChildAt(dealerSpot.numChildren-1); if (dealerSpot == null) {break;} } acesUsed = 0; playerCardOne = giveCard(); playerCardTwo = giveCard(); playerHand=calculateTwoCardHand(playerCardOne, playerCardTwo); dealerCardOne = giveCard(); dealerCardTwo = giveCard(); dealerHand=calculateTwoCardHand(dealerCardOne, dealerCardTwo); update(); initializePlayerHand(playerCardOne, playerCardTwo); initializeDealerHand(dealerCardOne, dealerCardTwo); } private function update():void { dealerTXTField.text = "Dealer cards: "+ dealerCardOne + " " + dealerCardTwo + " dealer hand totals: " + dealerHand; playerTXTField.text = "Player cards: "+ playerCardOne + " " + playerCardTwo + " your hand totals: " + playerHand; } private function giveCard():String { var theCard:String = "A"; var randomNumber:Number = Math.floor(Math.random()*10); if (randomNumber == 1) {theCard ="A";} else { if (randomNumber == 0) {theCard = "10";} else {theCard = randomNumber.toString();} } return(theCard); } private function calculateTwoCardHand(cardOne:String, cardTwo:String):Number { var theHand:Number = 0; if (cardOne == "A" && cardTwo != "A") { /* only two aces could make this happen */ if (11 + int(cardTwo) > 21) {theHand = 1+ int(cardTwo);} else {theHand = 11+ int(cardTwo);} } if (cardOne != "A" && cardTwo == "A") { if (11 + int(cardOne) > 21) {theHand = 1 + int(cardOne) ;} else {theHand = 11 + int(cardOne) ;} } if (cardOne == "A" && cardTwo == "A") { theHand = 2; if (cardOne !=dealerCardOne || cardTwo !=dealerCardTwo) {acesUsed += 1;} } if (cardOne != "A" && cardTwo != "A") { theHand = int(cardOne) + int(cardTwo); } return(theHand); } } }

Compiled Result: