Card Game
Create a deck of cards and write your own card game.
Required Knowledge
This series of labs require you to write program code to use the following topics. Tutorials for available topics have been linked.
- Intro to enum
- Iterating Over enum Values
- Introduction to Records
- Creating Record Objects and Calling Methods
- Class Data and Behaviors
- Class Creation
- Constructors and Methods in enum
- Introduction to Lists and ArrayList
- Using SequencedCollection class
- Formatting output with Textblocks
- Formatting output with Escape Sequences
Missions
Mission 1: Data, Data, Data...
We are creating a card game. We will need a deck of cards. Create the following:
- An enum called
Suitwith values:HEARTS,DIAMONDS,SPADES, andCLUBS. - An enum called
CardFacewith values:ACE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN, andKING. TheCardFaceenum has:int cardValuethat will store the value5forTWOthroughNINE;10forJACK,QUEEN, andKING;15forACE.boolean isFacethat istrueforJACK,QUEEN, andKING;falsefor all other values.
- A
Cardrecord with aSuitandCardFace. - A
Deckclass with anArrayListof filled with 52 cards:ACEthroughKINGfor eachSuitvalue.
Need help? Consult the following tutorials:
- Intro to enum
- Iterating Over enum Values
- Introduction to Records
- Creating Record Objects and Calling Methods
- Class Data and Behaviors
- Class Creation
- Constructors and Methods in enum
Mission 2: Dealing Cards
Let's deal cards between two players. Complete the following:
- Add a
Gameclass that has aDeckof cards. - Add a
Playerclass that has aStringfor the name and anArrayListfor 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
DeckandPlayerto manage extracting cards from aprivate ArrayListinDeckand adding them to aprivate ArrayListinPlayer? - When dealing the cards, is the
Cardobject at index0the top of the deck or the one at indexsize() - 1the 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
Gameto dealnumCardscards tonumPlayerplayers. - Add a method to display the player's hand.
Mission 3: 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?
Mission 4: 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
ArrayListinto two lists. Alternate taking cards from the front of each deck and adding them to anotherArrayList. 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:
Mission 5: Creating Card Visuals with Output
To make this game more usable, it would be great to have visuals of the cards. You could do this in many ways. You could use images, JavaFX, or in the case of this exercise, textblocks.
- Create an output statement that would make the ace of hearts using textblocks.
- Create an output statement that would make the ace of hearts using escape sequences.
Design Questions to Consider
- Which method of creating the text based image of a card do you prefer?
- What class would you need to alter to include a image or output to represent the card?
- What would be the best way to store the card image?
Need help? Consult the following tutorials: