Assignment 1: High or Low
Description
Program a simple card game, called "High Or Low":
Assume a deck of 7 cards, the cards are numbered from 1 to 7.
After shuffling, the uppermost card of the deck is presented to the player, who has to decide
if the next card will be higher or lower in value. If he/she guessed right, the game is continued, i.e.
the player may continue with another guess about the value of the third compared to the second card.
The player wins, if there are no more cards left to uncover.
He/She loses, if the guess was wrong.
A typical output of the game would be:
Welcome to High Or Low.
Shuffling...
Uncovered card: 3 --> (h)igh or (l)ow ? h
uncovered cards: 3 5 --> you were right ! 5 to go. (h)igh or (l)ow ? l
uncovered cards: 3 5 1 --> you were right ! 4 to go. (h)igh or (l)ow ? l
uncovered cards: 3 5 1 2 --> sorry, you were wrong ! You lost this game.
Bye.
Requirements
- We don't want a graphical user interface, simple console input/output
- The number of cards in the deck is 7, no variable number necessary.
- You HAVE TO program this project using 2 classes: the game itself, called HighOrLow, and the
Deck.
If you would write down use cases(Initial, Player's Decision, Lost, GoOn), they would suggest the following methods:
class HighOrLow:
- main
- Constructor --- this one is discussable. Because we don't go for an event driven approach, the games' logic
will be put in the main-method, there's no real need for an instance.
- compareDeckToInput --- this one compares the last two uncovered cards to the user's input. the result should
be boolean, saying if the user was right or not
- requestInput from user
- exit
class Deck:
- Constructor
- shuffle -- shuffling the deck, algorithm see below
- getNextCard -- uncover next card, return the card's value
- moreCards -- return true, if there are more cards to uncover
- show -- the deck prints out itself, i.e. console-printout of uncovered cards and number of covered cards.
Your program design should follow these design suggestions (I'll be less strict in the design of following projects).
As we discussed in class, the shuffling of the deck can be in 2 ways: you either have a method 'shuffle()', and use getNextCard()
to draw cards subsequently from top to bottom, or you don't shuffle first but draw cards from random positions. Both is ok.
Algorithm for Shuffling
the most natural data structure for the deck is an array of int. So let's define:
int[] theDeck = {1,2,3,4,5,6,7};
one typical algorithm to shuffle would be:
do 50 times:
randomly select 2 cards and swap them
end
Some Help
You can compute a random number between [0..6] (zero and six included) by:
int myRandomNumber = (int)(Math.floor(Math.rand() * 7));
a useful method:(java.io.* must be imported !)
/* requestUserInput
// read keyboard,
// return true if input was 'h'
// return false if input was 'l'
*/
private boolean requestUserInput()
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = "";
while (line.length() != 1 || (line.charAt(0) != 'h' && line.charAt(0) != 'l')) {
System.out.print("(h)igh or (l)ow ? ");
try{
line = in.readLine();
}catch(Exception e)
{}
}
return (line.charAt(0) == 'h');
}