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

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');
}