1 Replies - 1298 Views - Last Post: 14 April 2010 - 01:24 PM Rate Topic: -----

#1 Guest_dan*


Reputation:

java poker game help (chrcking for a winning hand)

Posted 14 April 2010 - 11:43 AM

Hi i'am having issue with my poker game. I'am trying to check the hand to see if i have a pair.Not sure what i'am doing wrong here is my code

/**
 * 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() {
    //create array
    String[] values = new String[5];    
     int counter = 0;
  //Put each cards numeric value into array 
     for(int i = 0; i < 5; i++){        
     values[i] = hand(i).getValue();    
     }
     
     
    //Compare each cards value
    for(int x = 0; x < values.length; x++){       
      for(int y = 0; y < 5; y++){           
     if(values[x].equals(card(y).toString()))            
          counter++;            
        if(counter == 2)
      return true;
     
      }
     
     counter = 0;  
    }    
    return false;
  }
     
  public String getGrade() {
    
  if(isPair() == true)
       return "Pair";
    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;
  }
}



  /**
  * 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());
  }
  
}


/**
 * Deck class
 * @author Rick Stephens
 * @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;
  }
}


/** 
 * 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  int 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;
  }
}



Edited by Dogstopper: :code:

This post has been edited by Dogstopper: 14 April 2010 - 11:58 AM


Is This A Good Question/Topic? 0

Replies To: java poker game help (chrcking for a winning hand)

#2 timkd127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 27-February 10

Re: java poker game help (chrcking for a winning hand)

Posted 14 April 2010 - 01:24 PM

It looks to me like your problem is in your isPair() method. I dont think comparing the value to the toString() is working like you think it is. Try something like this
int temp;

for (int i = 0; i < 5; i++){
  temp = hand(0).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(i).getValue() )
        return true;
  }
}
return false;


I havent compiled this but im pretty sure it works. Hope this helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1