Card Game
This series of labs require you to write program code to use the following topics. Tutorials for available topics have been linked.
Data, Data, Data...
We are creating a card game. We will need a deck of cards. Create the following:
- An enum called
Suit with values: HEARTS, DIAMONDS, SPADES, and CLUBS.
- An enum called
CardFace with values: ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, and KING. The CardFace enum has:
int cardValue that will store the value 5 for TWO through NINE; 10 for JACK, QUEEN, and KING; 15 for ACE.
boolean isFace that is true for JACK, QUEEN, and KING; false for all other values.
- A
Card record with a Suit and CardFace.
- A
Deck class with an ArrayList of filled with 52 cards: ACE through KING for each Suit value.
Need help? Consult the following tutorials:
Dealing Cards
Let's deal cards between two players. Complete the following:
- Add a
Game class that has a Deck of cards.
- Add a
Player class that has a String for the name and an ArrayList for their hand. The hand is the cards the player has been dealt.
- Add the
void deal() method that will deal 5 cards from the deck to 2 players. The cards in the player's hand should no longer appear in the deck.
Design Questions to Consider
- What helper methods might you need to have in
Deck and Player to manage extracting cards from a private ArrayList in Deck and adding them to a private ArrayList in Player?
- When dealing the cards, is the
Card object at index 0 the top of the deck or the one at index size() - 1 the top of the deck?
- How are the cards dealt? Alternating one at a time? two at a time? deal all 5?
Additional Updates:
- Add a method to
Game to deal numCards cards to numPlayer players.
- Add a method to display the player's hand.
Our First Game
Create a game where all the cards are dealt between two players. Each player plays a card and the higher card wins the lower card. The winner has both played cards added to their hand. If the two played cards have the same value, each player plays another card until the two cards played are not the same value or one of the players runs out of cards to play.
Design Questions to Consider
- What needs to be adjusted to deal all the cards between two players?
- What additional methods are needed to access values in the ArrayLists across the different class objects?
- How do you break this game into methods that each complete their own task and work together?
- How are you going to test the game to ensure it is working correctly?
Shuffle The Deck
Our game is great, but extremely predictable since the cards are not shuffled.
Different Shuffling the Deck Algorithms
- Algorithm 1: Alternate taking a card from the front and then the back of the deck and adding them to the front of a new
ArrayList. How many iterations would you need to complete in order to call the deck shuffled?
- Algorithm 2: Split the
ArrayList into two lists. Alternate taking cards from the front of each deck and adding them to another ArrayList. How many iterations would you need to complete in order to call the deck shuffled?
- Algorithm 3: Select a random card from the deck and add it to the front of a new
ArrayList. Keep doing this until all the cards have been added. How many iterations would you need to complete in order to call the deck shuffled?
- What other ways can you think of to shuffle the deck of cards?
- Which shuffling algorithm do you feel does the best job?
Need help? Consult the following tutorials: