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.
- int suit - represents the suit (for example 1 means Spades, 2 means Hearts, etc.)
- int rank - represents the rank (1 - 13 with 1 meaning Ace, 11 meaning Jack,
12 meaning Queen, and 13 meaning King)
The Card class must provide the following methods:
- Card() - a constructor that initializes the Card to random
values (a random value in the range 1 - 4 for suit and 1 - 13 for rank)
- String toString() - returns a string representation of the Card,
for example "3♥" or "K♠". You can use the Unicode
characters for card suits by using string escape sequences
(♠ = "\u2660", ♣ = "\u2663", ♥ = "\u2665",
♦ = "\u2666")
- int getRank() - Returns an integer representing the card's rank
(1 - 13)
- int getSuit() - Returns an integer representing the card's suit
(1 - 4)
- boolean sameSuit(Card otherCard) - returns true if the card
has the same suit as the other card which is passed as a parameter
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):
- The number of points starts at 40
- A random card is displayed.
- The player guesses higher or lower.
- The computer
will always guess higher if the rank of the current card is
less than 7 and guess lower otherwise.
- The person player
will enter a choice (have the player enter a number - for
example 1 for higher and 2 for lower).
- 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.
- The computer will always bet the minimum amount.
- The person player will enter a number in the
appropriate range for a bet. Your program must tell the user
what that range is and include a validation
loop to make sure the user enters a correct value.
- A new random card is displayed.
- 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.
- If the last card displayed has the same suit as the previous one
2 points are added to the player's score.
- 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).
- 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.
- Write the Card class (maybe add only a couple of methods at a time)
and make sure it compiles.
- Add code to the CardTest.java program and run the program several
times to make sure your Card class works.
- 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.
- 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.
- Add the code to validate the input.
- Clean up the code and the output; that is, format it nicely! And,
of course, make sure the program is properly documented.
Additional Requirements:
- Your program must use good programming practices as described
in previous assignments (such as meaningful identifiers,
constant identifiers, white space, documentation, etc.).
- Documentation for the Card class must include header documentation
at the beginning of the file that contains the name of the class,
your name, the date, and a description of the class. Each method
must have header documentation describing what the method does,
what parameters are expected and what, if anything, the method
returns.
-
Indent and align your code properly (emacs helps you do this - it automatically
indents in most cases -- if a line isn't indented correctly go to it
and press TAB OR select the whole program using CTRL-x, CTRL-p then
use ALT-CTRL-\. If TAB doesn't make it indent correctly you probably have
a syntax error).
- See the following
code conventions for more detail about formatting
and documenting your program.
- Warning: A program that doesn't compile receives almost no credit
no matter how small the error.
- Optional (Extra Credit): Add an outer loop that lets the user
play the game against the computer
as many times as he/she wishes and keeps score (counts the times
the computer wins and the times the computer loses).
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.