View Full Version : Custom objects causing me grief

08-08-2006, 08:49 PM
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.
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:

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

alert(d.cards[0].rank);//This gives me 'undefined'. I have no idea why!

The code for the Deck object is here:

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):
Absolutely any help would be greatly appreciated.

Philip M
08-09-2006, 01:55 AM
I note that you are calling a function d.shuffle();
But the name of the function is
function D_shuffle()

JavaScript is case sensitive.

08-09-2006, 04:37 AM
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. :D

Thanks anyway, Philip M

08-09-2006, 06:46 AM
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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum