A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: shuffle card game

Hybrid View

  1. #1
    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.

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