3 Replies - 17882 Views - Last Post: 15 April 2010 - 02:54 PM Rate Topic: -----

#1 Guest_Dan*


Reputation:

Java Poker Game (Checking for a Winning Hand)

Posted 15 April 2010 - 02:00 PM

Hi I"am having issues with, Checking the hand to see what i have.I can check for a pair with no issues.I'am having problems with checking for a flush, if i have a flush is says it but the issue is if i have nothing then i says i have a flush. The other issue is with the three of a kind and four of a kind checks. Here is my code

/** 
 * Card class
 * @version 0.1
 */

/**
 * The Card class simulates a playing card.
 */
public class Card {
  /** the card's suit
    */
  private String suit;
  
  /** the card's value
    */
  private int value;
  
  /**
   * Constructors a card object given the value of the card and the suit of the card.
   * @param value the card's value
   * @param suit the card's suit
   */
  public Card(int value, String suit){
    this.value = value;
    this.suit = suit;
  }
   public  int getValue(){            
    return value;
  }
    public  String getSuit(){            
    return suit;
  }
  
  
  /**
   * Returns a String representation of the card.
   * @return a String representation of the card
   */
  public String toString() {
    String stringValue = "";
    if (value == 11)
      stringValue = "Jack";
    else if (value == 12)
      stringValue = "Queen";
    else if (value == 13)
      stringValue = "King";
    else if (value == 1)
      stringValue = "Ace";
    else
      stringValue = String.valueOf(value);
      return stringValue + " of " + suit;
  }
}

/**
 * Deck class
 * @version 0.1
 */
import java.util.ArrayList;
import java.util.Collections;

/**
 * The Deck class simulates a deck for a card game.
 */
public class Deck {
  /**
   * list of cards in the deck
   */ 
  private ArrayList<Card> deck = new ArrayList<Card>();
  
  /**
   * Array of Strings representing the suits in the deck
   */ 
  public static final String[] SUITS = {"Clubs", "Diamonds", "Hearts", "Spades"};
  
  /**
   * number of cards in a standard suit of playing cards
   */ 
  public static final int NUMBER_OF_CARDS_IN_STANDARD_SUIT = 13;
  
  /**
   * number of suits in a standard deck of playing cards
   */ 
  public static final int NUMBER_OF_SUITS_IN_STANDARD_DECK = 4;
  
  /**
   * number of cards in a standard deck of playing cards
   */ 
  public static final int NUMBER_OF_CARDS_IN_STANDARD_DECK = NUMBER_OF_CARDS_IN_STANDARD_SUIT * NUMBER_OF_SUITS_IN_STANDARD_DECK;
  
  /**
   * Constructs an unshuffled deck of playing cards.
   */
  public Deck() {
    // populate the deck array list
    // loop through the suits
    for (int suit = 0; suit < NUMBER_OF_SUITS_IN_STANDARD_DECK; suit++) {
      //loop through the values
      for(int value = 1; value <= NUMBER_OF_CARDS_IN_STANDARD_SUIT; value++ ) {
        addCard(new Card(value,SUITS[suit]));
      }
    }
  }
  
  /**
   * Adds a card to the deck's ArrayList.
   */
  public void addCard(Card c) {
    deck.add(c);
  }

  /**
   * Returns a reference to the card object at index in the deck's ArrayList @see deck.
   * 
   * @param index the index of the card to return
   * @return a card object
   */
  public Card getCard(int index) {
    return (Card)deck.get(index);
  }

  /**
   * Returns the size of the deck.
   * 
   * @return an int representing the size of the deck
   */
  public int getSize() {
    return deck.size();
  }
  
  /**
   * Shuffles the deck.
   */
  public void shuffle() {
  int swapIndex1=(int)Math.floor(Math.random()*52);
  if(swapIndex1==52) swapIndex1--;
  int swapIndex2=(int)Math.floor(Math.random()*52);
  if(swapIndex2==52) swapIndex2--;

//swap them
 // Object tmp=card.get(swapIndex1);
 // card.set(swapIndex1, card.get(swapIndex2));
 // card.set(swapIndex2, tmp);
   Collections.shuffle(deck);
  }
  
  /**
   * Deals (returns and removes) a card from the top (front) of the deck.
   * 
   * @return a card object
   */
  public Card dealCard() {
    return (Card)deck.remove(0);
  }
  
