Problem with a Poker hand class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandy123456
    New Member
    • Jun 2007
    • 5

    Problem with a Poker hand class

    At the moment im trying to write a hand class for a game poker patientnce
    But when i get to the part having to catergorise the difference of full house straight flush flush four of a kind and straight i got stuck.I need to write boolean methods to return these (stright flush , four of a kind..etc) I can only do a pair and 2 pairs and three of a kind. The following is my code please someone if possible help me thanks

    [CODE=java] import java.util.*;

    public class Hand
    {
    public static final int CARDS_IN_HAND = 5;
    public static final int CARDS_IN_SUIT = 13;
    public static final int NUM_RANKS = 4;

    //Categories of hands
    public static final int HIGH_CARD = 0;
    public static final int PAIR = 1;
    public static final int TWO_PAIRS = 2;
    public static final int THREE_OF_A_KIND = 3;
    public static final int STRAIGHT = 4;
    public static final int FLUSH = 5;
    public static final int FULL_HOUSE = 6;
    public static final int FOUR_OF_A_KIND = 7;
    public static final int STRAIGHT_FLUSH = 8;
    private Card[] thecard;

    /**
    * Constructor for objects of class Hand
    */

    public Hand(Card[] cards)
    {
    if(cards.length == CARDS_IN_HAND)
    {
    thecard = new Card[5];
    for(int i = 0; i<cards.length ; i++)
    {
    thecard[i] = cards[i];
    }
    }

    }

    public Hand(Deck deck)
    {
    thecard = new Card[5];
    for(int i = 0; i < CARDS_IN_HAND; i++){
    thecard[i] = deck.takeTop();
    }
    }

    public Card getCard(int i)
    {
    return thecard[i];
    }

    public java.lang.Strin g toString()
    {
    String handS = "";
    for(int i = 0; i<thecard.lengt h; i++){
    handS += thecard[i]+" ";
    }
    return handS.trim();
    }

    public int getCategory()
    {
    int returnValue = HIGH_CARD;

    if(hasPair() == true){
    returnValue = PAIR;
    }
    if(hasTwoPairs( ) == true){
    returnValue = TWO_PAIRS;
    }
    if(hasThreeOfAK ind() == true){
    returnValue = THREE_OF_A_KIND ;
    }
    if(hasStraight( ) == true){
    returnValue = STRAIGHT;
    }
    if(hasFlush() = true){
    returnValue = FLUSH;
    }
    if(hasFUllHouse () = true){
    returnValue = FULL_HOUSE;
    }
    if(hasFourOfAKi nd() == true){
    returnValue = FOUR_OF_A_KIND;
    }
    if(hasStraightF lush() = true){
    returnValue = STRAIGHT_FLUSH;
    }
    return returnValue;
    }


    private boolean hasThreeOfAKind ()
    {
    int count = 1;
    for(int i = 0;i<(CARDS_IN_H AND-2);i++){
    for(int j = i+1;j<CARDS_IN_ HAND;j++){
    if(thecard[i].getRank() == thecard[j].getRank()){
    count++;
    }
    }
    if(count == 3)
    {
    break;
    }else{
    count = 1;
    }
    }
    return count == 3;
    }

    private boolean hasTwoPairs()
    {
    int count = 1;
    int pair = 0;
    for(int i = 0;i<(CARDS_IN_H AND-1);i++){
    for(int j = i + 1;j<CARDS_IN_HA ND;j++){
    if(thecard[i].getRank() == thecard[j].getRank()){
    count++;
    }
    if(count == 2){
    pair++;
    count =1;
    }else{
    count =1;
    }

    }
    }
    return pair == 2;
    }

    private boolean hasPair()
    {
    int count = 1;
    int pos = 1;
    for(int i = 0;i<(CARDS_IN_H AND-1);i++){
    for(int j = i+1;j<CARDS_IN_ HAND;j++){
    if(thecard[i].getRank() == thecard[j].getRank()){
    count++;
    }
    }
    if(count == 2)
    {
    break;
    }else{
    count = 1;
    }
    }
    return count == 2;
    }


    public static java.lang.Strin g getCategoryName (int category)
    {
    String[] rank = {"High card","Pair","T wo pairs","Three of a kind","Straight ","Flush"," Full house","Four of a kind","Straight flush"};
    return rank[category];
    }

    }
    [/CODE]
    Last edited by r035198x; Jun 2 '07, 09:42 AM. Reason: added the missing code tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Look how better the code looks under code tags! Remember them next time.
    2.) You don't have to write java.lang.Strin g for return type. Just use String(remember the java.lang package is automatically imported for you)
    3.) I don't know a thing about poker (they could be others like me here as well) so why don't you explain the rules of the methods that you want to implement. You don't have to explain the whole game. Just the stright flush e.t.c

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Think of a card as a rank [0, 12] and a suit [0, 3]. Create two int arrays, one for
      the ranks and one for the suits. Given the hand (five cards) add the corresponding
      entries in the rank and suit arrays. If you find five consecutive ones (1) in the
      rank array you've got a flush; if on top of that one of the suit elements in the
      suit array equals four, you've got a royal flush. If you find a four in the rank array
      you've got a carre etc. etc.

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        Think of a card as a rank [0, 12] and a suit [0, 3]. Create two int arrays, one for
        the ranks and one for the suits. Given the hand (five cards) add the corresponding
        entries in the rank and suit arrays. If you find five consecutive ones (1) in the
        rank array you've got a flush; if on top of that one of the suit elements in the
        suit array equals four, you've got a royal flush. If you find a four in the rank array
        you've got a carre etc. etc.

        kind regards,

        Jos
        Ah, looks like Jos has played a few hands.

        Comment

        • sandy123456
          New Member
          • Jun 2007
          • 5

          #5
          Originally posted by r035198x
          1.) Look how better the code looks under code tags! Remember them next time.
          2.) You don't have to write java.lang.Strin g for return type. Just use String(remember the java.lang package is automatically imported for you)
          3.) I don't know a thing about poker (they could be others like me here as well) so why don't you explain the rules of the methods that you want to implement. You don't have to explain the whole game. Just the stright flush e.t.c
          Yip it does look better under the code tags i will remember use it next time.
          About poker in my code there should be 5 cards in the hand and you want to check if there is flush straight etc in it.So a flush is where all 5 cards are the same suit eg all 5 cards are spades all 5 cards are diamonds. 4of a kind is where out of the 5 cards 4 cards are the same eg ace ace ace ace and a king tat is a four of a kind.Straight Flush is where those 5 cards are one after the other doesnt matter the order and with the same suit. Eg the 5 cards are spade ace spade 3 spade5 spade 2 spade 4 this is a straight flush. A full house is where there is a three of a kind with a pair eg the cards are ace ace ace 2 2. I m struggling with writing boolean methods for straight flush, flush,4 of a kind,straight in this class.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by sandy123456
            I m struggling with writing boolean methods for straight flush, flush,4 of a kind,straight in this class.
            Read my reply #3 for starters.

            kind regards,

            Jos

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by r035198x
              Ah, looks like Jos has played a few hands.
              I'm quite good at cheating and counting too ;-)

              kind regards,

              Jos (<--- miserable specimen)

              Comment

              • sandy123456
                New Member
                • Jun 2007
                • 5

                #8
                Originally posted by JosAH
                Read my reply #3 for starters.

                kind regards,

                Jos
                Ok i get the idea of what ur saying i can do the part about the same suit but i dont know how to find consecutive number in the array where bout can i look up tat kinda info i checked in my book cant find similar problem

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by sandy123456
                  Ok i get the idea of what ur saying i can do the part about the same suit but i dont know how to find consecutive number in the array where bout can i look up tat kinda info i checked in my book cant find similar problem
                  how about [CODE=java]if((array[i + 1] + 1) == array[i] ) [/CODE] ?

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    how about [CODE=java]if((array[i + 1] + 1) == array[i] ) [/CODE] ?
                    It can be much simple than that; have a look:[code=java]
                    char[] rank= "0000000000000" .toCharArray(); // Thirteen zeros
                    // add 1 to the rank and suit arrays for all cards;
                    int index= new String(rank).in dexOf("11111");
                    if (index >= 0)
                    // five consecutive cards starting at 'index'[/code]
                    kind regards,

                    Jos (<--- bag of old tricks)

                    Comment

                    • sandy123456
                      New Member
                      • Jun 2007
                      • 5

                      #11
                      Originally posted by JosAH
                      It can be much simple than that; have a look:[code=java]
                      char[] rank= "0000000000000" .toCharArray(); // Thirteen zeros
                      // add 1 to the rank and suit arrays for all cards;
                      int index= new String(rank).in dexOf("11111");
                      if (index >= 0)
                      // five consecutive cards starting at 'index'[/code]
                      kind regards,

                      Jos (<--- bag of old tricks)
                      umm i dont quite get this method is it possible if u explain it a little bit?
                      I understand tat array one but u said this one is simplier so im wanna understand it. Also is it possible to use a for loop will it be similar ideas to this.

                      Thanks

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by sandy123456
                        umm i dont quite get this method is it possible if u explain it a little bit?
                        I understand tat array one but u said this one is simplier so im wanna understand it. Also is it possible to use a for loop will it be similar ideas to this.

                        Thanks
                        Char arrays are the building blocks of Strings; you can easily convert one to
                        another and vice versa (see my litle code snippet). If I initialize a String with all
                        character '0's, convert the String to an array and add 1 to them for every card
                        rank in my hand, I get a 'rank score'. If I have five consecutive cards in my hand
                        I end up with a String that has five consecutive '1' values in it. The indexOf()
                        method will find them. If I'd used regular expressions I could even have found a
                        full house easily: "(2.*3)|(3. *2)" or two pairs or whatever. Check the API docs.

                        You don't need any loops except for updating the 'rank' and 'suit' arrays given
                        your hand of cards:[code=java]
                        for (Card card : hand) {
                        rank[card.getRank()]++;
                        suit[card.getSuit()]++;
                        }[/code]

                        kind regards,

                        Jos

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by JosAH
                          It can be much simple than that; have a look:[code=java]
                          char[] rank= "0000000000000" .toCharArray(); // Thirteen zeros
                          // add 1 to the rank and suit arrays for all cards;
                          int index= new String(rank).in dexOf("11111");
                          if (index >= 0)
                          // five consecutive cards starting at 'index'[/code]
                          kind regards,

                          Jos (<--- bag of old tricks)
                          I like this one.
                          Lesson learnt: Never leave a couple of string literals to Jos.

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by r035198x
                            I like this one.
                            Lesson learnt: Never leave a couple of string literals to Jos.
                            SImilar trickery-dickery applies equally well to that other "Hangman" thread.
                            Strings can come in handy when abused well ;-)

                            kind regards,

                            Jos

                            Comment

                            • sandy123456
                              New Member
                              • Jun 2007
                              • 5

                              #15
                              Thanks for the tips but i think im still stuck could somoene point out my mistakes or where i be confused with here my codes.Im still not too sure with rour method also what is it mean by regular expressions

                              Thank You


                              [PHP] public Hand(Card[] cards)
                              {
                              cardrank= new int[12];
                              if(cards.length == CARDS_IN_HAND)
                              {
                              thecard = new Card[5];
                              for(int i = 0; i<cards.length ; i++)
                              {
                              thecard[i] = cards[i];
                              }
                              }

                              }

                              public int getCategory()
                              {
                              if(hasStraight( ) == true){
                              returnValue = STRAIGHT;
                              }

                              return returnValue;

                              private boolean hasStraight()
                              {
                              cardrank= new int[12];
                              char[] cardrank= "0000000000000" .toCharArray();
                              int index= new String(cardrank ).indexOf("1111 1");
                              if (index >= 0){
                              return true;
                              }
                              return false;
                              }[/PHP]

                              Comment

                              Working...