5 Replies - 1977 Views - Last Post: 06 May 2010 - 11:43 PM Rate Topic: -----

#1 Guest_Dan*


Reputation:

Java poker game shuffling the deck and checking for a fullhouse

Posted 06 May 2010 - 11:12 AM

Hi I have a issue with my code for shuffling my arraylist. The error is in the deck class. Not sure what i'am doing wrong. The other issue i have is with checking my hand for a full house. If i have a full house it says i have a pair. A full house consist of a pair and three of a kind, so if both are true then you have a full house. Here is my code

[/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;
}
}
[/code]


[/code]
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©;
}

/**
* 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=deck.get(swapIndex1);
deck.set(swapIndex1, deck.get(swapIndex2));
deck.set(swapIndex2, tmp); //Error cannot find symbol: method set

//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;
}
}
[/code]

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

}
[/code]

[/code]
/**
* Hand class
* @author Rick Stephens
* @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©;
}

/**
* 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() {
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==2) { return true; }
}
return false;
}


public boolean isThreeOfAKind() {
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==3) { return true; }
}
return false;
}

public boolean isFlush() {
String temp;

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

public boolean isFullHouse(){

if(isThreeOfAKind() == true && isPair() == true){
return true;
}
return false;

}
public boolean isTwoPair(){
return false;
}




public String getGrade() {

if(isPair() == true)
return "Pair";
if(isTwoPair() == true)
return "Pair";
if(isThreeOfAKind() == true)
return "Three of a Kind";
if(isFlush() == true)
return "Flush";
if(isFourOfAKind() == true)
return "Four of a Kind";
if(isFullHouse() == true)
return "Full House";
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;
}
}

[/code]

Is This A Good Question/Topic? 0

Replies To: Java poker game shuffling the deck and checking for a fullhouse

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,645
  • Joined: 27-December 08

Re: Java poker game shuffling the deck and checking for a fullhouse

Posted 06 May 2010 - 11:17 AM

Please post your properly indented code into code tags, like so: :code:

As for the shuffling, you can use Collections.shuffle(List) on your ArrayList.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_dan*


Reputation:

Re: Java poker game shuffling the deck and checking for a fullhouse

Posted 06 May 2010 - 12:40 PM

teacher told us that we couldnt use Collections.shuffle(List).
Was This Post Helpful? 0

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,645
  • Joined: 27-December 08

Re: Java poker game shuffling the deck and checking for a fullhouse

Posted 06 May 2010 - 01:06 PM

I can't help you if you don't post your properly code between code tags so that we can read it, like so: :code:

Also, what error(s) are you getting when shuffling the deck?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Java poker game shuffling the deck and checking for a fullhouse

Posted 06 May 2010 - 07:30 PM

http://www.dreaminco...sted-to-fix-it/

Sounds like double post under to different names to me :plain:
Was This Post Helpful? 0
  • +
  • -

#6 aniri  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 657
  • Joined: 24-November 09

Re: Java poker game shuffling the deck and checking for a fullhouse

Posted 06 May 2010 - 11:43 PM

Your isFullhouse() method is wrong. You need to check that you have 3 cards of a kind and that the other 2 cards in the hand are a pair.

You are only checking separately: only 2 or 3 cards a t a time, not all 5 .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1