CIS 068 / 2: Lab - Assignment #2: High or Low
Program a simple card game, called "High Or Low":
Assume a deck of 7 cards given, the cards are numbered from 1 to 7.
After shuffling, the uppermost card of the deck is presented to the player, who now has to decide,
if the next card will be higher or lower in value.
if he guessed right, the game is continued, i.e.
he again has to guess, if the third card is higher/lower than the second one a.s.o.
The player wins, if there are no more cards left to uncover.
He loses, if he precalculated incorrectly.
A typical output of the game would be:
Welcome to High Or Low.
Shuffling...
Uncovered cards: 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, as developed in the Wednesday class:
- 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.
- We discovered 3 objects: the User, the Interface to the user (the game itself, called HighOrLow) and the
Deck. So the program will consist of 2 classes:
- class HighOrLow
- class Deck
The use-cases (Initial, Player's Decision, Lost, GoOn) led to 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.
...of course, we were in an early phase of detailed analysis, maybe it will show, that there are some methods
missing or must be adjusted...
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 get a random number myRandomNumber 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');
}
We will discover more problems in the friday-lab, maybe some more help will be added here. So stay online !