Tuesday, July 28, 2009

How do i make a deck of cards in Java?

How do I make a deck of cards in java that I can manipulate? Using an array preferably.

How do i make a deck of cards in Java?
A card has two values, the suit and the numerical value (including ace, jack, queen, and king)





My suggestion would be to create a "Card" object that has these two values.





Then create a collection object "Cards" that maintains a group of cards, add/remove functions would be required.





I choses to call the collection "Cards" because there may be other reasons to group cards together.





Example uses of the collection:





Cards deck = new Cards();


Cards hand = new Cards();





If you want to create a deck of cards initially then create an overload to your constructor:





boolean CreateDeck = true;


boolean AddJokers = false;


Cards deck = new Cards(CreateDeck,AddJokers);





I'd also suggest creating empty hands, and adding them to an array list that you can pass to the Cards class to "Deal" the initial hands





// setup the 'empty' hands and add them to an array;


Cards[] PlayersHands = new Cards[3];


Cards Hand1 = new Cards();


PlayersHands.Add(Hand1);


Cards Hand2 = new Cards();


PlayersHands.Add(Hand2);


Cards Hand3 = new Cards();


PlayersHands.Add(Hand3);





// setup the deck


boolean CreateDeck = true;


// without jokers


boolean AddJokers = false;


Cards deck = new Cards(CreateDeck,AddJokers);


// shuffle the deck


deck.Shuffle();


// deal 5 cards to each players hand.


deck.Deal(5, PlayersHands);











feel free to email me if you have any questions
Reply:ya what he said, lol :o)


No comments:

Post a Comment