Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2006
    Location
    Montreal, Canada
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Custom objects causing me grief

    Hello, this is my first post here, and i have a tricky question.
    I'm trying to make a javascript poker game (for fun) and have created my own classes to help: Card, Deck, Hand.

    The Card class mainly just holds information about the rank and suit of the card.
    example:
    var c = new Card(2, 'h');
    c.rank is 2
    c.suit is 'h'

    The problem is with the Deck object. It has an array called 'cards' that holds Card objects. I have a function called shuffle (to randomize the order of the cards in the array) and this function is causing the problem.

    The problem can be illustrated by the following:
    Code:
    var d = new Deck();
    alert(d.cards[0].rank); //this is ok. It outputs "2" since i create the 
    // 'cards' array from lowest to highest cards
    
    d.shuffle();
    alert(d.cards[0].rank);//This gives me 'undefined'. I have no idea why!
    The code for the Deck object is here:
    Code:
    function Deck()
    {
    	this.cards = new Array();//will hold Card objects
    	var suits = new Array();
    	suits[0] = "h";
    	suits[1] = "s";
    	suits[2] = "c";
    	suits[3] = "d";
    	
    	var count;
    	for( count = 2; count <= 14 ; count++)
    	{
    		for( suit in suits )
    		{
    			var c = new Card(count, suits[suit]);
    			this.cards.push( c );
    		}	
    	}
    	this.shuffle = D_shuffle;
    	this.drawCard = function(){ return this.cards.shift(); }
    	this.numCards = function() { return this.cards.length; }
    
    //shuffle cards in Deck object
    function D_shuffle()
    {
    	var count;
    	this.cards = new Array();
    	var tmpdeck = new Deck();
    	for( count = tmpdeck.numCards(); count > 0; count-- )
    	{
    		this.cards.push( tmpdeck.cards.splice(rnd(count), 1) );
    //function rnd(NUM) returns random number between 0 and NUM (not including NUM)
    	}
    	//even trying to access this.cards[0].rank from here gives 'undefined'
    }
    } //end of Deck object
    I have a feeling it's something to do with the arrays but I don't know javascript well enough yet.
    The full source code (.js file) is available here (please excuse the lack of in depth comments):
    http://ganesh.ugrad.physics.mcgill.c...er/js/Poker.js
    Absolutely any help would be greatly appreciated.
    thanks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    I note that you are calling a function d.shuffle();
    But the name of the function is
    function D_shuffle()

    JavaScript is case sensitive.

  • #3
    New Coder
    Join Date
    Aug 2006
    Location
    Montreal, Canada
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M
    I note that you are calling a function d.shuffle();
    But the name of the function is
    function D_shuffle()

    JavaScript is case sensitive.
    actually the constructor for the Deck object assignes D_shuffle to this.shuffle and in my example i created a Deck object called 'd', which may have been misleading.

    Actually I solved the problem. For some reason I thought that the array function splice() returned the removed element. But it doesn't. And i was catching the return value of that function thinking it was returning the Card. I was wrong. I fixed it and now it's working nicely.

    Thanks anyway, Philip M

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Careful how you shuffle. There are 52 cards to a deck, so consider some random ordering of the deck. There are 52 possibilities for the first card, 51 for the second, and so on. If you always randomly swap a card into positions 1-52, then there are 52^52 possibilities of how you shuffled, when in fact you should be getting only 52!. Now, if we're lucky, all the "repeated" distributions are uniformly distributed across the possibilities. However, 52^52 modulo 52! is 10777503050828868663485472229826764300032615709995954391232907575296, a rather large number. Dividing it by 52!, we get approximately 0.133619, or 13.36% of all possible deck configurations which *must* have some sort of bias to them.

    In other words, always randomly selecting a position from 1-52 will generate a bias in your shuffling algorithm. You need to select 1-52 for the first card, then 2-52 for the second, then 3-52 for the third, and so on.


  •  

    Posting Permissions

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