I need help in my lap so I can understand it better. The DeckOfCards class will
ID: 3545632 • Letter: I
Question
I need help in my lap so I can understand it better.
The DeckOfCards class will manage an array containing the 52 unique Cards. It should contain the following methods, at minimum (you may want to implement other methods, as well):
CardDealer
Write a driver class called CardDealer to test DeckOfCards. Demonstrate the creation of a deck of cards (show that all cards have been created by printing out the deck) and shuffling the deck (show that the cards have been shuffled). Implement a simple "draw two cards and determine who wins" game to demonstrate drawing and comparing cards. The game simply keeps drawing two cards and comparing until the deck is empty. Demonstrate that you can restore the deck to a fresh starting state when the game ends.
--------------------------------------------
Here is the enums that we already have:
2- enum FaceValue:
Start by importing enums for Suit (Diamonds, Clubs, Hearts, Spades) and FaceValue (Two, Three, ... , Nine, Ten, Jack, Queen, King, Ace), each of which is defined in its own .java file. The FaceValue enum is enhanced to associate a numeric ranking value for each face value for comparing which card is higher in value than another.
Also import the Card class which represents one card in the deck. Each card object will be a unique combination of a Suit and aFaceValue. The Card class implements the Comparable interface and has a method int compareTo() (that works the same as thecompareTo() method of String) for giving the relative ordering of one Card when compared to another.
Explanation / Answer
// DeckOfCards.java
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class DeckOfCards {
// Total number of cards
private static final int N_CARDS = Suit.values().length
* FaceValue.values().length;
private Card[] deck;
private int front; // index of next card to deal in deck
public DeckOfCards() {
this.deck = new Card[N_CARDS];
this.front = 0;
// Initialize each card in deck in order
// Clubs - 2 to A
// Diamonds - 2 to A
// Hearts - 2 to A
// Spades - 2 to A
// by creating 2 loops, outer loop is Suit, inner loop is FaceValue
for (int s = 0; s < Suit.values().length; ++s)
for (int r = 0; r < FaceValue.values().length; ++r)
this.deck[front++] = new Card(Suit.values()[s],
FaceValue.values()[r]);
this.front = 0; // reset front to 0 after inititalization
}
public void shuffle() {
// Convert inner array to ArrayList
ArrayList<Card> temp = new ArrayList<Card>(Arrays.asList(deck));
// Use build-in shuffle
java.util.Collections.shuffle(temp);
// Copy ArrayList back to inner array deck
temp.toArray(deck);
}
public Card draw() {
if (front == N_CARDS) return null; // no card left
return deck[front++]; // return deck[front] then increment front
}
public void restoreDeck() {
// Re-initialize each card in deck in order
this.front = 0;
for (int s = 0; s < Suit.values().length; ++s)
for (int r = 0; r < FaceValue.values().length; ++r)
this.deck[front++] = new Card(Suit.values()[s],
FaceValue.values()[r]);
this.front = 0;
}
public int numCardsRemaining() {
return deck.length - front;
}
public int numCardsDealt() {
return front;
}
public Card[] dealtCards() {
if (numCardsDealt() == 0) return null;
// Dealt cards are cards with index [0..front) in deck
// Copy them to ret
Card[] ret = new Card[front];
for (int i = 0; i < front; ++i)
ret[i] = deck[i];
return ret;
}
public Card[] remainingCards() {
if (numCardsRemaining() == 0) return null;
// Dealt cards are cards with index [front..N_CARDS) in deck
// Copy them to ret
Card[] ret = new Card[numCardsRemaining()];
for (int i = 0; i < numCardsRemaining(); ++i)
ret[i] = deck[i+front];
return ret;
}
public String toString() {
return "" + numCardsRemaining() + " card(s) remaining"
+ (numCardsRemaining() > 0 ? ", first card " + deck[front]
+ ", last card " + deck[N_CARDS-1] : "") + ".";
}
}
// CardDealer.java
public class CardDealer {
public static void main(String[] args) {
DeckOfCards deck = new DeckOfCards(); // create new deck
System.out.println(deck); // show it
deck.shuffle(); // shuffle deck
System.out.println(deck); // show it
while (deck.numCardsRemaining() > 0) { // deal cards until deck is empty
Card card1 = deck.draw(); // deal card to player 1
System.out.println("Player 1 draws " + card1);
Card card2 = deck.draw(); // deal card to player 1
System.out.println("Player 2 draws " + card2);
int cmp = card1.compareTo(card2); // compare 2 cards
// print result
if (cmp == 0) System.out.println("Draw!");
else if (cmp > 0) System.out.println("Player 1 wins!");
else System.out.println("Player 2 wins!");
System.out.println(deck + " "); // see current status of deck
} // deck is empty now
deck.restoreDeck(); // restore it
System.out.println(deck); // should print identical string with line 7
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.