3 Replies - 13374 Views - Last Post: 29 April 2010 - 08:03 PM Rate Topic: -----

#1 Guest_john*


Reputation:

java poker game checking for a fullhouse and two pair

Posted 29 April 2010 - 11:18 AM

Hi i'am having issues issue with my fullhouse method i'am getting a error have unreachable statement and i dont know why. Also i'am not sure on how to check for two pairs. here is my code

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



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;
  }
}



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



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


This post has been edited by Dogstopper: 30 April 2010 - 03:07 AM
Reason for edit:: code fixed


Is This A Good Question/Topic? 0

Replies To: java poker game checking for a fullhouse and two pair

#2 Scotty G   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 59
  • Joined: 09-April 10

Re: java poker game checking for a fullhouse and two pair

Posted 29 April 2010 - 11:46 AM

Hello there. What exactly is the error your getting? And make sure you post that code in code brackets my friend, So I can take a better look for you. The code bracket button looks like this <> :smile2:

This post has been edited by Scotty G: 29 April 2010 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Guest*


Reputation:

Re: java poker game checking for a fullhouse and two pair

Posted 29 April 2010 - 01:50 PM

Ok i fixed the error in my code for the full house. How would i check for 2 pairs.
Was This Post Helpful? 0

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12314
  • View blog
  • Posts: 45,412
  • Joined: 27-December 08

Re: java poker game checking for a fullhouse and two pair

Posted 29 April 2010 - 08:03 PM

Please, :code:

To check for pairs, you would want to compare the rank/values of your Cards to see if they are the same. If you make your Card class Comparable by implementing the Comparable interface, you could define ordering by rank and invoke Collections.sort() on your Hand (or the List<Card> in the Hand class) and check for consecutive equal ranks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1