4 Replies Latest reply on Jul 28, 2010 3:58 PM by peterguy1

    Card Deck Shuffle

    peterguy1

      Hey, everyone,

       

      I was looking for a way to shuffle a deck of cards last week and I was provided with some code to do so. I decided a straight shuffle would work better with my project than randomizing coordinates (as I was doing). I'm trying to implement the code, and I think I'm pretty close, but I'm running into a few snags. I'm using Shuffle.as as my document class, and another external class called Card.as to control the flipping and drag action of my cards. At the moment, I'm getting this message when I try to run the swf:

       

      1084: Syntax error: expecting leftbrace before Deck.

       

      I think this is indicative of an error somewhere else in my code, but I'm not sure where. Do you see anything upon first glance? Thanks in advance for your help; I really appreciate it!

       

      Peter

       

       

      Shuffle.as Code:

       

      package
      {
      import flash.display.MovieClip;
      import flash.events.MouseEvent;
      import Card;
      import Boarder;
      import BlueBoard;
      import RedBoard;
      import Snow;


      public class Card Deck
      {

        private var _card:Card;
        private var _boarder:Boarder;
        private var _blueBoard:BlueBoard;
        private var _redBoard:RedBoard;
        private var _snow:Snow;
              private var Contents:Array;
              private var RandomNum:Number;
        private var _cards:Array;


              public function CardDeck()
        {
                   //Contents = new Array();
         _cards = new Array();
        

              }
             
              public function set Cont(_parmConts)
              {
                  this.Contents = _parmConts;
              }
             
              public function get Cont()
              {
                  return this.Contents;
              }
             
             
              public function AddCard(_card):void {


                  Contents.push(_card);


              }
              ////////////////////////
              //start shuffle
              ///////////////////////
              public function Shuffle() {


                  var tempArray:Array = new Array();
                  var myCount = Contents.length;

       

                  for (var i = 0; i < myCount; i++) {
                      var rand  = Math.floor(Math.random()*Contents.length );
                      var Mytest = Contents[rand];
                      Contents.splice(rand, 1);
                      tempArray.push(Mytest);
                  }
                  for (var j = 0; j < myCount; j++) {
                      Contents[j] = tempArray[j];
                  }
              }
              /////////////////////////////////////////
              //End shuffle
              /////////////////////////////////////////

       

              private var CardPicked;
              public function DrawCard() {


                  CardPicked = Contents.shift();
                  //RemoveTopCard();
                  return CardPicked;
              }
              public function RemoveTopCard() {
                  Contents.shift();


              }
              public function DeckCount() {


                  return Contents.length;
              }
             
             
              public function ShowTopCard()
              {
                  return Contents[0];
              }
                 
              public function ShowCards(Num)
              {
                  var TempArray:Array = new Array(Num)
                  for (var i = 0 ; i< Num;i++){
                      TempArray[i] = Contents[i];
                  }
                  return TempArray;
              }
             
             
              public function RemoveCard(CardName)
              {
                  var myCount = Contents.length;
                  var RemovedOne = false;


                  for (var i = 0; i < myCount; i++) {
                     
                      trace("LoopCount: " + i );
                      if(RemovedOne == false)
                      {
                          var Mytest = Contents[i];
                          if(Mytest.CardName == CardName)
                          {
                              //remove this element
                              trace("REMOVE THIS CARD");
                              Contents.splice(i,1);
                              RemovedOne = true;
                          }
                 
                      }
                 
                  }
                         
              }
             
             
          }
      }

       

       

      Card.as Code:

       

      package
      {
      import flash.display.MovieClip;
      import flash.events.MouseEvent;

      public class Card extends MovieClip
      {
        private var _type:*;
       
        public function Card()
        {
         this.buttonMode = true;
         this.addEventListener(MouseEvent.CLICK, onClick);
         this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
         this.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
        }
       
        private function onClick(event:MouseEvent):void
        {
         this.play();
         this.removeEventListener(MouseEvent.CLICK, onClick)
        }
       
        private function mouseDownHandler(event:MouseEvent):void
        {
         var thisMC:MovieClip = event.currentTarget as MovieClip;
         thisMC.startDrag(false);
         thisMC.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
        }
       
        private function mouseUpHandler(event:MouseEvent):void
        {
         var thisMC:MovieClip = event.currentTarget as MovieClip;
         thisMC.stopDrag();
         thisMC.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
        }
       
        private function mouseMoveHandler(event:MouseEvent):void
        {
         event.updateAfterEvent();
        }
       
        public function setType(type:*):void
        {
         _type = type;
         loader_mc.addChild(_type);
        }
      }
      }

        • 1. Re: Card Deck Shuffle
          Ned Murphy Adobe Community Professional & MVP

          I can't say this is a typo or the forum throwing spaces into code (which it will often do), but if the following is what's in the file it is probably the source of the problem...

           

          public class Card Deck

          • 2. Re: Card Deck Shuffle
            peterguy1 Level 1

            Thanks, Ned. I have to remember to eliminate spaces in those!

             

            I did that, and I'm now receiving this message:

             

            "5000: The class 'Shuffle' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type."

             

            I've moved the shuffle class around a bit, but it doesn't seem to work. Have you seen this sort of thing before?

             

            Peter

            • 3. Re: Card Deck Shuffle
              Ned Murphy Adobe Community Professional & MVP

              I don't remember if it was you I mentioned it to recently or not, but Classes aren't my forte.  I recommend starting a new posting so that you have a nice "0 Replies" to act as bait.  If you post in the AS3 forum instead of here you might get a couple additional sets of helpful eyes to latch onto it, but sticking it out here will probably work as well.

               

              If I had to guess though, I'd say your Shuffle class needs to have words to the effect of "extends MovieClip" in the beginning.

              1 person found this helpful
              • 4. Re: Card Deck Shuffle
                peterguy1 Level 1

                Sounds good. Thanks, Ned!