HiLow Game

Assignment Purpose:

The purpose of this assignment is to practice implementing an interface and the binary search algorithm.

Part 1

A Simple game where the Applet tries to guess the number you are thinking of.

Your browser is ignoring the <APPLET> tag!


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;

 * Class HiLow - Number Guessing Game
 * @author Chris Thiel, OFMCap
 * @version 28 Feb 2007
 * The idea is that this Applet will make a new guess and keep track of
 * how many guesses it takes to guess a number that the user picks.
 * The user will say higher, lower, correct or Start Over
 * As a starter, I have made the user interface. The Applet implements
 * the ActionListener Interface.  You can see that the only method required
 * to do this is actionPerformed(ActionEvent e).  You can also see that that all the 
 * information is contained in  ActionEvent e.  It is up to you if you want to keep 
 * the timing information.  
 * You can see that we add an
 * ActionListener to each of the four buttons, 
 * and each time we tell it 
 * to process the ActionEvent with this Class' 
 * implementation of the ActionEvent interface. 
 * All the buttons will send any ActionEvents 
 * to our implementation right here.  So it is just a matter of
 * adding some intance variables to keep track of a few things, and
 * come up with a way to make a new guess.
 * You need to:
 * 1-keep track of how many guesses
 * 2-make guesses based upon Higher or Lower clues from the user.  
 *   To start with, just add or subtract a number (or even a random number!)
 * 3-reset the guessing process when done (either by a sucessfull guess or the
 *   user's request to start over.
 * For extra points:
 * 4-make the guess between 1 and 100 (or even more!)
 * 5-use the binary serach algorithm to make your guesses.
public class HiLow extends Applet implements ActionListener 
    // instance variables - replace the example below with your own
    TextField output;
    TextArea textArea;
    Button higher;
    Button lower;
    Button correct;
    Button reset;
    long lastTime;
    public void init()
        Date d=new Date();
        textArea = new TextArea();
 		textArea.setText("Pick a Number from 1 to 10.  Is it 7?\n");
 		higher = new Button("Higher");
 		lower = new Button("Lower");
 		correct = new Button("Correct!");
 		reset = new Button("Start Over");
        Panel buttonPanel= new Panel();
        buttonPanel.setLayout(new GridLayout(0,1)); //single column
        setLayout(new BorderLayout() );
        add (buttonPanel, BorderLayout.SOUTH);
        add (textArea, BorderLayout.CENTER);
    public void actionPerformed(ActionEvent e) 
        long thisTime=e.getWhen();
        long timeLapsed=thisTime-lastTime;
        textArea.append(timeLapsed+" milliseconds lapsed\n");


Part 2: Use the binary search algorithm

The idea is that you keep track of the range of possibilities, and split the difference. For example, lets say we are going to guess a number between 1 and 100.

Lets set low=1 and high=100. then the first guess

Now these are all type int, so even though (1+100)/2=50.5, guess gets 50 (the fractional part is ignored).

Now this guess 50 is made. If the user says "Higher" we need to keep the high end of our guesses at 100, but set our lower end to the last guess.

If instead the user says "Lower", then we keep the lower end the same, and set the top end of our range to the last guess.

Now with these new values of low and high, we compute our next guess exactly the same way.

This should work as long as our user tells us the truth.

An Example: the user thinks of 49 (worst case with this algorithm)

  1. guess is (1+100)/2 = 50, user says lower
  2. guess is (1+50)/2 = 25, user says higher
  3. guess is (25+50)/2 = 37, user says higher
  4. guess is (37+50)/2 = 43, user says higher
  5. guess is (43+50)/2 = 46, user says higher
  6. guess is (46+50)/2 = 48, user says higher
  7. guess is (48+50)/2 = 49, user says correct!
Since our first guess was only one away, it took us 7 guesses to get it right. In fact, the worst case will generally take log2n if we are searching among n possible choices this way. If we were guessing between 1 and 1000, then 10 guesses (or less) should do the trick (since log21000=9.97)