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(); lastTime=d.getTime(); textArea = new TextArea(); textArea.setEditable(false); textArea.setText("Pick a Number from 1 to 10. Is it 7?\n"); higher = new Button("Higher"); higher.addActionListener(this); lower = new Button("Lower"); lower.addActionListener(this); correct = new Button("Correct!"); correct.addActionListener(this); reset = new Button("Start Over"); reset.addActionListener(this); Panel buttonPanel= new Panel(); buttonPanel.setLayout(new GridLayout(0,1)); //single column buttonPanel.add(higher); buttonPanel.add(lower); buttonPanel.add(correct); buttonPanel.add(reset); setLayout(new BorderLayout() ); add (buttonPanel, BorderLayout.SOUTH); add (textArea, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { textArea.append(e.getActionCommand()+"\n"); long thisTime=e.getWhen(); long timeLapsed=thisTime-lastTime; lastTime=thisTime; textArea.append(timeLapsed+" milliseconds lapsed\n"); } }
Lets set low=1 and high=100. then the first guess
guess=(low+high)/2;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.
guess=(low+high)/2;This should work as long as our user tells us the truth.