CharlieM
May 17th, 2007, 12:43 PM
Fucking timer class.
I have this assignment.
Prerequisites are a program of at least 5 classes, with 2 of them using inherritence.
I have decided to go with a hangman game, cause im just so full of imagination and creativity.
Anywho....my problem is this.
I have a timer that counts down from 90 secconds, and interrupts the event listener ever seccond.
On that seccond, it decrements a counter that controls when the timer stops.
There is also a conditional that the timer is to stop when the player accumulates 10 incorrect guesses (the final drawing on the hangman pic).
The game also ends the timer when it gets to zero.
The problem, it does not stop the timer when 10 guesses have been incorrectly accrued.
I have a class handling the drawing.
The main is controlling the timer, and has an object declared of the drawing class.
A counter is controlling what gets drawn, and has set and get methods to set the counter upon an incorrect guess.
I also have in there a system.out to show me that I am actually incrementing an incorrect guess counter....which is fucking pissing me off, cause the conditional within my "nest of doom" simply does not work as it should.
Heres the action listener portion, keep in mind its nested like a mother fucker to prevent other button pushes from intefering with the timer. I could of done an inner class, but fuck im trying to push myself here...inner classes are cheats way out.
public void actionPerformed(ActionEvent e)
{
//The following is a nest of DOOM!!!
if(e.getSource()==newMenu) //If new game is pressed within the menu heirachy
{
btnGuess.setEnabled(true); //Enable the functionality of the Guess button
dictionaryWord = newWord(); //getting a new word
lengthOfDictionary = dictionaryWord.length(); //Get the length (in integer) of the word fetched from the dictionary.
counter = 90; //resetting the timer to 90
startTime = 90; //resetting the counter to 90 (iterates to Label)
stringCounter=Integer.toString(startTime); //Parseing the counter to a String, to iterate it to the Label in GUI
lblTimer.setText(stringCounter); //Iterating the label to current timer
theTimer.start(); //Start the timer
System.out.println("The dictionary word is: " +dictionaryWord); //debugging the word
LeonardoDaVinci.resetPainter(); //Resetting the incorrect guesses to zero in DrawImage class
LeonardoDaVinci.repaint(); //Calling the DrawImage object to paint nothing on the canvas
dictionaryCompare = 0; //Setting a comparison integer for searching the dictionary String object to zero.
}
else //If NOT new menu - but something else is triggering the event listener :O
{
if(e.getSource()==btnGuess) //If the guess button is pressed
{
theTimer.stop(); //Stop the timer
guess = JOptionPane.showInputDialog("Enter a single letter"); //Query the user to input a character
guess.toLowerCase(); //parse the input to lower case
while(guess.equals("")) //Checking to ensure nothing is entered as a guess
{
JOptionPane.showMessageDialog(null,"Not a character!!!"); //Notifying user of error
guess = JOptionPane.showInputDialog("Enter a single letter"); //Query the user to input a character
guess.toLowerCase(); //Parse the chatacter to a lower case
}
if(dictionaryWord.contains(guess)) //Checking to see if the dictionary word contains the letter guessed
{
while(lengthOfDictionary>dictionaryCompare) //Looping the length of the word times, to search each character.
{ //Needs tweaking to capture ALL positions of the guess
System.out.println(dictionaryWord.indexOf(guess)); //Debug output to CMD window the index of the word which contains the guess
dictionaryCompare++; //Increase the comparrison search integer
}
dictionaryCompare = 0; //Resetting the comparrison search integer
}
else //Wrong Guess!!
{
LeonardoDaVinci.wrongGuess(); //Indicating a wrong guess, increasing the wrong guess counter by 1.
if(LeonardoDaVinci.getWrongGuesses() >= 10) //Checking to see that the wrong guess's havnt exceeded the end-game conditional
{
JOptionPane.showMessageDialog(null,"Game Over !"); //Inform user that the game is over, and they suck at life in general
btnGuess.setEnabled(false); //Remove functionality of the guess button
theTimer.stop(); //Timer does not stop - needs fixing //Stop the timer
//break;
}
//set visible letters guessed right
//set guess'd letter to the guessed letter collumn
}
theTimer.start(); //Start the timer
LeonardoDaVinci.repaint(); //Repaint the canvas
}
else //If NOT the guess button - but something else is triggering the event listener :O
{
if(e.getSource()==scoreMenu) //If the High Score is pressed within the menu heirachy
{
getHighScore(); //Display the Highest Score, or a dialog box if there is none recorded so far :(
}
else //If not High Score - but something else is triggering the event listener :O
{
if(e.getSource()==exitMenu) //If Exit Game is pressed within the menu heirachy
{
System.exit(0); //Close the application
}
else //If not Exit Game - but something else is triggering the event listener :O
{
if(theTimer!=null) //If the timer is triggering the event handler and is above zero
{
stringCounter = Integer.toString(counter); //Parseing the counter to a String, to iterate it to the Label in GUI
lblTimer.setText(stringCounter); //Iterating the label to current timer
counter = --startTime; //Decrementing the counter
if(counter<0) //If the timer reaches zero
{
btnGuess.setEnabled(false); //Remove functionality of the guess button
JOptionPane.showMessageDialog(null,"Game Over !"); //Inform the user of how horribly they fail at life
theTimer.stop(); //Stop the game
}
}
}
}
}
}
}
Someone find out why my timer is not stopping when I reach 10 guesses, ill be greatful.
The drawing class extends JPanel because it is emedded in the GUI as a panel...essentially a canvas panel.
Edit: goddamnit wig, these code tags fucked up my indentation.
Fucking sad face. :(
I have this assignment.
Prerequisites are a program of at least 5 classes, with 2 of them using inherritence.
I have decided to go with a hangman game, cause im just so full of imagination and creativity.
Anywho....my problem is this.
I have a timer that counts down from 90 secconds, and interrupts the event listener ever seccond.
On that seccond, it decrements a counter that controls when the timer stops.
There is also a conditional that the timer is to stop when the player accumulates 10 incorrect guesses (the final drawing on the hangman pic).
The game also ends the timer when it gets to zero.
The problem, it does not stop the timer when 10 guesses have been incorrectly accrued.
I have a class handling the drawing.
The main is controlling the timer, and has an object declared of the drawing class.
A counter is controlling what gets drawn, and has set and get methods to set the counter upon an incorrect guess.
I also have in there a system.out to show me that I am actually incrementing an incorrect guess counter....which is fucking pissing me off, cause the conditional within my "nest of doom" simply does not work as it should.
Heres the action listener portion, keep in mind its nested like a mother fucker to prevent other button pushes from intefering with the timer. I could of done an inner class, but fuck im trying to push myself here...inner classes are cheats way out.
public void actionPerformed(ActionEvent e)
{
//The following is a nest of DOOM!!!
if(e.getSource()==newMenu) //If new game is pressed within the menu heirachy
{
btnGuess.setEnabled(true); //Enable the functionality of the Guess button
dictionaryWord = newWord(); //getting a new word
lengthOfDictionary = dictionaryWord.length(); //Get the length (in integer) of the word fetched from the dictionary.
counter = 90; //resetting the timer to 90
startTime = 90; //resetting the counter to 90 (iterates to Label)
stringCounter=Integer.toString(startTime); //Parseing the counter to a String, to iterate it to the Label in GUI
lblTimer.setText(stringCounter); //Iterating the label to current timer
theTimer.start(); //Start the timer
System.out.println("The dictionary word is: " +dictionaryWord); //debugging the word
LeonardoDaVinci.resetPainter(); //Resetting the incorrect guesses to zero in DrawImage class
LeonardoDaVinci.repaint(); //Calling the DrawImage object to paint nothing on the canvas
dictionaryCompare = 0; //Setting a comparison integer for searching the dictionary String object to zero.
}
else //If NOT new menu - but something else is triggering the event listener :O
{
if(e.getSource()==btnGuess) //If the guess button is pressed
{
theTimer.stop(); //Stop the timer
guess = JOptionPane.showInputDialog("Enter a single letter"); //Query the user to input a character
guess.toLowerCase(); //parse the input to lower case
while(guess.equals("")) //Checking to ensure nothing is entered as a guess
{
JOptionPane.showMessageDialog(null,"Not a character!!!"); //Notifying user of error
guess = JOptionPane.showInputDialog("Enter a single letter"); //Query the user to input a character
guess.toLowerCase(); //Parse the chatacter to a lower case
}
if(dictionaryWord.contains(guess)) //Checking to see if the dictionary word contains the letter guessed
{
while(lengthOfDictionary>dictionaryCompare) //Looping the length of the word times, to search each character.
{ //Needs tweaking to capture ALL positions of the guess
System.out.println(dictionaryWord.indexOf(guess)); //Debug output to CMD window the index of the word which contains the guess
dictionaryCompare++; //Increase the comparrison search integer
}
dictionaryCompare = 0; //Resetting the comparrison search integer
}
else //Wrong Guess!!
{
LeonardoDaVinci.wrongGuess(); //Indicating a wrong guess, increasing the wrong guess counter by 1.
if(LeonardoDaVinci.getWrongGuesses() >= 10) //Checking to see that the wrong guess's havnt exceeded the end-game conditional
{
JOptionPane.showMessageDialog(null,"Game Over !"); //Inform user that the game is over, and they suck at life in general
btnGuess.setEnabled(false); //Remove functionality of the guess button
theTimer.stop(); //Timer does not stop - needs fixing //Stop the timer
//break;
}
//set visible letters guessed right
//set guess'd letter to the guessed letter collumn
}
theTimer.start(); //Start the timer
LeonardoDaVinci.repaint(); //Repaint the canvas
}
else //If NOT the guess button - but something else is triggering the event listener :O
{
if(e.getSource()==scoreMenu) //If the High Score is pressed within the menu heirachy
{
getHighScore(); //Display the Highest Score, or a dialog box if there is none recorded so far :(
}
else //If not High Score - but something else is triggering the event listener :O
{
if(e.getSource()==exitMenu) //If Exit Game is pressed within the menu heirachy
{
System.exit(0); //Close the application
}
else //If not Exit Game - but something else is triggering the event listener :O
{
if(theTimer!=null) //If the timer is triggering the event handler and is above zero
{
stringCounter = Integer.toString(counter); //Parseing the counter to a String, to iterate it to the Label in GUI
lblTimer.setText(stringCounter); //Iterating the label to current timer
counter = --startTime; //Decrementing the counter
if(counter<0) //If the timer reaches zero
{
btnGuess.setEnabled(false); //Remove functionality of the guess button
JOptionPane.showMessageDialog(null,"Game Over !"); //Inform the user of how horribly they fail at life
theTimer.stop(); //Stop the game
}
}
}
}
}
}
}
Someone find out why my timer is not stopping when I reach 10 guesses, ill be greatful.
The drawing class extends JPanel because it is emedded in the GUI as a panel...essentially a canvas panel.
Edit: goddamnit wig, these code tags fucked up my indentation.
Fucking sad face. :(