I need to make a flush method and I've been trying different things based off what I've read and what my instructor has told me.
- Add a method getSuit to class Card
- Modify the class TestGame to deal a five-card poker hand
- Modify the class DeckOfCards to include a method that determines a hand contains a flush
I've added bold to the code that has error.
------------------------------------------------------------------------------------
import java.security.SecureRandom;
public class DeckOfCards {
private static final SecureRandom randomNumbers = new SecureRandom();
private static final int NUM_OF_CARDS = 52;
private Card[] deck = new Card[NUM_OF_CARDS];
int[] hand = new int[5];
private int currentCard = 0;
public DeckOfCards() {
String[] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
String[] suits = {"Hearts", "Clubs", "Diamonds", "Spades"};
// populate deck with card objects
for (int count = 0; count < NUM_OF_CARDS; count++) {
deck[count] = new Card(faces[count % 13], suits[count / 13]);
}
} // end of DeckOfCards constructor
public void shuffle() {
// after shuffling, dealing should start at deck{[0]
currentCard = 0;// reinitliaze currentCard
// for each Card, pick another random Card (0-51) and swap them
for(int first = 0; first < deck.length; first++) {
// select a random number between 0 and 51
int second = randomNumbers.nextInt(NUM_OF_CARDS);
Card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
} // end of loop
} // end of method shuffle
public Card dealCard() {
if (currentCard < deck.length) {
return deck[currentCard++];
} else {
return null;
}
}
public Card dealHand() {
if (currentCard < 5 ) {
return deck[currentCard++];
} else {
return null;
}
} // end of method
flush(hand);
public Card flush(int hand[]) {
String suit = hand{0}.getSuit();
for(int i = 1; i < hand.length; i++){
if (hand[i].getSuit() != suit){
ret
}
return null;
}
}
}// end of class
--------------------------------------------------------------------
public class Card {
private final String face; //face of card ("Ace", "duce", ...)
private final String suit; // suit of a card ("hearts:, "diamonds",...)
// constructor of Card object
public Card(String cardFace, String cardSuit) {
this.face = cardFace;
this.suit = cardSuit;
} // end of constructor
public String toString() {
return face + " of " + suit;
} // end of toSTring
public String getSuit(){
return this.suit;
} // end of getSuit
} // end of class
-----------------------------------------------------------
public class TestGame {
public static void main(String[] args) {
// TODO code application logic here
DeckOfCards myDeck = new DeckOfCards();
Card[] hand = new Card[5];
for(int card = 0; card < 52; card++){
System.out.printf("%-19s", myDeck.dealCard());
if ((card % 4) == 0) {
System.out.println();
}
} // end of for
// shuffle the entire deck
myDeck.shuffle();
System.out.println("\n\n");
System.out.println("Shuffled deck of cards:");
for(int card = 0; card < 52; card++){
System.out.printf("%-19s", myDeck.dealCard());
if ((card % 4) == 0) {
System.out.println();
}
} // end of for
for(int i=0; i <= 5;i++){
hand[i] = myDeck.dealHand();
System.out.printf("%-19s", myDeck.dealHand());
}
} // end of main method
} // end of class