Homework 2

due: Thursday February 27 at 11:59pm (submit on Gradescope here).

Goals:

FaceUp Card Game

In this assignment we will implement a made-up card game we'll call FaceUp. When the game starts, you deal five cards face down. Your goal is to achieve as high a score as possible. Your score only includes cards that are face up. Red cards (Hearts and Diamonds) award positive points, while black cards (Clubs and Spades) award negative points. Cards 2-10 have points worth their face value. Cards Jack, Queen, and King are worth 10 points, and Ace is worth 11 points.

The game is played by flipping over cards, either from face-down to face-up or from face-up to face-down. As you play, you are told your total score (i.e. total of the face-up cards) and the total score of the cards that are face-down. The challenge is that you only get up to a fixed number of flips and then the game is over.

Here is an example of the output from playing the game:

FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 0
Face down total: -5
Number of flips left: 5
Pick a card to flip between 1 and 5 (-1 to end game): 1

Queen of Hearts | FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 10
Face down total: -15
Number of flips left: 4
Pick a card to flip between 1 and 5 (-1 to end game): 2

Queen of Hearts | Jack of Clubs | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 0
Face down total: -5
Number of flips left: 3
Pick a card to flip between 1 and 5 (-1 to end game): 2

Queen of Hearts | FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 10
Face down total: -15
Number of flips left: 2
Pick a card to flip between 1 and 5 (-1 to end game): 10

10 is not a valid card
Queen of Hearts | FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 10
Face down total: -15
Number of flips left: 2
Pick a card to flip between 1 and 5 (-1 to end game): 0

0 is not a valid card
Queen of Hearts | FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 10
Face down total: -15
Number of flips left: 2
Pick a card to flip between 1 and 5 (-1 to end game): 5

Queen of Hearts | FACE-DOWN | FACE-DOWN | FACE-DOWN | 3 of Hearts
Face up total: 13
Face down total: -18
Number of flips left: 1
Pick a card to flip between 1 and 5 (-1 to end game): -1

----------------------
Your score: 13
Best possible score: 15

At each point where the program asks the user to pick a card, the game waits for user input.

Code

We’ll implement the game using four classes, two of which you'll leave unchanged, and two you will modify and submit. Note that the FaceUpCard.java and CardDealer.java files have some pieces that might help you with Lab 2. Download the starter code here. As we have been doing, move the extracted hw2 folder to your cs201 folder and open this folder in a new VS Code window.

The TODO comments in FaceUpCard.java and FaceUpHand.java mark what you need to implement in this assignment.

How to proceed

You may fill in the details of the FaceUpCard and FaceUpHand classes however you’d like, but here’s what we recommend.

  1. Complete the two methods (marked as TODO) to the FaceUpCard class (described in comments in the code). Add a main method to this class and test the two methods to make sure they work.
  2. Read through the other two new classes FaceUpHand and FaceUpGame. Make sure you understand what all of the methods in FaceUpHand are supposed to do and that you understand how they're used in the FaceUpGame class.
  3. Implement the required methods (marked as TODO) in the FaceUpHand class incrementally. Write one method and then test it. Again, add a main method and write a small test or two to make sure it behaves like you'd expect (see some examples in the next section). If you try to implement all of the methods at once and then test it by jumping to running FaceUpGame, 1) it’s unlikely to get everything working the first time and then 2) it’s going to be very hard to debug where the problem is.
  4. Once you have things working, play the game!

Testing your FaceUpHand implementation.

We recommend adding a main method in the FaceUpHand.java file to test the FaceUpHand class. You can explicitly create some cards in the hand, call the methods you implemented and then ensure the number of points match the expected values.

Here are some example hands (each an instance of FaceUpHand), and their optimal scores:

Ace of Spades, 3 of Hearts, 6 of Hearts, King of Hearts, 3 of Diamonds
Best possible score: 22

4 of Clubs, 8 of Hearts, Jack of Diamonds, 8 of Clubs, 6 of Diamonds
Best possible score: 24

3 of Spades, 5 of Diamonds, 3 of Diamonds, 7 of Spades, 5 of Spades
Best possible score: 8

Ace of Diamonds, 10 of Spades, 4 of Diamonds, 8 of Clubs, 9 of Spades
Best possible score: 15

3 of Clubs, 3 of Spades, 2 of Hearts, 4 of Spades, Jack of Diamonds
Best possible score: 12

9 of Spades, 5 of Clubs, Jack of Clubs, 2 of Clubs, Jack of Diamonds
Best possible score: 10

King of Diamonds, 3 of Diamonds, King of Hearts, 7 of Clubs, Ace of Hearts
Best possible score: 34

7 of Clubs, 6 of Spades, 4 of Clubs, 3 of Hearts, 3 of Spades
Best possible score: 3

Jack of Clubs, 6 of Clubs, 8 of Clubs, 7 of Spades, Queen of Spades
Best possible score: 0

10 of Hearts, Ace of Diamonds, Queen of Spades, Jack of Clubs, Queen of Clubs
Best possible score: 21

Submission

Submit FaceUpCard.java and FaceUpHand.java to Gradescope (here) (unlimited attempts). We won't check for style yet, but try to stick to the guidelines we introduced in class.