• Please go to your preferences page and make sure your "See Mature Topics" setting is set. Setting it to "Yes" means you see the Mature Topics forum (contains political and religious debates). Setting it to "No" means you do not see those threads.

Java code! Yay! Criticize!

Status
Not open for further replies.

Mesmerale

Banned
Messages
2,560
Role
Diaper Lover, Babyfur, Sissy
M'kay, so this code is made within Eclipse Europa. >.> I haven't updated it to Ganymede on this computer yet...

But it still works! And it's ready to be turned in tomorrow! Take a look. :D

The Objective

ACSL
American Computer Science League
Intermediate Division
ACSL Pinochle

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.

OUTPUT: The score for each input hand

SAMPLE INPUT
SAMPLE OUTPUT
1. D,ATKQQJ,AKQQ,KQQJN,A​
1. 4​
2. C,KQN,ATTQN,AQJ,ATKQJ​
2. 56​
3. H,ATKQJN,TJ,AQJ,ATKQJ​
3. 58​
4. S,KQJ,ATKQ,TKKQN,TKNN​
4. 12​
5. H,NNKT,NNJQ,NNAK,NTTQ​
5. 0​

:down2: This is the client (driver) for the program. :D
:down2: This is the code for the hand itself. :)
:down2: This is the code for the point system (The 12 cases)
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.
 
Last edited:

Mesmerale

Banned
Messages
2,560
Role
Diaper Lover, Babyfur, Sissy
Pinochle Client

 
Last edited:

Mesmerale

Banned
Messages
2,560
Role
Diaper Lover, Babyfur, Sissy
Hand

 
Last edited:

Mesmerale

Banned
Messages
2,560
Role
Diaper Lover, Babyfur, Sissy
Point System

 
Last edited:

IncompleteDude

Est. Contributor
Messages
1,083
Role
Private
Ahh, the good old ACSL. I came 13th overall in their 3 member senior team division one year. Hope you make it to the All-Stars too! :D
 

Mesmerale

Banned
Messages
2,560
Role
Diaper Lover, Babyfur, Sissy
Ahh, the good old ACSL. I came 13th overall in their 4 person team contest one year.
Cool! :) I haven't done anything like that. I'm just doing this for school. :D


On another note... I found an error (Well, multiple errors, but this one mostly) in the code. :(

static public boolean doubleBook(String trump, int[] diamonds, int[] clubs, int[] spades, int[] hearts)
{
if(trump.equals("D") && diamonds[0] == 2 && diamonds[1] == 2 && diamonds[2] == 2
&& diamonds[3] == 2 && diamonds[4] == 2 && diamonds[5] == 2)
return true;
else if(trump.equals("C") && clubs[0] == 2 && clubs[1] == 2 && clubs[2] == 2
&& clubs[3] == 2 && clubs[4] == 2 && clubs[5] == 2)
return true;
else if(trump.equals("S") && spades[0] == 2 && spades[1] == 2 && spades[2] == 2
&& spades[3] == 2 && spades[4] == 2 && spades[5] == 2)
return true;
else if(trump.equals("H") && hearts[0] == 2 && hearts[1] == 2 && hearts[2] == 2
&& hearts[3] == 2 && hearts[4] == 2 && clubs[5] == 2)
return true;
return false;
}

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.
 

IncompleteDude

Est. Contributor
Messages
1,083
Role
Private
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.
 

Mesmerale

Banned
Messages
2,560
Role
Diaper Lover, Babyfur, Sissy
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)?


Right now my priority in life is to make the code work.

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.)
 
Last edited:

IncompleteDude

Est. Contributor
Messages
1,083
Role
Private
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.
 

Mesmerale

Banned
Messages
2,560
Role
Diaper Lover, Babyfur, Sissy
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.
Al...Gor...Ith...Mic... Sol...Uti...On?

Update: Our teacher has just informed us ('Just' meaning three minutes ago.) that if there is a 8 Aces, then it DOES also count as 2 - 4 Aces...

So the answer to your questions are yes, they do count. >.>
 
Last edited:

valastar

Contributor
Messages
1
Role
Other
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 :eek: How exactly did you solve it? Like what method?

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

IncompleteDude

Est. Contributor
Messages
1,083
Role
Private
Al...Gor...Ith...Mic... Sol...Uti...On?

Update: Our teacher has just informed us ('Just' meaning three minutes ago.) that if there is a 8 Aces, then it DOES also count as 2 - 4 Aces...

So the answer to your questions are yes, they do count. >.>
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.
 

Mesmerale

Banned
Messages
2,560
Role
Diaper Lover, Babyfur, Sissy
Or, do this. Since I did as you commanded and made it a 2D array.

if(PointSystem.eightOfAKind(hand, 0) == true)
{
points += 120;
}
else if(PointSystem.fourOfAKind(hand, 0) == true)
{
points += 10;
}

I plan on posting the updated / revised code when I'm done with my sweep up the new threads / posts. :)
 
Status
Not open for further replies.
Top