  /**
   * Returns a String representation of the deck.
   * 
   * @return a String representation of the deck
   */
  public String toString() {
    String myDeck = "";
    for (int i = 1; i<= deck.size(); i++)
      myDeck += "Card " + i + ": " + deck.get(i-1) + "\n";
    return myDeck;
  }
}

/**
  * Five Card Draw Poker Simulation
  * @author ____________________________
  * @version x.xx
  */

/**
  * The FiveCardDraw class simulates a five card draw poker game.
  */ 
  import java.util.Scanner;
public class FiveCardDraw {
  /**
   * the size of a five card draw poker hand
   */
  public static final int HAND_SIZE = 5;
  
  /**
   * the deck of cards to be used in the game
   */
  private Deck deck = new Deck();
  
  /**
   * the player's hand
   */
  private Hand hand;

  /**
   * the main method that runs the game
   * @param command line arguments if any (none used)
   */
  public static void main(String[] args) {
    FiveCardDraw game = new FiveCardDraw();
     Scanner input = new Scanner(System.in); 
    game.showDeck();
    game.shuffleDeck();
    game.showDeck();
    game.showHand();
    // deal a hand
    System.out.println("Dealing new hand:\n");
    game.dealNewHand();
    game.showHand();
     System.out.println("How many cards do you want to remove? "); // asking player how many card they want to remove
    int discards = input.nextInt();
    
    for (int i = 0; i < discards; i++) { 
   System.out.println("What card do you want to remove."); //removing selected cards   
    int cards = input.nextInt();
    game.hand.removeCard(cards - 1);
    game.showHand();
   }
    game.showHand();
    for (int i = 0; i < discards; i++){
   game.hand.addCard(game.deck.dealCard());
    }
   game.showHand();
   game.gradeHand();
   
    
   

     
    
   

  }

  /*
   * outputs the contents of the deck in order to the console
   */
  public void showDeck() {
    System.out.println("The current deck is:\n");
    System.out.println(deck);
  }

  /**
   * outputs the contents of the hand in order to the console
   */
  public void showHand() {
    if (hand != null) {
      System.out.println("The current hand is:\n");
      System.out.println(hand);
    } else 
      System.out.println("A hand has not been dealt.\n");
  }

  /**
   * calls the method that shuffles the deck
   */
  public void shuffleDeck() {
    deck.shuffle();
  }
  
  /**
   * creates a new Hand object and deals HAND_SIZE cards to it
   */
  public void dealNewHand() {
    hand = new Hand();
    for (int i = 0; i < HAND_SIZE; i++)
      hand.addCard(deck.dealCard());
  }
  
  /**
   * calls the method that evaluates the hand
   */
  public void gradeHand() {
    System.out.println(hand.getGrade());
  }
  
}

/**
 * Hand class
 * @version 0.1
 */ 

import java.util.ArrayList;

/** 
 * The Hand class simulates a hand for a card game
 */
public class Hand {
  /**
   * list of cards in the hand
   */
  private ArrayList<Card> hand = new ArrayList<Card>();
  
  /**
   * Constructs a hand of cards.
   */
  public Hand() {
  }
  
  /**
   * Adds a card to the hand. 
   * 
   * @param c the card to be added to the hand
   */
  public void addCard(Card c) {
    hand.add(c);
  }
  
  /**
   * Returns the size of the hand. 
   * 
   * @return an int representing the hand size
   */
  public int getSize() {
    return hand.size();
  }
 
  /**
   * Removes a card from the hand. 
   * 
   * @param index the index of the card to be removed
   */
  public void removeCard(int index) {
    hand.remove(index);
  }
  
  /**
   * Returns a String representation of the hand. 
   * 
   * @return aString representation of the hand
   */
  public String toString() {
    String myHand = "";
    if (hand.size() == 0)
      return "The hand is empty";
    else {
      for (int i = 1; i<= hand.size(); i++)
        myHand += "Card " + i + ": " + hand.get(i-1) + "\n";
      return myHand;
    }
  }
  
  /**
   * Evaluates the hand.
   * 
   * @return a String representing the grade of the hand
   */
  
