tags:

views:

197

answers:

4

I was wondering if anyone knew how to implement the code in java to print all cases of full house. There are roughly 3700 different cases. So far i'm around 2700 but I am having trouble changing the suits, her is what I have so far.

public class FullHouseTest 
{//
static int count = 1;
static int [] cards ={1,2,3,4,5,6,7,8,9,10,11,12,13};
static int[] suit ={1,2,3,4};
static int[] suit2 ={2,3,4,1};
static int[] suit3 ={3,4,1,2};
 public static void main(String[] args) 
 { 
  for(int k = 0; k< 12; k++)
  {
   for(int i = 0; i < 3; i++)
   {
    for (int t = 0; t <3; t++)
    { 
     Card one = new Card(new Suit(suit[t]), new Pips(cards[k]));
     Card two = new Card(new Suit(suit2[t]), new Pips(cards[k]));
     Card three = new Card(new Suit(suit3[t]),new Pips(cards[k]));

     for (int j =0; j < 12; j++)
      { 
        Card four = new Card(new Suit(suit2[i]), new Pips(cards[j+1]));
        Card five = new Card(new Suit(suit[i]), new Pips(cards[j+1]));
        System.out.println("Hand:" + count + " | " + one + two + three + four + five);
        count ++;
      }
    }
   }
  }
  for(int i = 0; i < 3; i++)
  {
   for(int k = 0; k< 12; k++)
   {
     for(int s = 0; s<3; s++)
     {
      Card one = new Card(new Suit(suit[i]), new Pips(cards[k]));
      Card two = new Card(new Suit(suit2[i]), new Pips(cards[k]));
     for (int j =0; j < 12; j++)
     {

      Card three = new Card(new Suit(suit3[s]),new Pips(cards[j+1]));
      Card four = new Card(new Suit(suit2[s]), new Pips(cards[j+1]));
      Card five = new Card(new Suit(suit[s]), new Pips(cards[j+1]));
      System.out.println("Hand:" + count + " | " + one + two + three + four + five);
      count ++;


     }
    }
   }
  }

 }
}
+2  A: 

Add some comments to your code before you go any further. It'll help you understand what's going on, especially when you have loops nested 4 deep with single-char variable names.

Next, break the problem down: What's really unique about a full house? Both of the # of pips are unique but can't be the same. The 3 of a kind has 3 different suits (or just lacks one), and the pair has 2 different suits.

total_pips * (total_pips-1) * number_suits *  (possible combinations of 2 suits )  = 3744
     13            12               4                         6

Think about what you might be missing from this list. If you have any specific questions just edit the answer and we'll get right on it :)

CrazyJugglerDrummer
+1  A: 

Way too much code in a main method. you need to use methods better here.

create a method called isFullHouse(Card[] card), which takes in an array (or ArrayList) of 5 cards and will determine for you if the hand is a full house.

Then how you choose to create all possible combinations of hands is up to you. Each time you get a new hand, call this method. It will simplify things for you. Everything in main is very hard to read.

As far as how you store your deck of cards. instead of all those arrays, store one which is 0-51. you can use divide and mod operators on the array to determine which card you have. The magic number is 13.

i.e. The 47 card in the deck could be: 47/13=3 ; 47 % 13 = 8

If you determine ahead of time that 0=hearts, 1= diamonds, 2=clubs and 3=spades, then you can determine that this card is the 9 of spades (8+1 due to no card having the value 0 so add one)

Store all these ideas in their own methods, and you can simplify your loops considerably.

Sean
Assuming my math is correct, there are P(52,5) = 311,875,200 possible 5-card hands. Your algorithm may take a while to run.
Loadmaster
Most will unless the algorithm is pruned to omit suites as they have no baring on a full house. knowing that you can have no more then 4 of any value (A-K) in a hand at any given time, the algorithm on how to look at each possible hand can be cut down. You can also trim it further, by taking each permutation of the 4 suites with the 13 cards per suite to form the three of a kind, and the same for the pair. How the OP implements that is up to him. Enough rambling. My main point was to clean up the code to help it become more readable.
Sean
2,598,960 possible hands. (52!/(5!*47!)), 311 mill seemed a bit high. Still a lot to look at though.
Sean
A: 

Quick and dirty. There is plenty of room for improvement but if my math is correct this should give you all the valid hand combinations of a full house.

public class PrintFullHouse {

enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES}
enum Rank {Ace, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}

