A07
Due Date:
Friday, 12 March 2021
File(s) to be submitted:
Deck.java
Sample Output:
SampleOutput.html
Starter Files:
SUBMIT
/
Check
ArrayLists and Collections
Summary
Create a data type class for a deck of cards.
I have provided you with a data type Card
for the cards themselves,
and a program Game
that simply deals out hands and then returns them to the deck.
The Cards
I have provided you with a class Card
that represents a single playing card.
Do not modify this class.
It performs exactly the way I want it to.
It contains several constants
and two Lists that will be helpful to you:
-
SPADES, HEARTS, DIAMONDS, CLUBS:
ints representing the four suits a card might be from;
-
ACE, JACK, QUEEN, KING:
ints representing the four non-numeric ranks
a card might have;
-
SUITS:
a List of Integers representing the four suits
(SPADES, HEARTS, DIAMONDS, CLUBS);
and
-
RANKS:
a List of Integers representing the thirteen ranks
(ACE, 2, 3, 4, ..., 10, JACK, QUEEN, KING).
The Card constructor is expecting to be given two int values
representing the rank and the suit
of the card to be constructed.
For example,
Card aceOfClubs = new Card(Card.ACE, Card.CLUBS);
creates the ace of clubs.
And,
of course,
you can use variables in the constructor.
So if r = 3 and s = Card.DIAMONDS
then
Card card = new Card(r, s);
creates the 3 of diamonds.
The Deck
Your task in this assignment
is to create the Deck class.
It represents a deck of cards
using a List.
The class implements the following methods
and constructors:
-
Deck()
create a standard deck of cards
-
int cardsLeft()
return the number of cards left in the deck
-
Card takeTopCard()
return the top card of the deck (which is removed from the deck)
-
void shuffle()
shuffle the deck
-
List<Card> deal(int numCards)
deal the given number of cards from the deck
param numCards the number of cards to be dealt
return a sorted list of cards (which are removed from the deck)
-
void returnCards(List<Card> cards)
add cards back into the deck (at the bottom)
param cards a list of the cards to be returned to the deck
-
void shuffleIn(List<Card> cards)
add cards back into the deck and shuffle the resulting deck
param cards a list of the cards to be returned to the deck
-
void shuffleIn(Deck otherDeck)
add another deck to this one and shuffle the resulting deck
param otherDeck the deck to be added to this one
The longest of those
is the constructor --
which must figure out what all 52 cards are
and create them.
The majority of the others can be done in a single line.
Remember to call your own methods where appropriate
rather than copying-and-pasting code.
Remember also
to start with stubs for all the methods above.
Once you have all the stubs written,
compile and run the game.
It won't do much,
but will start to fill out
as you fill in the proper method definitions.
The Game
I have provided the "game" for you.
It doesn't do much --
essentially it deals out hands from a single deck,
then deals out hands from a double deck.
(The hands from the double deck are returned to the bottom of the deck
after being shown to the user,
so you can have as many players as you want,
with up to 104 cards in each hand.)
You should stick with the first game
until you have the Deck class pretty much finished.
The hands in the first game will be sequential cards --
for example,
six hands of five cards might come out as
Player 1 gets [AS, 2S, 3S, 4S, 5S]
Player 2 gets [6S, 7S, 8S, 9S, 10S]
Player 3 gets [JS, QS, KS, AH, 2H]
Player 4 gets [3H, 4H, 5H, 6H, 7H]
Player 5 gets [8H, 9H, 10H, JH, QH]
Player 6 gets [KH, AD, 2D, 3D, 4D]
It doesn't matter if they come out different than that --
so long as you can tell that all 52 cards are there,
and none are duplicated.
After you're sure of that,
you can activate the line in Game
that shuffles the cards.
That should give you hands more like:
Player 1 gets [4S, AD, 3D, 2C, 7S]
Player 2 gets [9H, 8D, 4C, 6D, QC]
Player 3 gets [QS, 3H, 4H, 3S, 10H]
Player 4 gets [9S, 2H, 6S, JS, AS]
Player 5 gets [7C, KH, 7D, 8H, 4D]
Player 6 gets [3C, 8S, 2S, 7H, 6H]
And after you have the shuffling working,
you must revise the Game
Deck
so that the hands are sorted before they are printed out.
Your hands should now look like:
Player 1 gets [AS, 3S, AH, 10H, QH, 2D, 4D, QD, 3C, 6C]
Player 2 gets [4S, 5S, 10S, 4H, 3D, 8D, AC, 2C, 4C, KC]
Player 3 gets [2S, 6S, 8S, 9S, KS, 5H, 9H, 10D, JD, 10C]
Player 4 gets [7S, JS, QS, 2H, 6H, 5D, KD, 7C, JC, QC]
Player 5 gets [3H, 7H, 8H, JH, 6D, 7D, 9D, 5C, 8C, 9C]
(Notice that the cards are sorted by suit
(spades, hearts, diamonds, clubs),
and from lowest to highest within each suit --
that is the way cards just "naturally sort".
Remember,
there is a one-line way to sort your list of cards!)
The game with the double deck
is used to test
the shuffleIn(Deck) method
(obviously),
but also
the returnCards method.
If you use 10 players
with 13 cards each in their hands,
the last two players
should get exactly the same hands as the first two.
For example:
Player 1 gets [7S, 2C, JS, QD, 10C, 9S, 3D, 6D, KC, AS, 2S, QC, 8D]
Player 2 gets [6H, 7H, JH, 5D, 5H, JS, 5S, QD, 2C, 8H, AC, 4H, 3C]
Player 3 gets [7S, 9C, 2D, 2D, 10D, 10S, 5C, 8S, 6C, AD, KD, 7D, 6H]
Player 4 gets [2H, QH, 9H, KS, 10H, JH, 10C, KS, 8D, KH, 9S, 3C, 5C]
Player 5 gets [6S, AD, JC, 8C, 6D, 7H, 5D, 4D, 4C, 4C, 9H, 2H, KH]
Player 6 gets [7C, AC, 3H, 3S, QC, 8S, 4H, JD, JD, JC, 9C, 3D, QS]
Player 7 gets [10S, 4S, 7C, AS, 3H, AH, 9D, KD, 8H, 3S, 4D, 5H, 7D]
Player 8 gets [KC, 10H, QH, 6C, AH, 2S, 8C, 4S, QS, 6S, 9D, 5S, 10D]
Player 9 gets [7S, 2C, JS, QD, 10C, 9S, 3D, 6D, KC, AS, 2S, QC, 8D]
Player 10 gets [6H, 7H, JH, 5D, 5H, JS, 5S, QD, 2C, 8H, AC, 4H, 3C]
SUBMIT
/
Check