jpalfree
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.
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:
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:
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.ca/~caffeine/poker/js/Poker.js
Absolutely any help would be greatly appreciated.
thanks.
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:
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:
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.ca/~caffeine/poker/js/Poker.js
Absolutely any help would be greatly appreciated.
thanks.