4 Replies Latest reply on Nov 18, 2006 8:24 PM by Rothrock

    randomness (but not quite)

    wullie_big_g
      hi there! this is my first post but hopefully not my last!

      im having trouble in Flash 8 with some actionscript.
      when a button is pushed i want to select a random playing card from a standard deck of 52.
      i have managed to do this successfully so far, with each card being a single frame in the timeline.
      heres the problem: when a card is selected i want to make sure it is not selected again. imagine like a game of pairspairs where a card is turned over and cant be used again.

      basically i want flash to "remember" the card has been turned. ive tried to set a variable that means the timeline can stop at a card when the variable is 0, but skips that card if the variable is anything else. but that didnt work so any ideas?

      i know theres probably something already out there that can do this easily, or some sample Actionscript that will do exactly what i want, but i'd rather learn for myself. so please help!!!
        • 1. Re: randomness (but not quite)
          Level 7
          Wullie,

          The logic: store all your cards in a temporary array that you draw from on a
          random basis, each time you pull a card, you cut that card out of the array
          so it doesn't get picked again.

          Code:

          var deckSize = 52 ;
          var myDeck = new Array() ;

          for ( var card = 1 ; card <= deckSize ; card++ ) {
          myDeck.push(card) ;
          }

          function getRandomCard() {
          var randomID = Math.floor(Math.random()*myDeck.length) ;
          var randomCard = myDeck[randomID];
          myDeck.splice(randomID, 1) ;
          return randomCard ;
          }


          for ( var k = 0 ; k < deckSize ; k++ ) {
          trace("[" + k + "]\t" + getRandomCard()) ;
          }


          "wullie big g" <webforumsuser@macromedia.com> wrote in message
          news:ejntup$7c9$1@forums.macromedia.com...
          > hi there! this is my first post but hopefully not my last!
          >
          > im having trouble in Flash 8 with some actionscript.
          > when a button is pushed i want to select a random playing card from a
          > standard
          > deck of 52.
          > i have managed to do this successfully so far, with each card being a
          > single
          > frame in the timeline.
          > heres the problem: when a card is selected i want to make sure it is not
          > selected again. imagine like a game of pairspairs where a card is turned
          > over
          > and cant be used again.
          >
          > basically i want flash to "remember" the card has been turned. ive tried
          > to
          > set a variable that means the timeline can stop at a card when the
          > variable is
          > 0, but skips that card if the variable is anything else. but that didnt
          > work so
          > any ideas?
          >
          > i know theres probably something already out there that can do this
          > easily, or
          > some sample Actionscript that will do exactly what i want, but i'd rather
          > learn
          > for myself. so please help!!!
          >


          • 2. Re: randomness (but not quite)
            Rothrock Level 5
            This is a popular type of question. Use the real world model of a deck of cards and think about how that works. You shuffle/randomize the cards, take a card from the top, and then what – do you leave it in the deck, but "marked" or do you remove it from the deck? Then you take the next one and so on.

            So the key is to use a stand in for the deck – an array – randomize it and then just take one at a time. TADA no repeats!

            Search the forum for the shuffle prototype.
            • 3. Re: randomness (but not quite)
              wullie_big_g Level 1
              thanks a lot! hopefully i'll try this and see what i can get going.

              and also thanks for replying at all. a lot of forums ive used for my problems with flash and most mock you for having problems that mean nothing to them. i could probably just copy and paste code that would do what i want cuz like you said, its a common problem. but you guys spelled it out, and i much prefer that cuz i dont wnat to steal other peoples stuff, i want to learn to do it myself.

              cheers very much!
              • 4. Re: randomness (but not quite)
                Rothrock Level 5
                Poke around a bit and come on back if you hit a snag.

                I've always found these forums to be quite friendly and helpful – especially to those who put in some effort and try to help themselves.