public static void main(String[] args) {

     String[] ranks = new String[14];
     String[] suits = new String[4];

    populateSuits(suits);
    populateCards(ranks);

    int numberOfCombos = 0;

    //Loop over all card values. This outer for loop is for the 3 of kind values. 3 Aces, 3 Twos, 3 Threes, etc
    for(int card = 0; card < ranks.length; card++)
    {
        String firstCard = ranks[card]; 

        for(int suit1 = 0; suit1 < suits.length; suit1++)
        {
            for(int suit2 = suit1+1; suit2 < suits.length; suit2++)
            {
                for(int suit3 = suit2+1; suit3 < suits.length; suit3++)
                {
                    //Loop over all card values that aren't equal to the firstCard.So we won't have 3 Aces and 2 Aces
                    for(int card2 = 0; card2 < ranks.length; card2++)
                    {
                        String secondCard = ranks[card2]; 

                        //Dont Compare the 3 of a Kind and 2 pair when they are the same rank. ie Aces and Aces
                        if(firstCard.compareTo(secondCard) != 0){
                            for(int othersuit1 = 0; othersuit1 < suits.length; othersuit1++)
                            {
                                for(int othersuit2 = othersuit1+1; othersuit2 < suits.length; othersuit2++)
                                {
                                    //Found a valid combo if 3 of a kind have different suits, 2 pair have different suits, and card1 is not equal to card2
                                    numberOfCombos++;
                                    System.out.println(numberOfCombos+". "+firstCard+" "+suits[suit1]+" "+firstCard+" "+suits[suit2]+" "+firstCard+" "+suits[suit3]+ " "
                                                       +secondCard+" "+suits[othersuit1]+" "+secondCard+" "+suits[othersuit2]);
                                }
                            }
                        }
                    }               
                }
            }
        }
    }
}

private static void populateSuits(String[] suits) {

    int index = 0;
    for(Suit suit: Suit.values())
    {
        suits[index++] = suit.toString();
    }
}

private static void populateCards(String[] ranks) {

    int index = 0;
    for(Rank rank: Rank.values())
    {
        if(index != ranks.length)
        ranks[index++] = rank.toString();
    }
}

}

aahrens
A: 

I saw this problem the other day (while out sick). I've been debating about chiming in ever since. On one hand, it seems like homework. (It's a simple problem. Your code is difficult to understand, indicating of a lack of experience.)

On the other hand, I don't mind helping out. I'm not going to do your work for you, but I can point you in the right direction...


First step: Define the problem. Once clearly defined, answers become far more straightforward.

A "full house", presumably 5 cards consisting of a three-of-a-kind plus a pair. Presumably this is a single-deck game. Presumably this is a standard deck (Ace,2,3,4,5,6,7,8,9,Jack,Queen,King) with suits (Spades,Clubs,Hearts,Diamonds). (Abbreviated below as (A23456789JQK) and (SCHD).)

You mention 3700 combinations. Presumably you therefore consider the hands (2S,2C,2H,3H,3D) and (3D,3H,2H,2C,2S) as being equivalent rather than distinct. (That's actually quite an important point, as touched upon by Sean & Loadmaster in their comments. There are 311,875,200 (52*51*50*49*48) possible 5-card drawings. However, only 2,598,960 of those hands are distinct!)

We have (13 * 4) possible three-of-a-kinds. E.g. For each rank card (such as 3), we can have 4 three-of-a-kinds ({0S,3C,3H,3D}, {3S,0C,3H,3D}, {3S,3C,0H,3D}, {3S,3C,3H,0D}). (Perhaps you begin to notice a pattern: 0111 1011 1101 1110.)

Give our three-of-a-kind, and presuming it's a single deck and standard deck game, our pair must be one of the other 12 remaining card ranks. For each card rank, there are six possibilities for a pair. E.g. Given a card rank of 7, we could have ({7S,7C,0H,0D}, {7S,0C,7H,0D}, {7S,0C,0H,7D}, {0S,7C,7H,0D}, {0S,7C,0H,7D}, {0S,0C,7H,7D}). (Again, perhaps you notice the pattern: 1100 1010 1001, 0110 0101, 0011.)

This gives us 13 * 4 * 12 * 6 = 3744 combinations.

From here it is a simple matter of looping to print them out.

I suggest you consider more descriptive variable names. While there are places and times to use single character loop variables, this is not one of them. Well written code is nearly self-documenting, allowing documentation to concentrate on the more complex higher-level abstractions. The few extra characters you save will wind up costing you a fortune in debugging time. If desired, you can be lazy like me, learn emacs, use (require 'completion), (global-set-key "\C-\\" 'complete), type the first few characters and let emacs auto-complete for you.

I suggest you consider supporting, and perhaps private, methods. For instance, you might be able to do something like: (It's been a while since I last coded in Java.)

for ( suit = 0;  suit < 4 ;  ++ suit )
    private_printThreeOfAKind( card, suit!=0, suit!=1, suit!=2, suit!=3 )

Three of those (!=) would be true, one would be false.

With respect to printing pairs, you may want to investigate the continue statement. Ref: http://en.wikipedia.org/wiki/Java_syntax#continue_statement

E.g. This would allow you to skip over the case where the pair card is the same rank as the three-of-a-kind:

if ( threeOfAKindCard == pairCard )
    continue;

I suggest you build your software in parts. Trying to build a complete system rarely works, even for the experts. Build parts, test them, rinse, repeat. Yes, that means writing scaffolding code that you won't turn it. Perhaps even a testing subclass... But small steps are easier to get working. As you improve as a programmer, you'll be able to take larger steps...

Mr.Ree
Thanks for all the help, I got it figured out a-little while ago. Problem was I was in a rush and was avoiding breaking the problem down. I figured it out by printing out all pairs to get a better understanding. Again thanks for the help, time and thought out into this problem, and sorry for my sloppy code.
Tony