7 Replies Latest reply on Jul 23, 2010 3:14 PM by peterguy1

    Shuffling cards using array

    peterguy1

      Hey, everyone,

       

      I'm working on a card game that will allow users to flip the cards and drag them. I have those two functions worked out, but now I am trying to randomize the order of the deck when I load the swf using an array. I know a lot has been said about this issue on here, and I adapted some code that I found. I'm not getting error messages, but it doesn't seem to be shuffling my deck either. Am I missing something? I just have three cards at the moment (figured I'd keep it simple until I get everything worked out!). Below is the code for MemoryGame.as, my document class. Thanks in advance for your help. The shuffle function is near the bottom.

       

       

      MemoryGame.as

       

       

      package
      {
      import flash.display.MovieClip;
      import Card;
      import Boarder;
      import BlueBoard;

      public class MemoryGame extends MovieClip
      {
        private var _card:Card;
        private var _boarder:Boarder;
        private var _blueBoard:BlueBoard;
        private var _redBoard:RedBoard
       
       
       
       
        public function MemoryGame()
        {
         createCards();
        }
       
        private function createCards():void
        {
       
        for(var i:Number = 0; i < 1; i++)
        {
         _card = new Card();
         addChild(_card);
         _boarder = new Boarder();
         _card.setType(_boarder);
        }
       
        for(var j:Number = 0; j < 1; j++)
        {
         _card = new Card();
         addChild(_card);
         _blueBoard = new BlueBoard();
         _card.setType(_blueBoard);
        }
       
        for(var k:Number = 0; k < 1; k++)
        {
         _card = new Card();
         addChild(_card);
         _redBoard = new RedBoard();
         _card.setType(_redBoard);
        }
       
        var a:Array = [Boarder, RedBoard, BlueBoard];


        function shuffle():void
        {
           var b:Array = [];
           var i:int = a.length;
           var j:int;
           while(i)
        {
              i--;
              j = (Math.random() * a.length);
              b.push(a.splice(j, 1));
          }
          a = b.concat();
      }
        }
      }
      }

        • 1. Re: Shuffling cards using array
          Ned Murphy Adobe Community Professional & MVP

          I can't offer much help as far as properly creating the package goes, but it appears you are nesting a function within a function, which is not a good practice.  By itself, the shuffle function appears to work okay, so I am going to offer that I don't see where you call the function to make use of it in any of the code you show... it won't execute on its own.

          • 2. Re: Shuffling cards using array
            peterguy1 Level 1

            Hey, Ned,

             

            Thanks so much for the tips. I'm new to action script, so I make lots of errors still (like nesting functions in functions, etc.). Would you be able to show me how to change the code? Should I call the function right above the shuffle code? Or, would I be better off creating an external class for the shuffle function?

             

            Thanks so much!

             

            Peter

            • 3. Re: Shuffling cards using array
              Ned Murphy Adobe Community Professional & MVP

              The reason I can't help much with the package/class is because I am poorly versed in using them--most of my work has involved in-file/timeline coding.  But I can suggest trying... create the shuffle function as a separate private function within the class and call it right after you call the createCards() function.  You will need to declare the array outside of the createCards function so that it can get filled by the card creation and then shuffled by the shuffler.

               

              In the createCards function you probably need to add an: a.push(_card); line inside the end of each for loop to fill the array.

               

              Hopefully my offering isn't misleading.

              1 person found this helpful
              • 4. Re: Shuffling cards using array
                peterguy1 Level 1

                Thanks, Ned. I've been giving this a try, but I'm so tired I can't see straight. :  ) So, I'll be back at the drawing board tomorrow morning.

                I'll give your suggestions another try.

                 

                Thanks!

                 

                Peter

                • 5. Re: Shuffling cards using array
                  CineJason

                  here is card deck class(still needs work)  that im using for a game  i have another class that defines the playing cards

                   

                   

                  public class CardDeck {

                   


                          private var Contents:Array;
                          private var RandomNum:Number;

                   

                          public function CardDeck() {
                               //Contents = 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;
                                      }
                             
                                  }
                             
                              }
                                     
                          }
                         
                         
                      }
                  }

                  1 person found this helpful
                  • 6. Re: Shuffling cards using array
                    peterguy1 Level 1

                    Hey, Ned. Thanks for the help! I went back a bit in my project to make sure I knew what was going on where, and I seem to making some progress now.

                     

                    Thanks!

                     

                    Peter

                    • 7. Re: Shuffling cards using array
                      peterguy1 Level 1

                      Hey, Jason. Thanks for the code! I'm trying to randomize the cards using x and y coordinates at the moment, but if that doesn't work out, I'm going to give this a try.

                       

                      Thanks!

                       

                      Peter