COMP 15
Programming Assignment 6:
The Java Guessing Game
- Assigned:
- Monday Nov 29, 1999
- Due:
- Tuesday Dec 7, 1999
Note: This assignment is to be completed individually. No collaboration
is permitted.
Problem Description
In this second and final Java programming assignment you are asked
to construct a simple interactive game in which the objective is
for you to guess a number chosen by the computer. For each guess
you make, the computer reports whether your guess is too high or too
low. When you determine the correct number, the game starts over again
with a new number. This continues until you are sick of the game
at which point you indicate that you want to quit, and some statistics
are printed. A sample run of the program is shown at the end of this
page (user inputs are shown in bold font).
Supplied Solution Components
You can obtain the zip file prog6.zip from the
the class web page.
Create a folder called GuessGame in a safe place
on your computer and use winzip to unzip this file into
that folder.
When this is complete, the folder will contain a skeleton VJ++
project that you can edit to complete this problem.
To work on the project, double-click on GuessGame.vjp in the
GuessGame folder.
The project contains a single
Main class with several class (static) methods, incuding a
main method.
The project can be compiled and run as distributed, but only prints
instructions for the game and then arrives at the last line of the
run above. Your job is to insert the missing functionality.
You need not be concerned with how to read input
from the keyboard; use the prompt method supplied in the
project. An
example use of prompt can be found on the last line of the
main procedure. Note that import java.io.*
is required at the head of the Main class to
provide access to the methods used within
prompt.
You will also find a file P6demo.exe in the GuessGame
folder.
This is a ready-to-run solution for this programming assignment, which
you can use to check how your program should behave. Double-click this
application to try it out. In this version the computer chooses numbers
between 1 and 10 (as shown above) to simplify experimenting with the
program. To make your own program more interesting, you may wish to set
max to 100, which should cause the computer to
pick numbers in the range 1 to 100, inclusive.
While your program should generate output that closely parallels the
output of the demo program, it is not necessary to precisely match the
spacing and formatting of the sample program.
Random numbers in Java
The generation of random numbers is quite a bit simpler in Java than
in Haskell. The class method Math.random() returns
a (different) random double r such that
0 <= r < 1,
each time it is called. The class Math is implicitly available
in Java, so no import statement is required to use this method.
Extra Credit
A simple improvement you can make to your program is to keep track of
the range to which the search has been narrowed by the player guesses
and computer responses responses generated
thus far in a game. This could take the form of values shown in
the prompt and/or a warning if the player makes a guess that yields no
new information.
Submission Procedure
The times available for submission of program 6 are
- Monday, December 6, 4-6PM in SN030
- Tuesday, December 7, 4-6PM in SN030
A sample execution of the Guessing Game |
Welcome to the guessing game.
I will choose a number between 1 and 10 ... and you try to guess it.
With each guess, I will tell you whether you are high or low.
The objective is to find the number using as few guesses as possible.
Let's start!
I am thinking of a number between 1 and 10.
Enter guess (or 0 to quit): 5
Your guess is too low.
Enter guess (or 0 to quit): 8
Your guess is too high.
Enter guess (or 0 to quit): 6
Your guess is too low.
Enter guess (or 0 to quit): 7
*** Correct! You got it in 4 guesses!
Let's play again!
I am thinking of a number between 1 and 10.
Enter guess (or 0 to quit): 5
*** Correct! You got it in 1 guesses!
Let's play again!
I am thinking of a number between 1 and 10.
Enter guess (or 0 to quit): 0
You played 2 games and used an average of 2.5 guesses per game
Thanks for playing. Enter any number to close this window: 0
|
$ Revised: Mon Nov 29 1999 by prins@cs.unc.edu