Java - number guessing game
Just wondering if someone could help me out here. I'm brand new to java and applet programming in particular so my code might be very questionable.
it's a simple guess the number program.
The user has 8 chances.
My problem is when the user enters their 8th guess the program seems to go through the paint method twice because it never says "guess = 8/8"... instead it seems to go through the method a second time and displays 0/8 guesses. can anyone help me out...
Also can anyone comment on anything that i should or shouldn't do with my current code. I'm just looking for pointers really... thanks
Code:
import java.applet.Applet;
import java.awt.*; // import the java.awt package
import java.awt.event.*; // import the java.awt.event package
public class myApplet extends Applet implements ActionListener
{
Label prompt; // message that prompts user to input
TextField input; // input values are entered here
double rnd;
int number; // variable that stores random value
int guessLeft;
int random;
int wins = 0, losses = 0;
private Image picture;
//-----------------------------------------------------------------
public void init()
{
setSize(500, 500);
rnd = (Math.random()*100) + 1;
guessLeft = 0;
random = 12;//(int) rnd;
number = -1;
prompt = new Label( "Enter a number between 1 and 100:" );
input = new TextField(5);
add(prompt); // put prompt on applet
add(input); // put input TextField on applet
input.addActionListener( this ); // "this" applet handles action events for TextField input
}
//-----------------------------------------------------------------
public void actionPerformed( ActionEvent e ) // process user's action in TextField input
{
number = Integer.parseInt( e.getActionCommand() ); // get the number and convert it to an integer
guessLeft++;
input.setText( "" ); // clear data entry field
if(number == random)
{
wins++;
}
if (guessLeft == 8)
{
losses++;
}
picture = getImage(getCodeBase(), "pic"+guessLeft+".jpg");
repaint(); // Force paint to be invoked
}
//-----------------------------------------------------------------
public void paint (Graphics g)
{
g.drawImage(picture, 80, 80, this);
if (number == random) //WIN
{
picture = getImage(getCodeBase(), "win.jpg");
g.drawString("You WON ",50,40);
g.drawString("Wins = " + wins + " / Losses = " + losses + "Guesses = " + guessLeft + "/8", 50, 60);
g.drawImage(picture, 80, 80, this);
guessLeft = 0;
return;
}
else if (number == -1)
{
return; // makes sure nothing is displayed the first time the game is played
}
else if (guessLeft == 8)
{
picture = getImage(getCodeBase(), "lose.jpg");
g.drawString("Sorry but you LOST",50,40);
g.drawString("Wins = " + wins + " / Losses = " + losses + "Guesses = " + guessLeft + "/8", 50, 60);
g.drawImage(picture, 80, 80, this);
guessLeft = 0;
}
else if (number > random)
{
g.drawString("Your guess was too HIGH",50,40);
g.drawString("Wins = " + wins + " / Losses = " + losses + "Guesses = " + guessLeft + "/8", 50, 60);
g.drawImage(picture, 80, 80, this);
}
else
{
g.drawString("Your guess was too LOW",50,40);
g.drawString("Wins = " + wins + " / Losses = " + losses + "Guesses = " + guessLeft + "/8", 50, 60);
}
}
}
//-----------------------------------------------------------------