![]() |
Assignment 9TaskWrite a simple text-based number-guessing game. Details:OverviewYou are to write a game that generates a random number between 1 and 80. The game then prompts the user to guess the number. If the user's guess is correct, the game congratulates the user and ends. If the user's guess is not correct, the program will report whether the guess is too high or too low. It will also draw a simple text-based line, showing the user's guess (marked with a '|'), the target random number (marked with a '^'), and the distance between them (marked with '-'s). Here is the output of a sample game, with user input marked in green: I'm thinking of a number between 1 and 80. Can you guess what it is? Pick a number: 1000 Sorry, but 1000 is not a number between 1 and 80. Try again. Pick a number: 5 You are too LOW: |------------------------------------------------------^ Pick a number: 65 You are too HIGH: ^----| Pick a number: 61 You are too HIGH: ^| Pick a number: 60 CORRECT! You guessed it. Most console windows are 80 characters wide by default (hence the 1 to 80 range). Depending on your current Eclipse settings you might need to resize your console window a bit to prevent the line from wrapping. NumberGuesser.java
There are no code structure requirements for this assignment. You can just make one big If you're stuck and would like some guidance, here's the structure I used when writing this program: public class NumberGuesser { //class and instance variables, including: public static final int MAX_NUM = 80; private Scanner input = new Scanner(System.in); private int number; //the random number to be guessed private int guess; //the user's current guess public static void main(String[] args) { NumberGuesser game = new NumberGuesser(); game.playGame(); } public void playGame() { //explain the game rules to the user //generate a new number to guess: this.number = (int) (Math.random() * MAX_NUM) + 1; loop (until user guesses the correct answer) { this.guess = this.getUserGuess(); if (guess == number) { //user got the correct answer (so tell them so) }else { //need to report whether they are too HIGH or too LOW; //then graphically display their guess: this.displayGuess(); } } } public int getUserGuess() { int entered = 0; loop (until user enters a valid number b/w 1 and MAX_NUM) { //prompt user to enter a number try{ entered = input.nextInt(); input.nextLine(); //clear input stream if (entered < 1 || entered > MAX_NUM) { //report error and let the loop prompt user again } }catch (InputMismatchException ime) { System.out.println("Sorry, that is not a valid integer. Try again."); input.nextLine(); //clear input stream of bad data } } return entered; } public void displayGuess() { loop (for each character in the line, so i goes from 1 to MAX_NUM) { if (i equals guess) { //print a ^ character }else if (i equals number) { //print a | character }else if (i is less than both guess and number OR i is greater than both guess and number) { //print a ' ' (space) character }else { //i is between guess and number, //so print a '-' character } } //add a final newline character to finish the line } }
I've highlighted the NumberGuesser members--variables and methods--in blue. Also, you will need to decide how to write the Again, this pseudo-code just presents the way I went out about solving this problem. There is always more than one way to do it! So if a different algorithm makes more sense to you, go ahead and do it your way. You may still be able to pilfer a few tips from the code above, such as how to generate the random number or how to draw the guess line.
Also, in How to write your programDo not just cut and paste all the code above into a file and start editing it. If you do that, you're not going to be able to compile and run it until you've written the complete program. This will make it much harder for you to ever get it working. (Really. Trust me.) You should program step-by-step. After each step, compile and run your program to make sure everything you've done so far works correctly. To get started, I'd suggest you do the following:
Good luck! What to SubmitAttach your NumberGuesser.java file to an email. FAQ
GradingOut of 10 points:
There's also a little extra credit possible for this assignment:
|
~ztomasze Index :
TA Details: ICS111:
Assignment 9 http://www2.hawaii.edu/~ztomasze |
Last Edited: 03 Nov 2007 ©2007 by Z. Tomaszewski. |