Problem: Pinochle uses a deck of 48 cards that is dealt out to the players (16 cards each). A deck has 2 of each type of card (Ace, King, Queen, Jack, 10, 9) per suit (diamonds, clubs, spades, hearts). The first step in playing the game is to count the number of points in a hand using the following rules:

The same card can be used in multiple cases but only once in each case. If a hand has two Jacks of diamonds and two Queens of spades, those cards could be used in Case #2 and Case #8 to accumulate 38 points.

INPUT: There will be 5 input lines each representing the cards in one hand. Each input line will contain 5 strings. The first string will be one-character in length and will give the trump suit (D, C, S or H) The other 4 strings name the cards in each suit the player has been dealt. The order will always be diamonds, clubs, spades and hearts. The card names used will be A (Ace), K (King), Q (Queen), J (Jack), T (Ten) and N (Nine). There will be at least one card in each suit.

The spacing between lines sucks because the indent command is stupid, but otherwise, everything is exactly as it is in my program.

I knowwwww I should have comments in the code!! I really really really really know I should, but by the time I remembered, I'd already finished indenting it into this and I was NOT gonna go through that again. I'll add them to the posts that I reserve after this.

Banned

Scanner keyboard = new Scanner(System.in);
String input = new String(), command = new String(); // input is used to send the player's hand from the client to the hand.
// command is used to define what should be done next: End the program or input a hand.
System.out.print("Please enter a command: ");
if(keyboard.next().equalsIgnoreCase("Input_Hand")) // String marker that will cause the program to enter a loop.
{

// This method runs through every case to test if it is present in the player's hand or not. If so, it adds the points, if not, it moves on to the next case with points remaining unchanged.
private void tallyPoints()
{

These parts are unnecessary to the code and could mess up the output under certain conditions. I didn't catch it because the five example inputs weren't affected by it. But if my teacher's inputs had the certain condition, the point value would have been very wrong.

Usually there are many more testing inputs than sample inputs, enough to catch almost every case, so yeah, it would have been a problem.

Personally, I think your points system is generally ugly, and error prone. It's just too verbose, there are simpler ways of solving the problem. For example, both the 4 and 8 card cases can be coded as a single function that takes the desired card (ace, queen, etc...) as a parameter. You've just cut down on the chances of errors by 1/4. You combine the resulting two functions again by adding another parameter for how many of each you need (integer 1 or 2). Again, the 4 individual tests in the book case can be combined by changing your hand array into a 2D one that goes "hand[suit][card]=amount", then for the single book going through each suit in a loop, and for the double just checking the trump suit. All this and other refactorings can greatly reduce your code size and therefore the chance of errors.

You should always watch for lines of code that look almost identical. If you see them, it almost always means there is a better way, imho.

I wonder, if you have a non-trump double book, does it count as 2 single books and therefore 50+50=100 points? Have you dealt with cases were multiple of the same case are present (without reusing cards of course), or is that not allowed? Again, say you had 8 aces, does that also count as 2 four ace cases? Or does the 8 ace case overrule the 4 ace one (i.e. you must only have 1 ace in one of the suits for the 4 ace case to be valid)?

EDIT:

Haha those stupid Americans! They separated the international schools into their own division this year, because they keep beating the Americans. What a bunch of sore losers. I'm glad to see my high school no longer participates in the ACSL in light of this.

Banned

Usually there are many more testing inputs than sample inputs, enough to catch almost every case, so yeah, it would have been a problem.

Personally, I think your points system is generally ugly, and error prone. It's just too verbose, there are simpler ways of solving the problem. For example, both the 4 and 8 card cases can be coded as a single function that takes the desired card (ace, queen, etc...) as a parameter. You've just cut down on the chances of errors by 1/4. You combine the resulting two functions again by adding another parameter for how many of each you need (integer 1 or 2). Again, the 4 individual tests in the book case can be combined by changing your hand array into a 2D one that goes "hand[suit][card]=amount", then for the single book going through each suit in a loop, and for the double just checking the trump suit. All this and other refactorings can greatly reduce your code size and therefore the chance of errors.

You should always watch for lines of code that look almost identical. If you see them, it almost always means there is a better way, imho.

I wonder, if you have a non-trump double book, does it count as 2 single books and therefore 50+50=100 points? Have you dealt with cases were multiple of the same case are present (without reusing cards of course), or is that not allowed? Again, say you had 8 aces, does that also count as 2 four ace cases? Or does the 8 ace case overrule the 4 ace one (i.e. you must only have 1 ace in one of the suits for the 4 ace case to be valid)?

But when I start to tweak it to make it better, I'll definitely keep all of this in mind. Thank you.

I determined this stuff based on the sample inputs and outputs.

I'm pretty sure that I determined that a non-trump double book does NOT make two single books.

For the most part, any multiple of the same case is handled by the fact that there is an entirely separate case to account for it.

Which leads to the next question. 8 Aces overrules 4 Aces.

But this is only the intermediate division.

The senior takes these questions into account, and also adds two new cases!

(Note: I finished the Senior division a few hours ago! Yay! The code is a copy of this with some tweaking, so it's just as inefficient as this one. The sample input testing of the Senior is also how I found the error in the Intermediate.)

Yeah, I understand what you're saying, but as the problems get harder, iterating through every input and mapping it to an output is impossible. You need an algorithmic solution, so it's good to think about.

Banned

Yeah, I understand what you're saying, but as the problems get harder, iterating through every input and mapping it to an output is impossible. You need an algorithmic solution, so it's good to think about.

Hey, I'm not participating in ACSL (I think I'm the only one interested in my school... You need a 3 person team...) But I do have a friend who gives me the programming prompts after the contest is over

I was trying the Senior prompt a couple days ago (the rules are sorta similar to the ones that you have on the top) and it's pretty difficult How exactly did you solve it? Like what method?

btw, the intermediate version I made was pretty short when compared to yours A lot more loops though ^.^

Ok, so you're going to have to do something like this: For each case, test it once. If it's true, remove the cards involved in the first test from the hand, and test again. Continue until the test fails. Then you have a count for each case of how many times it occurs. Finally, restore the hand to its original state.