Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Java code! Yay! Criticize!

  1. #1
    Mesmerale

    Cool Java code! Yay! Criticize!

    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.

    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.


    :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 by Mesmerale; 12-Feb-2009 at 03:49. Reason: Addition of real quotes

  2. #2
    Mesmerale

    Default Pinochle Client

    Last edited by Mesmerale; 12-Feb-2009 at 04:00.

  3. #3
    Mesmerale

    Default Hand

    Last edited by Mesmerale; 12-Feb-2009 at 04:15.

  4. #4
    Mesmerale

    Default Point System

    Last edited by Mesmerale; 12-Feb-2009 at 04:15.

  5. #5

    Default

    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!

  6. #6
    Mesmerale

    Default



    Quote Originally Posted by Incomplete Dude View Post
    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.


    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.

  7. #7

    Default

    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.

  8. #8
    Mesmerale

    Default



    Quote Originally Posted by Incomplete Dude View Post
    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 by Mesmerale; 13-Feb-2009 at 13:39.

  9. #9

    Default

    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.

  10. #10
    Mesmerale

    Default



    Quote Originally Posted by Incomplete Dude View Post
    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 by Mesmerale; 13-Feb-2009 at 13:24. Reason: Update.

Similar Threads

  1. Dress code
    By FluffyFluffers in forum Mature Topics
    Replies: 38
    Last Post: 24-Nov-2008, 23:46
  2. Code Lyoko
    By Takashi in forum Off-topic
    Replies: 13
    Last Post: 07-Jun-2008, 00:09
  3. Learning java
    By Mornavial in forum Computers & Gaming
    Replies: 14
    Last Post: 05-May-2008, 23:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
ADISC.org - the Adult Baby / Diaper Lover / Incontinence Support Community.
ADISC.org is designed to be viewed in Firefox, with a resolution of at least 1280 x 1024.