aspose file tools*
The moose likes Game Development and the fly likes Best way to represent cards? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Best way to represent cards?" Watch "Best way to represent cards?" New topic
Author

Best way to represent cards?

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Hi,

I am trying different strategies to improve my programming and skills and figure that coding simple games would be a good area for this. I want to write a card game but am just wondering what is the best way to represent a deck of cards programmatically? Would it be an array of strings with the card name eg 2D, 3D for diamonds, 2H, 3H for hearts, and so on...

Thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

I'd have an enum for the suit, possibly one for the rank, and a Card class with a Suit and Rank value. Then I'd use an array or list of those. The code will be much more readable than just using Strings. For a start, if you use Strings then every time you want to compare suit or value you'll have to parse the string.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Matthew Brown wrote:I'd have an enum for the suit, possibly one for the rank, and a Card class with a Suit and Rank value. Then I'd use an array or list of those. The code will be much more readable than just using Strings. For a start, if you use Strings then every time you want to compare suit or value you'll have to parse the string.


Thanks! That made me realise that Im not at the stage where I am recognising that everything should be put into a class if it can be. Its just human vs computer but so far my classes are Player, Computer, Game, Hand, Deck and now Card.. thanks.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3371
    
    9
Actually, you don't even need Hand and Deck classes. Instead, let Game consist of Deque<Card> for its deck and discard piles, and Player can hold a List<Card>.

I don't usually hand out complete examples, and I don't want to take away the joy of creating your own class, but if you're interested, here is an implementation I made a while ago. Note that the class is instance-limited, so you can compare any two cards using the == operator.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Stephan van Hulst wrote:Actually, you don't even need Hand and Deck classes. Instead, let Game consist of Deque<Card> for its deck and discard piles, and Player can hold a List<Card>.


That's true, but it would be sensible to have a Hand class if there's logic you want to apply at the hand level, which means it depends on the sort of game you're writing. For instance, if you're writing a poker game, you'll want to be able to recognise the various types of hand (straight, full house, etc). I'd say a Hand class was the sensible place for that sort of logic.

As it happens, I've just finished coding a solution to this (I'm doing them as an exercise in learning a new language). All my logic took place in a Hand class there.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3371
    
    9
True, a Hand class would be an elegant solution in these cases.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to represent cards?
 
Similar Threads
Need Help in java
Displaying Playing Cards...
images in HTML/JSP
illegal start of expression
JPanel or JFrame problem?