Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to represent cards?

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5334
48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5334
48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, a Hand class would be an elegant solution in these cases.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic