Reputation: 13
I am a newbie and i am having some problems with structuring a solution to my new assignment. I cant figure out how add the points based on the number of cards in a suit which is the void,singleton and doubleton part of the question. I have posted the whole questions here. Can anyone please give me some ideas? I will be grateful.
In a card game, each player's hand is made up of 13 cards. Each hand has a total point value determined by the number of cards that have a point value. The cards which are worth points are the Ace (4 points), King (3 points), Queen (2 points) and Jack (1 point). The other cards (2, 3, 4, 5, 6, 7, 8, 9, 10) have no point value. There are four of each type of card, one in each of the four suits. The suits are called clubs (C), diamonds (D), hearts (H), and spades (S). As well, points are assigned for each suit which has a void (3 points), a singleton (2 points), or a doubleton (1 point). A void in a suit means that there are no cards of that suit (e.g. a hand with no spades). A singleton in a suit means that there is only one card in that suit (e.g. a hand with only one diamond). A doubleton in a suit means that there are only two cards in that suit. Write a program to read a set of thirteen cards in the form of a string, then evaluate the number of points in the hand. The suits will appear in increasing alphabetical order. Within each suit there will be no duplicate cards. The output is to be the hand and the point value shown in a table form as below. Your output should list the cards in the same order as the input. Note that 10 is represented by the character T in both the input and the output. Input is from the keyboard, output to the screen.
Sample session
Enter cards:
C258TJKD69QAHSTJA
Cards Dealt Points
Clubs 2 5 8 T J K 4
Diamonds 6 9 Q A 6
Hearts 3
Spades T J A 5
Total 18
this is what i have done so far: I cant figure out how to break my input into individual suits so that i can count the number of cards per suite and add to the points. Any help will be appreciated
for (int i=0; i<cards.length(); i++) {
letter= cards.substring(i,i+1);
if (letter.equals("C")) {
System.out.print("\nClub");
total= total + points;
points=0;
}
else if (letter.equals("D")) {
System.out.print("\nDiamonds");
total= total + points;
points=0;
}
else if (letter.equals("H")) {
System.out.print("\nHearts");
total= total + points;
points=0;
}
else if (letter.equalsIgnoreCase("S")) {
System.out.print("\nSpades");
total= total + points;
points=0;
}
else if (letter.equalsIgnoreCase("K")) {
points=points+3;
System.out.print(" K");
}
else if (letter.equalsIgnoreCase("A")) {
points=points+4;
System.out.print(" A");
}
else if (letter.equalsIgnoreCase("Q")) {
points=points+2;
System.out.print(" Q");
}
else if (letter.equalsIgnoreCase("J")) {
points++;
System.out.print(" J");
}
else {
System.out.print(" " + letter);
}
}
total=total+points;
System.out.println("\nGrand total\t" + total);
}
} }
Upvotes: 1
Views: 1480
Reputation: 51553
In real life you never get as good a set of specifications as you do in a coding class.
Take the problem description and break it up into individual sentences and think about what you have to do to satisfy each and every individual sentence.
In a card game, each player's hand is made up of 13 cards.
Ok, we're going to have to provide storage for 13 cards. A List will probably suffice.
Each hand has a total point value determined by the number of cards that have a point value.
Ok, we're going to have to calculate point values. Sounds like some math will be involved.
The cards which are worth points are the Ace (4 points), King (3 points), Queen (2 points) and Jack (1 point). The other cards (2, 3, 4, 5, 6, 7, 8, 9, 10) have no point value.
Ok, we're going to have to associate cards with points. A Map will probably suffice.
There are four of each type of card, one in each of the four suits. The suits are called clubs (C), diamonds (D), hearts (H), and spades (S).
Ok, this is a deck of playing cards. I'm familiar with a deck of playing cards.
As well, points are assigned for each suit which has a void (3 points), a singleton (2 points), or a doubleton (1 point). A void in a suit means that there are no cards of that suit (e.g. a hand with no spades). A singleton in a suit means that there is only one card in that suit (e.g. a hand with only one diamond). A doubleton in a suit means that there are only two cards in that suit.
Whoa, this is a little more complicated. How about I get the rest of the requirements satisfied, and then I can come back to this one.
Right now, it looks like I'll have to go through that List of cards and check for these conditions.
Write a program to read a set of thirteen cards in the form of a string, then evaluate the number of points in the hand. The suits will appear in increasing alphabetical order. Within each suit there will be no duplicate cards.
Ok, I'm going to have a specific String input, that I'm going to have to break up into individual cards. Maybe I should write a Card class to hold the value and the suit.
The output is to be the hand and the point value shown in a table form as below. Your output should list the cards in the same order as the input. Note that 10 is represented by the character T in both the input and the output.
Ok, I know what the output is supposed to look like. I'd better be careful with the T (10). I definitely have to use a List to hold the Card instances, since I have to maintain input order.
Input is from the keyboard, output to the screen.
I can use Scanner for the input and System.out for the output. I know how to do that.
I can't wait to get started!
And that is how you decompose a problem into small enough steps so that you can solve the problem.
Upvotes: 1