The moose likes Beginning Java and the fly likes Card game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Card game" Watch "Card game" New topic

Card game

Dave Gomels

Joined: Jul 29, 2010
Posts: 2
First post on here so firstly hello all!
I am looking into making a card guessing game. Having 5 chances to guess the correct card
I want the user to be able to select one of 2 options either
1. Guess a card
2. Quit
with the guess a card i want them to be able to type in either S,D,C,H for the suits and 2,3,4,5,6,7,8,9,10,J,Q,K,A for the suit so when it comes up it says Guess a card and the user would input 2H for the 2 of hearts
if incorrect then the user would be told incorrect guess you have 4 guesses remaining. I have got the inputreader which i think i will need and i have tried to write the card class but i have struggled writing it. Below is the input reader
Input reader:

If anyone has any help or tips they can give me towards writing the card class and if required any others then it would be greatly appreciated!
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43905
Those methods and fields of the reading class could beneficially be declared static, as long as you are not calling them from several threads.
Write down what you think a Card class ought to look like . . .

And welcome to the Ranch
Dave Gomels

Joined: Jul 29, 2010
Posts: 2
Sorry it has taken me a while to reply.
I attempted a card class but i don't know how good it is.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19719

There's the little problem that you won't be able to fit "10" into a char, so a char wasn't the best choice to hold the value of the card.

Personally I wouldn't use a char for any of the Card's attributes. I would use some enums instead. Like this for a start:
marc weber

Joined: Aug 31, 2004
Posts: 11343

It's admirable that you're working on such a detailed class to model a card. But is that necessary for your purposes? One way to look at the game you described is guessing a value from 0 through 51.

But beyond questioning whether you need it, some feedback on the class would be:
  • The methods getValueName and getSuitName calculate and assign these variables every time the method is called. You could do this once in the constructor (if needed). Also, a switch statement might be more straight forward than multiple else if's. (Remember, chars can be used in a switch.)
  • Consider implementing Comparable, so that Card instances can be easily compared and ordered.
  • Consider overriding methods toString, equals, and hashCode.

  • "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    I agree. Here's the link:
    subject: Card game
    It's not a secret anymore!