  public boolean isPair() {
   int temp; 
    int counter = 0;
for (int i = 0; i < 5; i++){ 
 temp = hand.get(i).getValue(); 
  for (int j = 0; j < 5; j++){  
    //dont check the card against itself 
    if ( i != j) 
      //if temp = card(i), a pair exists 
      if( temp == hand.get(j).getValue() ) 
  counter++;
            if(counter == 2)
            return true;
  } 


} 
return false;
       
  }
  
 public boolean isThreeOfAKind() { 
   int temp; 
    int counter = 0;
for (int i = 0; i < 5; i++){ 
 temp = hand.get(i).getValue(); 
  for (int j = 0; j < 5; j++){  
    //dont check the card against itself 
    if ( i != j) 
      //if temp = card(i), a pair exists 
      if( temp == hand.get(j).getValue() ) {
      counter++;
    }
    
  
            if(counter == 3)
            return true;
  
}

} 
return false;
       
  }
  
 public boolean isFlush() { 
  String temp; 
 
for (int i = 0; i < 5; i++){ 
  temp = hand.get(0).getSuit(); 
  for (int j = 0; j < 5; j++){ 
    //dont check the card against itself 
    if ( i != j) 
      //if temp = card(i), a pair exists 
      if( temp.equals (hand.get(i).getSuit() )) 
        return true; 
  } 
} 
return false;
 }   
 public boolean isFourOfAKind() {
    int temp; 
    int counter = 0;
for (int i = 0; i < 5; i++){ 
 temp = hand.get(i).getValue(); 
  for (int j = 0; j < 5; j++){  
    //dont check the card against itself 
    if ( i != j) 
      //if temp = card(i), a pair exists 
      if( temp == hand.get(j).getValue() ) 
      counter++;
            if(counter == 4)
            return true;
  } 


} 
return false;
       
  }
 
 
   
   
  public String getGrade() {
    
  if(isPair() == true)
       return "Pair";
  if(isThreeOfAKind() == true)
        return "Three of a Kind";
   if(isFlush() == true)
        return "Flush";
   if(isFourOfAKind() == true)
       return "Four of a Kind"; 
    return "Nothing";
  //  Your code here
    //String grade = "This method should evaluate the hand and return the grade i.e. One pair, Two pairs, Three of a kind, etc.";
  //  return grade;
  }
}


This post has been edited by baavgai: 15 April 2010 - 02:39 PM
Reason for edit:: tagged


Is This A Good Question/Topic? 0

Replies To: Java Poker Game (Checking for a Winning Hand)

#2 C++ Programmer   User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: Java Poker Game (Checking for a Winning Hand)

Posted 15 April 2010 - 02:12 PM

Please use the [ code] code here [ /code] tags, it makes it much easier to see and handle your code.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_dan*


Reputation:

Re: Java Poker Game (Checking for a Winning Hand)

Posted 15 April 2010 - 02:35 PM

ok i fixed the error with checking for a flush still having problems with the three and four of a kind
Was This Post Helpful? 0

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7181
  • View blog
  • Posts: 14,969
  • Joined: 16-October 07

Re: Java Poker Game (Checking for a Winning Hand)

Posted 15 April 2010 - 02:54 PM

Your isFlush needn't be so long. In fact, the double loop seems like a bug.
public boolean isFlush() { 
	String suit = hand.get(0).getSuit(); 
 	for (int i = 1; i < 5; i++){ 
		if (!suit.equals(hand.get(i).getSuit())) { return false; }
	} 
	return true;
} 



For four of a kind... maybe:
public boolean isFourOfAKind() {
	for(int kind=0; kind<Deck.NUMBER_OF_CARDS_IN_STANDARD_SUIT; kind++) {
		int kindCount = 0;
		for (int i = 0; i < 5; i++){ 
			if (hand.get(i).getValue()==kind) { kindCount++; }
		}
		if (kindCount==4) { return true; }
	}
	return false;
} 



Though, if you're going to do that, you might as well do:
private boolean countKind(int findNum) {
	for(int kind=0; kind<Deck.NUMBER_OF_CARDS_IN_STANDARD_SUIT; kind++) {
		int kindCount = 0;
		for (int i = 0; i < 5; i++){ 
			if (hand.get(i).getValue()==kind) { kindCount++; }
		}
		if (kindCount==findNum) { return true; }
	}
	return false;
}
public boolean isFourOfAKind() { return countKind(4); }
public boolean isThreeOfAKind() { return countKind(3); }



Note, you're always assuming your hand if 5 in length. That may not be the case.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1