CPSC 120 -- Assignment #3

Higher or Lower?
Due Friday, November 18, 2011 by 4 p.m.

Write a program to play a card game named Higher or Lower. In this game a player starts with 40 points and a card showing. The player then guesses whether the next card will be higher or lower and places a bet (a certain number of points) on the guess. If the guess is correct the bet added to the player's score, otherwise it is subtracted unless the new card is the same rank in which case no points are added. There is a bonus of 2 points added if the suit of the next card matches the suit of the current card. Play continues, each time the player guessing if the next card will be higher or lower than the previous one, until a predefined number of cards have been drawn or the player has 0 points.

Our game will have the user play against the computer. The computer will play with a well-defined set of rules for its bet and its guess (these are described below).

The Specifics:

You must write a Card class to model a card from an ordinary deck of cards and a main program to play the game.

The Card Class: A card has two attributes - a suit and a rank. Hence a card should have two instance variables.

The Card class must provide the following methods:

Note: If you use the Random class to generate the random values in the constructor you should instantiate the generator object as follows (where you declare your instance variables, not in the constructor method):

    private static Random generator = new Random();
(Instead you may use the random method in the Math class if you wish.)

Use the CardTest.java program to test your Card class. You will need to fill in some code.

The main program to play the Higher - Lower Game: The basic outline of the program is as follows:

The user chooses a number of cards to play, then the computer takes a turn playing the specified number of cards, then finally the user takes a turn playing the specified number of cards.

A Turn: The following take place on a turn (either computer or person):

  1. The number of points starts at 40
  2. A random card is displayed.
  3. The player guesses higher or lower.
  4. The player places a bet on their guess. The bet must be between one-fourth of the current points (rounded down) and the current points. So if at some point the player has 55 points the the bet must be a value between 13 and 55.
  5. A new random card is displayed.
  6. If the player was correct the amount of the bet is added to the points for the player. That is, if the player guessed higher and the rank of the new card is higher than the rank of the current card the amount of the bet is added. Similarly, if the player guessed lower and the rank of the new card is lower than the current card the points are added. (Note: An Ace is the lowest card.) The bet is subtracted if the player guessed higher but the next card is lower or if the player guessed lower but the next card is higher. No points are added or subtracted if the ranks are the same.
  7. If the last card displayed has the same suit as the previous one 2 points are added to the player's score.
  8. The last card drawn now becomes the current card and the player will guess if the next one will be higher or lower. That is, play continues with step 3 above (the user guesses higher or lower).
  9. Play continues until the appropriate number of cards have been played (after the initial card displayed) or the player has 0 points.

Sample Output: Click here to see sample output.

Suggested Working Strategy

Do this assignment incrementally. The following is a suggestion of the order to work.
  1. Write the Card class (maybe add only a couple of methods at a time) and make sure it compiles.
  2. Add code to the CardTest.java program and run the program several times to make sure your Card class works.
  3. Start the program to play the game writing the code for just one of the players to play its turn (you can write the code for either the computer or the person player). Run that several times to make sure it is working right.
  4. Now add the code for the other player. For this you can duplicate the code for the other player and just make a few changes. Test the program thoroughly.
  5. Add the code to validate the input.
  6. Clean up the code and the output; that is, format it nicely! And, of course, make sure the program is properly documented.

Additional Requirements:

Submit to Inquire: A zip file containing your program.

Academic Integrity Reminder!!! Programming assignments are to be your own work. You may get help on the specifics of the assignment from no one except the instructor. You may not show your program to anyone or look at anyone else's program or share ideas with anyone about how to write the program.