Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 7 of 7

Thread: Help needed with a card shuffling function

  1. #1

    Help needed with a card shuffling function

    Hello all! I'm trying to make a game that makes primary use of a deck of cards, but right now I'm having trouble making the deck random. My attempted method is to pop or shift (based on a random roll) the contents of array1 into array2 and back for a total of about 5 swaps ultimately ending with a nicely randomized array1 in array1. I've tried a couple different methods, but it always ends up a direct copy of array1 in array2.

    function shuffle(){
    for(i=0; i<=5; i++){
    if(i/2 % 0){
    swap = 1
    }else{
    swap = 2
    }
    if(swap=1){
    while(array1.length > 0){
    num = random(2);
    if(num=0){
    array2[array2.length] = array1.pop();
    }else if(num=1){
    array2[array2.length] = array1.shift();
    }
    }
    }else if(swap=2){
    while(array2.length > 0){
    num = random(2);
    if(num=0){
    array1[array1.length] = array2.pop();
    }else if(num=1){
    array1[array1.length] = array2.shift();
    }
    }
    }
    }
    }
    If anyone could tell me how to fix this code, or even better a more efficient way to write a shuffle function, I'd be greatly appreciative. Please do excuse the mess, this is my first attempt at a real program.

  2. #2
    150
    posts
    Registered User
    here is one of many methods to suffle, not the best...

    Code:
    var arr:Array = new Array("jedan", 
    						  "dva", 
    						  "tri", 
    						  "cetiri", 
    						  "pet", 
    						  "sest", 
    						  "sedam", 
    						  "osam", 
    						  "devet", 
    						  "deset");
    
    var sArr:Array = new Array();
    
    trace("Array : " + arr);
    sArr = shuffle(arr);
    trace("Shuffle : " + sArr);
    
    function shuffle(arr:Array) : Array {
    	var tmp:Array = new Array();
    	while(arr.length > 0) {
    		var rnd:int = Math.random() * arr.length;
    		tmp.push(arr.splice(rnd,1));
    	}
    	return tmp;
    }
    Everything is possible....

  3. #3
    For a deck of 52 cards, here's what I use:
    PHP Code:
    _root.cardcount=1;
    cards=new Array();
    used=new Array();
    for(
    c=1;c<53;c++){
        
    newcard Math.ceil(Math.random() * 52);
        while(
    used[newcard]) {
            
    newcard Math.ceil(Math.random() * 52);
        }
        
    used[newcard]=true;
        
    cards[c]=newcard;


  4. #4
    Code:
    deck = new Array("1C", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "1D", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "1D", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "1H", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "1S", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS") 
    deck.sort(function () {return Math.round(Math.random());});
    trace (deck);
    2006-11 GlosRFC - Searching 8,168,684,336 brain cells

  5. #5
    Glos....my sox are officially rox'd. Thats exactly what I was looking for! Thanks

  6. #6
    Nice bit of code glos, very simple.

  7. #7
    207
    posts
    Registered User
    The downside of glosrfc's method is that not all card orders are equally likely.

    I use something more like kosko's example, because I know all results are equally likely.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012