A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: shuffle card game

  1. #1
    Junior Member
    Join Date
    Apr 2004
    Posts
    10

    shuffle card game

    Hi all! I am posting an .fla of what I am trying to do, perhaps someone can help me figure it out.

    I am creating a card game, and at the end of the game if the player wants to replay the game, I want my

    cards to shuffle. What I have managed so far in this demo file, is when the game starts the movie clips

    are placed in certain positions on the stage, and when you press the button instead of shuffeling...all

    movie clips are placed behind the other and they all move along. What I want instead is on each mouse

    press, the cards to swap places and only one movie clip per place! Appreciate anyone's wisdom!

    P.s. I HAVE flash Mx...NOT...flash MX 2004.
    Attached Files Attached Files

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Here's a modified version of your code that will do it. I was able to shorten your code a bit by using the same function for shuffling the cards to perform the initial placement.

    code:

    // Some Useful constants - better to encode these up here
    // than to have them as literals inside your code...
    kLeftMargin = 80;
    kTopMargin = 80;
    kCellWidth = 70;
    kCellHeight = 70;
    kNbrColumns = 4;
    kNbrRows = 4;
    kTotalCards = kNbrColumns*kNbrRows;

    // The basic trick:
    // You can convert a single number n to a pair of card indexes (x,y)
    // using x = (n % kNbrColumns) and y = int(x/kNbrColumns)
    // I use this technique a couple of times below.

    // Function to place cards in random order
    ShuffleAndDeal = function()
    {
    // create an array of card indexes
    shuffleOrder = [];
    for (var i = 0; i < kTotalCards; ++i) {
    shuffleOrder[i] = i;
    }
    // shuffle the array using a random sort
    shuffleOrder.sort(function(){ return random(2)? -1 : 1});
    trace("Order: " + shuffleOrder);

    // use the array to select cards
    for (var i = 0; i < kTotalCards; ++i)
    {
    r = shuffleOrder[i];

    // card index
    // note: if your cards were numbered consecutively (0,1,2) then you
    // could simply use 'r' and don't need to compute rx,ry
    rx = r % kNbrColumns;
    ry = int(r/kNbrColumns);

    // position on board
    ix = i % kNbrColumns;
    iy = int(i/kNbrColumns);
    piece = _root[ry+"-"+rx];
    piece._x = kLeftMargin + iy*kCellWidth;
    piece._y = kTopMargin + ix*kCellHeight;
    }
    }

    stop();
    ShuffleAndDeal(); // initial placement

    press_mc.onRelease = function() {
    ShuffleAndDeal();
    }




    - Jim Bumgardner

    EDIT: I've modified the code a few times as I thought of optimizations.
    Last edited by jbum; 09-28-2004 at 05:11 PM.

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    You may enjoy the following card trick, which I made in Flash a few weeks ago.

    Mind Power

    Instructions.

    A wheel of cards will appear. Mentally choose one of the cards, and also choose a 4 letter word of your choosing.

    Let's say you choose the 7 of hearts, and the word "BARN".

    Using the handle, rotate the wheel so that your card is under the first letter of your word. So you'd rotate it so the seven of hearts is under the B.

    Then press the red-button in the middle.

    At this point, there's no way to know either your card, or the word chosen, since each card is under a different letter.

    Then rotate the wheel so the seven is under the "A" in "BARN", and press the button again.

    Repeat for the remaining two letters.

    You'll be astonished at what happens next.

    - Jim

  4. #4
    Jolene
    Join Date
    Jul 2005
    Posts
    1

    You rocks!

    Jbum,

    your code for shuffling an array rocks!!! I have been working it for so long, yet cannot find an answer... but i modified your codes and place it into my application and it works!!! Cool !!!

  5. #5
    Junior Member
    Join Date
    Mar 2008
    Posts
    10
    Great code, Jim.

    Almost bang-on what I was looking for. My cards are 2 frame movieclips. Frame 1 is the "backing", Frame 2 is the "face". They start out face down and when clicked on, the cards switch to Frame 2 (showing their value).

    With your code above, it rearranges the order of the cards (which is great) but it doesn't flip the ones showing Frame 2 back to Frame 1.

    Is there a way to do it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center