Graphic by Keith Ohlfs |
|
The game of HiLo has two players: (1) the Chooser, who chooses an integer between 1 and some predetermined upper bound (say 1000); and (2) the Guesser, who attempts to guess the chosen integer in as few turns as possible. On each turn, the Guesser makes one guess at the chosen integer, and the Chooser gives one of three responses:
The game ends when the Guesser guesses the correct value.
HiLo is not particularly exciting to play because there is a simple optimal strategy that narrows the range of possible numbers in half each time by choosing the midpoint of the range. For example, for the range [1..1000], the midpoint guess is 500. If 500 is too high, the next range is [1..499] and next guess is 250; if 500 is too low, the next range is [501..1000] and next guess is 750. With this strategy, it is possible to guess any number between 1 and 1000 in ten or less guesses because 1000 can successively be divided by 2 only ten times before a 1 is reached. (In other words, the base 2 logarithm of 1000 is about 10). This divide-in-half strategy, known as binary search, is extremely important in computer applications for efficiently searching large information spaces.
Even though HiLo may not be much fun to play, it is a good game to implement as a Java program because it nicely illustrates both conditionals and instance variables. In this problem, you will implement the behavior of a program that plays the role of the Chooser; the user of the program plays the role of the Guesser. You will create a user interface layout for the game and must specify how to handle events generated by the user. Below is what the user interface looks like. (You can play a working version of the game by executing the HiLo.html applet within the Test subfolder of the GUI_HiLo folder.)
Pressing the New Game button starts a new game by having the computer choose a random number between 1 and the number chosen in the Choice menu at the top of the screeen. The user then guesses numbers by typing them in next to the Guess button and pressing the Guess button. The computer provides feedback in red depending on the situation; see below for details. If the checkbox labelled Show number of guesses? is checked, then the number of guesses made so far appears in blue below the feedback phrase; otherwise, this area is blank. Furthermore, clicking the checkbox on and off dynamically toggles the blue phrase. The game ends as soon as a correct guess is made; further guesses are ignored until a new game is begun.
There are several kinds of feedback that can be reported in red depending on the configuration of the game. Below is a list of all possible feedback strings with explanations. Italicized elements should be filled in by appropriate numbers.
|
|
|
|
|
|
|
|
|
|
between 1 and upperBound. |
|
Press New Game to start. |
|
Your first task is to create a user interface layout, by placing the various components on the applet window. To start, download the HiLo_programs folder from the download folder. The HiLo applet is in a BorderLayout, and has three panels, one in each of the North, Center and South regions of the applet window. Your job is to fill in the methods that create these panels and add the user interface elements to them. Be aware that the user interface elements are all instance variables that have already been declared at the top of the class, so you do not need to declare them again. The panels are as follows:
Once You have finished the layout of the GUI depicted above, you
must flesh out the action()
method
skeleton in this file so that a user can play a game with the
interface as described above. If you have questions about what the
game should do in a particular situation, run the version in
Test/HiLoTest.html.
Recall that an action method has the following format:
public boolean action (Event evt, Object arg) { if (test for event 1) { statements for event 1 } else if (test for event 2) { statements for event 2 } else... more test/statement pairs here ... } else { return super.action(evt, arg); } return true; }
Your action method should handle the following three events:
Remember that you can check for the component over which an event occured via the boolean expression:
evt.target ==
component-expression
Rather than writing all of your code in the action method itself, it is strongly suggested that you write three auxiliary methods, one for each of the events you need to handle. Then the action method will simply dispatch to your auxiliary methods. This makes the code easier to read, and makes for much better coding style.
Below is documentation on the instance variables and methods you will need to refer to in your solution. You additionally need to know the following facts about strings:
"abc" + "d" + "ef"
yields the string
"abcdef"
.
two
contains the integer
2 and double variable pi
contains the floating point
value 3.141. Then the value of the expression:
"The value of " + two + " + " + pi + " is " + (two +
pi) + "."
is the string
"The value of 2 + 3.141 is 5.141."
Note that if the subexpression
(two + pi)
were not parenthesized, the resulting string would be"The value of 2 + 3.141 is 23.141."
because Java would concatenate the strings for 2 and 3.141 rather than add the numbers.
Here is documentation on the instance variables of the HiLo class:
User Interface Components
Button newGameButton : Pressing this button starts a new game.
Choice upperBoundChoice : Choice menu that determines the upper bound of the computer's choice when a new game is started.
Checkbox showNumberOfGuessesCheckbox : checkbox indicating whether or not the number of guesses should be displayed.
Button guessButton : The user presses this button to make a guess.
TextField guessField : The user enters a guess in this text field.
Label feedbackLabel : Label in which the red feedback string is displayed.
Label guessesLabel : Label in which the blue number of guesses is displayed.
State Variables of the Game
int chosen : The number chosen by the computer for the current game
int guesses : The number of guesses made so far by the user in the current game.
int upperBound : The upper limit of chosen in the current game.
boolean done : If true, no game is in progress; otherwise, the current game is in progress.
Randomizer rand :Random number generator used for choosing number. See notes on the intBetween() method below.
Method Documentation
Here is documentation on various methods that will be helpful in your solution.
Checkbox instance methods
public boolean getState()
Returns the state of this checkbox: true if checked, false if not.Choice instance methods
public String getSelectedItem()
Returns the label of the selected item in this choice menu.Integer class methods
public static String toString (int n)
Returns the string representation of the given integer. E.g.Integer.toString(17)
returns"17"
.
public static int parseString (String s)
Returns the integer corresponding to a given string representation of a number. E.g.Integer.parseInt("17")
returns17
. If s is not interpretable as a number, a run-time exception occurs.Label instance methods
public void setText (String s)
Changes the displayed string of this label to be s.Randomizer instance methods
public int intBetween(int lo, int hi)
Returns a random integer between lo and hi, inclusive.TextField instance methods
public String getText ()
Returns the text in this text field.