• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Arrays and Poker Probability

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having trouble with coding for three of a kind and flush. So what I am stuck on is implementing a hand as an ArrayList of Card objects so that I can add cards to it at run-time using the dealCard() method in DeckOfCards. And then I have to do these two methods in the DeckOfCardsTest file:
static boolean isThreeOfKind( ArrayList<Card> hand )
//A hand is a 3OAK if 3 of the cards have the same face, and the other two do not.  Additionally, the other two cards may not be the same face as each other.
static boolean isFlush( ArrayList<Card> hand )
//A hand is a Flush if all of the cards are the same suit.
Thank you for any suggestions or help in advance!



/**************************************************************************

*******************************************************************

 
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

am having trouble with coding for three of a kind and flush.


Please explain what problems you are having?
How would you solve the problem if you had physical cards in your hand?
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Your shuffle implementation suffers from a flaw that Joshua Bloch describes in the "Lost in the Shuffle" (Puzzler #94) of his "Java Puzzlers" book. Short story: your shuffle algorithm is not a fair random shuffle.

2. Try counting how many of each face you have in the hand first. Then go from there. I would actually try to just write some code that said exactly what you said: "3 of the cards have the same face, and the other two do not. Additionally, the other two cards may not be the same face as each other." If you think about in terms of counting how many different faces you have in the hand, then the problem going forward gets a lot easier.


The first cut of real code might be something like:

Yes, the names are wordy but I'm trying to show how I'd translate from pseudocode to actual code. Once you see that the logic makes sense, then you can start whittling down those names to make them less verbose and more succinct.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're wondering how you'd count the occurrences of each face in a hand, an analogous problem would be to count how many occurrences of each letter there are in a sentence.  For example, "apples are good for you" has these counts: 'A' - 2, 'P' - 2, 'L' - 1, 'E' - 2, 'S' - 1, 'R' - 2, 'G' - 1, 'O' - 4, 'D' - 1, 'F' - 1, 'Y' - 1, and 'U' - 1.  You just need to find an appropriate data structure to store this information in.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic