dcsimg
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Help me shuffle 22 card deck in AS3 Please

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    19

    Help me shuffle 22 card deck in AS3 Please

    Hello,

    I want to create and randomize an array containing twenty two numbers, each of which will represent a different card. The cards should not repeat. I have attempted a number of solutions and have been getting nowhere. I've tried to do this using only push, splice, and a for/while loops but I think the real solution lies in the sort() method, which unfortunately I don't understand at this point.

    If someone could whip up some sample code for me, I would be eternally grateful. The solution needs to be in actionscript 3.0.

    Thanks for the help (and Merry Christmas!),
    IronGleet

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Give this a try:

    PHP Code:
    var a:Array = [123, ...];


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

    trace(a)
    shuffle();
    trace(a

  3. #3
    Junior Member
    Join Date
    Nov 2008
    Posts
    19

    actionscript 3 equivalent?

    Thanks -- this is probably a good template for me to work from. I need to do this in actionscript 3.0 though, dunno how similar the array methods are, but it's great starting point.

    You wouldn't happen to have an AS3 equivalent though onhand?

    Thanks,
    IronGleet

  4. #4
    Junior Member
    Join Date
    Nov 2008
    Posts
    19
    WOW!!

    Nix the above reply... this code worked right off with minimal tweaks!!

    Are php and as3 typically quite similar in other respects? Or did we just luck out on this one?

    Thanks and enjoy your christmas neznein9!
    IronGleet

  5. #5
    Junior Member
    Join Date
    Nov 2008
    Posts
    19

    New Problem - Dealing/layout from shuffled Array

    Ok -- thanks to your generous help, I've got the cards shuffled in an array. The code below is intended to accomplish the following:
    1.) Shuffle the cards
    2.) Utilize the dealing functions to place three cards in a row in the upper and lower right screen corners, center, and upper and lower left hand corners.
    I had planned on simply using a[i] < with corresponding adjustments to the for loop in each function > but now I'm seeing that when I do that, i also being used to calculate layout -- everything is thrown off.

    Any advice? If I havn't explained myself adequately, let me know and I'll try to do a better job. To any interested in helping me, I can e-mail you the .fla too.

    Thanks,
    IronGleet


    //initialize some variables

    var a:Array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];


    // deal the trumps
    dealUpperRight();
    dealLowerRight();
    dealCenter();
    dealTopLeft();
    dealLowerLeft();
    shuffle();
    trace(a);

    // deal the upper right three trumps
    function dealUpperRight():void {
    for (var i:int=0; i<2; i++) {
    var trump:Trumps=new Trumps();
    trump.gotoAndStop(a[i]);
    trump.addEventListener(MouseEvent.CLICK, fliptrump);
    trump.x = (30 + trump.width * i);
    trump.y=51;
    addChild(trump);


    }
    }
    // deal the lower right three trumps
    function dealLowerRight():void {
    for (var i:int=2; i<5; i++) {
    var trump:Trumps=new Trumps();
    trump.gotoAndStop(a[i]);
    trump.addEventListener(MouseEvent.CLICK, fliptrump);
    trump.x = (30 + trump.width * i);
    trump.y=399;
    addChild(trump);

    }
    }
    // deal the center three trumps
    function dealCenter():void {
    for (var i:int=05; i<8; i++) {
    var trump:Trumps=new Trumps();
    trump.gotoAndStop(a[i]);
    trump.addEventListener(MouseEvent.CLICK, fliptrump);
    trump.x = (265 + trump.width * i);
    trump.y=225;
    addChild(trump);
    }

    }
    // deal the lower left trumps
    function dealLowerLeft():void {
    for (var i:int=9; i<12; i++) {
    var trump:Trumps=new Trumps();
    trump.gotoAndStop(a[i]);
    trump.addEventListener(MouseEvent.CLICK, fliptrump);
    trump.x = (503 + trump.width * i);
    trump.y=399;
    addChild(trump);
    }

    }
    // deal the top left trumps
    function dealTopLeft():void {
    for (var i:int=13; i<15; i++) {
    var trump:Trumps=new Trumps();
    trump.gotoAndStop(a[i]);
    trump.addEventListener(MouseEvent.CLICK, fliptrump);
    trump.x = (503 + trump.width * i);
    trump.y=51;
    addChild(trump);

    }
    }
    function fliptrump(evt:MouseEvent):void {
    trace("flip trump");

    }


    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();
    }

  6. #6
    Junior Member
    Join Date
    Nov 2008
    Posts
    19
    forget to add: The trumps are laid out in a movie clip, 21 of them + a card backside, one per frame.

  7. #7
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Hey - first in regard to your earlier post - I was writing in AS3, I just use the [ php ] ... [/ php ] tags to show the code better (since we don't have tags specifically for AS).

    For the dealing - I would use the unshift() or pop() functions on the deck to remove a card from the deck and then pass that card to your custom functions for placement and 'flipping'.

  8. #8
    Junior Member
    Join Date
    Nov 2008
    Posts
    19

    creepy code puzzle

    Thanks Nez --no wonder that code worked so well :>

    I've just about worked out my tarot shuffle and deal routines. A spook still haunts my code though. Whenever element zero in the "a" array -- which happens to be 'The Fool' -- appears, it always appears twice. No other element does this and the fool does not always appear. So the code appears to work -- that is, until the fool appears, and the fool always appears twice. I know the problem is not your sort function -- I've traced it out and even though 0, the fool, when it does appear, appears only once in the a array -- still, it appears twice onscreen. I am stumped.

    HELP!

    //initialize some variables
    // main array - holds all 22 shuffled trump cards
    var a:Array=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, 20,21];
    // sub arrays -- each holds 3 elements from a 1,2,3 - 4,5,6....
    var lowerRightArray:Array;
    var upperRightArray:Array;
    var centerArray:Array;
    var lowerLeftArray:Array;
    var upperLeftArray:Array;

    // shuffle and deal function calls
    shuffle();
    dealUpperRight();
    dealLowerRight();
    dealCenter();
    dealUpperLeft();
    dealLowerLeft();


    // deal the upper right three trumps
    function dealUpperRight():void {
    for (var i:int=0; i<3; i++) {
    var trump:Trumps=new Trumps();
    trump.gotoAndStop(upperRightArray[i]);
    trump.addEventListener(MouseEvent.CLICK, fliptrump);
    trump.x = (30 + 70 * i);
    trump.y=51;
    addChild(trump);
    }
    }
    // deal the lower right three trumps
    function dealLowerRight():void {
    for (var i:int=0; i<3; i++) {
    var trump:Trumps=new Trumps();
    trump.gotoAndStop(lowerRightArray[i]);
    trump.addEventListener(MouseEvent.CLICK, fliptrump);
    trump.x = (30 + trump.width * i);
    trump.y=399;
    addChild(trump);
    }
    }
    // deal the center three trumps
    function dealCenter():void {
    for (var i:int=0; i<3; i++) {
    var trump:Trumps=new Trumps();
    trump.gotoAndStop(centerArray[i]);
    trump.addEventListener(MouseEvent.CLICK, fliptrump);
    trump.x = (265 + trump.width * i);
    trump.y=225;
    addChild(trump);
    }

    }
    // deal the lower left trumps
    function dealLowerLeft():void {
    for (var i:int=0; i<3; i++) {
    var trump:Trumps=new Trumps();
    trump.gotoAndStop(lowerLeftArray[i]);
    trump.addEventListener(MouseEvent.CLICK, fliptrump);
    trump.x = (503 + trump.width * i);
    trump.y=399;
    addChild(trump);
    }

    }
    // deal the top left trumps
    function dealUpperLeft():void {
    for (var i:int=0; i<3; i++) {
    var trump:Trumps=new Trumps();
    trump.gotoAndStop(upperLeftArray[i]);
    trump.addEventListener(MouseEvent.CLICK, fliptrump);
    trump.x = (503 + trump.width * i);
    trump.y=51;
    addChild(trump);

    }
    }
    // card flip routine -- plus sound
    function fliptrump(evt:MouseEvent):void {
    trace("flip trump");

    }
    // this function shuffles the 22 trump cards into array "a"
    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();
    trace(a);
    // place the shuffled cards into the five sub arrays
    // declared uptop
    upperRightArray=[a[1],a[2],a[3]];
    lowerRightArray=[a[4],a[5],a[6]];
    centerArray=[a[7],a[8],a[9]];
    upperLeftArray=[a[10],a[11],a[12]];
    lowerLeftArray=[a[13],a[14],a[15]];

    }

    Thanks again for all your help,
    IronGleet

  9. #9
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    I don't see anything wrong in the code you posted - is there any other code anywhere?

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Smile More info, Please?

    Hi There,
    I'm a bit slow on the uptake, but am having a crack at actionscript after quite some time away (5 years). I'm trying to shuffle and display some cards, sounds similar (although not Tarot). Your posts have helped a lot, but I'm missing the fundamentals of the movie clip organisation. Is it possible to have a look at the fla file?
    Thanks... if you're still out there ...

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