A8: Guessing Game

On This Page


Overview

During this assignment, you will:

  • Write a "guess the number" game
  • Decide how to organize your program into methods
  • Write a method to validate user input

Sample Output

I'm thinking of a number between 1 and 100.
Enter your guess (Ctrl-C to end): 50
50 is too low. Try again: 75
75 is too high. Try again: 67
67 is too low. Try again: 71
71 is too low. Try again: 73
73 is too low. Try again: 74
You guessed the number!
Either you know the secret or you got lucky!
Try again? (y/n): y

I'm thinking of a number between 1 and 100.
Enter your guess (Ctrl-C to end): 50
50 is too low. Try again: 75
75 is too low. Try again: 82
82 is too low. Try again: 90
90 is too low. Try again: 95
95 is too high. Try again: 93
93 is too low. Try again: 94
You guessed the number!
Congratulations! You know the secret!
Try again? (y/n): n

Thanks for playing!

Operation

  • The application prompts the user to enter an int value from 1 to 100 until the user guesses the random number that the application has generated.
  • The application displays messages that indicate whether the user’s guess is too high or too low.
  • When the user guesses the number, the application displays the number of guesses along with a rating. Then, the application asks if the user wants to play again.
  • When the user exits the game, the application displays a goodbye message.

Specifications

  1. Write a program that plays "guess the number" like that shown in the Sample Output section and as as described in the Operation section.
  2. Your program randomly chooses a number to guess between 1 and 100. Use the random() method of the java.lang.Math class to generate a random number.
  3. Your program displays instructions and a prompt to guess a number.
  4. If the player types an incorrect guess, your program displays the number and whether it is too high or too low. This allows the player to "zero in" on the answer.
  5. When the player guesses the correct answer, your program evaluates their play based on the number of guesses made.
  6. If the count is less than 7, display "Either you know the secret or you got lucky!"
  7. If the count is exactly 7, display "Congratulations! You know the secret!"
  8. Otherwise, display "You should be able to do better!"
  9. The name of the program source code file must be GuessingGame.java.
  10. Your program must validate user input and gracefully handle all error conditions.
  11. The program must have the following methods with the exact headings shown:
/**
 * Creates a new random number to guess
 *
 * @return The new random number to guess.
 */
public static int calcRandomNumber()

/**
 * Evaluates the player's game base on the number of tries
 * needed to guess the number and displays appropriate
 * comments.
 *
 * @param count The number of tries needed to guess the number.
 */
public static void evaluateGame(int count)

/**
 * Prompts for an int and verifies a correct value is entered.
 * Exits the program if the Ctrl-C buttons are pressed.
 *
 * @param prompt The message prompt to display.
 * @return The value entered by the user.
 */
public static int readInt(Scanner input, String prompt)

/**
 * Prompts for a String and returns the String entered.
 * Exits the program if the Ctrl-C buttons are pressed together.
 *
 * @param input The scanner from which to read input.
 * @param prompt The message prompt to display.
 * @return The value entered by the user.
 */
public static String readString(Scanner input, String prompt)

Extra Credit

The following are worth extra credit points:

  1. Explain in your README.txt file why it should take no more than 7 guesses for 100 numbers. (1 point)
  2. Add a method named readChoice() that when the user responds to the Play Again prompt, the application only accepts a value of "y" or "n". (2 points)
  3. Play again? (y/n):
    Error! This entry is required.
    Play again? (y/n): x
    Error! Entry must be "y" or "n".
    Play again? (y/n): n
    

Make certain that your README.txt file describes any extra credit attempted.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.

Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the course information page.

Program Compilation

  • 4: Source code compiles with no errors or warnings
  • 2: Source code compiles with warnings
  • 0: Does not compile

Functionality

  • 10: Demonstrates mastery of the assignment
    • Has extra features or demonstrates techniques beyond the assignment
    • Applies concepts from the lessons appropriately
    • Meets all specifications (see above) with particularly elegant solutions
    • Runs to completion with no abnormal error conditions
    • Generates correct output given correct input
    • Behaves in a reasonable way in response to incorrect data
    • All test cases pass
  • 8: Has all the functionality expected of the assignment
    • Demonstrates many techniques from the lesson
    • Meets all specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
    • All test cases pass
  • 6: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets all but one of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
    • All but one test case passes
  • 4: Has some of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have more than 3 minor errors
    • At least 1/2 of all test cases pass
  • 2: Serious functional problems but shows some effort and understanding
    • Meets less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
    • Less than 1/2 of all test cases pass
  • 0: Does not execute

Program Style

  • 4: Code is well-documented
  • 3: Code has minor documentation errors
    • Has 1 documentation error
  • 2: Code has some documentation errors
    • Has 2-3 documentation errors
  • 1: Code has many documentation errors
    • Has more than 3 documentation errors
  • 0: No apparent attempt to document code

README.txt File

  • 2: README.txt file submitted with specified information included
  • 1: README.txt submitted but some information was not included
  • 0: No README.txt submitted

Maximum Score: 20, plus extra credit

What to Turn In

Submit your assignment following the instructions for homework. Include the following items for grading:

  1. README.txt file
  2. GuessingGame.java
  3. Any other source code needed to make your program function.

You must submit all the files needed to make your assignment function properly. Do not assume that the instructors has any files unless explicitly stated by the instructor. Your assignment must work as submitted.

Home | WebCT | Announcements | Schedule | Expectations | Course info
Help | FAQ's | HowTo's | Links

Last Updated: November 02 2005 @13:49:30