...
[rmh3093.git] / lab1 / Activity2 / GreaterLessGame.java
blob5a59daa4e6425d63e6a90ac757aca1161c113e60
1 /*
2 * GreaterLessGame.java
4 * Version:
5 * $Id: GreaterLessGame.java,v 1.5 2008/03/13 18:51:11 rmh3093 Exp $
7 * Revisions:
8 * $Log: GreaterLessGame.java,v $
9 * Revision 1.5 2008/03/13 18:51:11 rmh3093
10 * Fix run time issues
12 * Revision 1.4 2008/03/13 18:38:10 rmh3093
13 * fix compile issues
15 * Revision 1.3 2002/12/04 17:01:28 cs2
16 * Corrected typo -- ptt
20 import java.io.BufferedReader;
21 import java.io.InputStreamReader;
22 import java.io.IOException;
23 import java.util.Random;
25 /**
26 * Play the game of "Greater than, less than". The computer will think of
27 * a random number from 0 to 100 and you try to guess it. The computer
28 * will give you clues by telling you whether your guess is greater or
29 * less than the number it is thinking of. You have 25 tries to guess
30 * the number.
32 * This program contains errors!!
34 * @author Aleksey Tentler
37 public class GreaterLessGame {
39 // The maximum number the computer will guess
40 private static final int MAX_GUESS = 100;
42 // The number of guesses a user can make
43 private static final int NUMBER_OF_TRIES = 25;
45 // Set this constant to true to see the secret number
46 private static final boolean SHOW_SECRET_NUMBER = false;
48 /**
49 * This method plays the game, keeping track of how many tries are used.
51 * @param secret the number the computer is thinking of.
53 private static void playTheGame( int secret ) {
54 int tries = 0; // Number of tries
55 boolean correct = false; // Did the user guess the number
57 // Give the user 25 tries to guess the number
58 while ( !correct && tries <= NUMBER_OF_TRIES ) {
59 // Calls isGuessCorrect to check whether the guess is correct
60 // Obtains the user's guess by calling getGuess
61 // You can assume that the getGuess function works correctly
62 // isGuessCorrect, however, may have errors
63 correct = isGuessCorrect( getGuess(), secret );
64 tries++;
67 // Check whether the user won or lost
68 if( correct ) {
69 System.out.println( "\nYou got it! The answer is " + secret );
70 System.out.println( "\nYou did it in " + tries + " tries." );
72 else {
73 System.out.println( "\nYou took too long. You lose!");
77 /**
78 * Checks whether the guess is correct and prints whether the number
79 * is smaller or greater than the guess.
81 * @param secret the computer's secret number.
82 * @param guess the user's guess.
84 * @return true if the guess is correct, false otherwise.
86 private static boolean isGuessCorrect( int guess, int secret ) {
87 boolean retVal = (secret == guess); // Value to return
89 if( secret < guess ) {
90 System.out.println( "The number is less than " + guess );
92 else {
93 System.out.println( "The number is more than " + guess );
96 return retVal;
99 // --------------------------------------
100 // EVERYTHING BELOW THIS LINE IS CORRECT!
101 // --------------------------------------
103 private static BufferedReader in = null; // Source of input
106 * Play the game.
108 * @param args seed for random number geneator.
111 public static void main( String args[] ) {
112 Random rand = new Random(); // Source of random numbers
114 // See if a seed was specified on the command line
115 if ( args.length == 1 ) {
116 try {
117 rand.setSeed( Long.parseLong( args[ 0 ] ) );
119 catch ( NumberFormatException e ) {
120 System.err.println( "Invalid seed" );
121 System.exit( 1 );
125 // Connect streams to standard input
126 in = new BufferedReader( new InputStreamReader( System.in ) );
128 // Guess a secret number. Note that nextInt() returns an
129 // an integer between 0 and N-1 which is why we must use
130 // MAX_GUESS+1
131 int secret = rand.nextInt( MAX_GUESS + 1 );
133 // Debugging help
134 if( SHOW_SECRET_NUMBER ) {
135 System.out.println( "\n[ The secret number is " + secret + "]\n" );
138 // Play the game
139 playTheGame( secret );
143 * Read the next valid number from the input stream.
145 * @return the next valid integer in the input stream.
147 private static int getGuess() {
148 int guess = -1; // The number that was entered
150 // Read until a valid number is entered
151 do {
152 System.out.print( "Enter a number between 0 and 100: " );
154 try {
155 guess = Integer.parseInt( in.readLine() );
157 if ( guess < 0 || guess > MAX_GUESS ) {
158 System.out.println( "Number out of range." );
159 guess = -1;
162 catch( NumberFormatException e ) {
163 System.out.println( "Not an integer." );
164 } catch( IOException e ) {
165 System.out.println( "Bad Input!\n" );
168 } while( guess < 0 );
170 return guess;
173 } // GreaterLessGame