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
Expand|Select|Wrap|Line Numbers
- 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.String toString()
- {
- String handS = "";
- for(int i = 0; i<thecard.length; 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(hasThreeOfAKind() == true){
- returnValue = THREE_OF_A_KIND;
- }
- if(hasStraight() == true){
- returnValue = STRAIGHT;
- }
- if(hasFlush() = true){
- returnValue = FLUSH;
- }
- if(hasFUllHouse() = true){
- returnValue = FULL_HOUSE;
- }
- if(hasFourOfAKind() == true){
- returnValue = FOUR_OF_A_KIND;
- }
- if(hasStraightFlush() = true){
- returnValue = STRAIGHT_FLUSH;
- }
- return returnValue;
- }
- private boolean hasThreeOfAKind()
- {
- int count = 1;
- for(int i = 0;i<(CARDS_IN_HAND-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_HAND-1);i++){
- for(int j = i + 1;j<CARDS_IN_HAND;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_HAND-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.String getCategoryName(int category)
- {
- String[] rank = {"High card","Pair","Two pairs","Three of a kind","Straight","Flush","Full house","Four of a kind","Straight flush"};
- return rank[category];
- }
- }