Java Hangman Game
Hi, I'm just about finished a hangman game in Java (eclipse). I getting a logical error that I can't find after 30 minutes of searching and testing. I would be very thankful if some could pinpoint the problem.
When the user hits the spacebar it lets them enter the entire answer (hidden word), instead of tapping in each letter. However, when the user wins or loses after answering throught the spacebar method, my program repaints really quickly and the outcome(win or lose) can't be viewed. I would like the program to act as it does when the user answers through the single key tapping method (it shows the outcome and waits for the user to hit the "new game" button.
if you need me to explain more, just ask.
thank you.
the whole program:
//Hangman
//by Chris Elwell
import java.awt.*;
import java.applet.*;
import javax.swing.*;
publicclass ElwellAssignment5extends Applet{
String hiddenWord="", guessWord, guessList;
// number of wrong letter guesses
int missCount;
// maximum number of misses allowed
int maxMisses;
boolean win, gameOver;
boolean[] knownChars;
Color bgColor =new Color(0x00dddddd);
publicvoid init()
{
setSize(1000,625);
setBackground(bgColor);
JOptionPane.showMessageDialog(this,"Use your keyboard to guess each letter. Hit the spacebar to guess the entire word.","Java Hangman - by Chris Elwell", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(this,"Click on the screen to activate. Have fun!","Java Hangman - by Chris Elwell", JOptionPane.INFORMATION_MESSAGE);
newGame();
}
publicvoid newGame()
{
hiddenWord = getHiddenWord();
guessList ="";
knownChars =newboolean[hiddenWord.length()];
// make known characters false unless it happens to be a space
for (int i=0; i<hiddenWord.length(); i++)
{
if (hiddenWord.charAt(i) ==' ')
knownChars[i] =true;
else
knownChars[i] =false;
}
win =false;
missCount = 0;
maxMisses = 6;
gameOver =false;
}
publicvoid paint(Graphics g)
{
g.setColor(new Color(0x00eeeeee));
g.fillRect(50, 60, 100, 30);
g.setColor(new Color(0x00aaaaaa));
g.drawRect(49, 59, 102, 32);
g.setColor(Color.black);
g.setFont(new Font("Helvetica", Font.BOLD, 16));
g.drawString("new word", 64, 80);
g.setFont(new Font("Helvetica", Font.BOLD, 32));
// base
g.drawLine(50,550,375,550);
// vertical pole
g.drawLine(150,550,150,150);
// cross-bar
g.drawLine(150,150,375,150);
// rope
g.drawLine(375,150,375,199);
if (!gameOver)
{
for(int i=0; i><=(hiddenWord.length()-1)*2; i++)
{
if (i%2 == 0)
{
if (hiddenWord.charAt(i/2) !=' ')
g.drawLine(i*15+225,100,(i+1)*15+225,100);
if (knownChars[i/2] ==true)
g.drawString(""+hiddenWord.charAt(i/2), i*15+224, 95);
}
}
switch (missCount)
{
case 6: g.drawLine(375,270,300,280);
case 5: g.drawLine(375,270,450,280);
case 4: g.drawLine(375,400,325,450);
case 3: g.drawLine(375,400,425,450);
case 2: g.drawLine(375,250,375,400);
case 1:{g.drawOval(349,199,51,51); g.setColor(new Color(0x00ffcc99)); g.fillOval(350,200,50,50);}
}
g.setColor(Color.black);
for(int i=0; i<guessList.length(); i++)
{
g.drawString(""+guessList.charAt(i),50+i*28,40);
}
if (win ==true)
{
// 0x00009900 is dark green
g.setColor(new Color(0x00009900));
g.drawString("You Win!",600,200);
gameOver =true;
for(int i=0; i><=(hiddenWord.length()-1)*2; i++){
if (i%2 == 0)
{
g.drawString(""+hiddenWord.charAt(i/2), i*15+224, 95);
}
}
}
if (missCount == maxMisses)
{
g.setColor(Color.red);
g.drawString("You Lose!",600,200);
gameOver =true;
for(int i=0; i<=(hiddenWord.length()-1)*2; i++){
if (i%2 == 0)
{
g.drawString(""+hiddenWord.charAt(i/2), i*15+224, 95);
}
}
}
}
}
public String getHiddenWord(){
String[] wordList ={"thanksgiving","radio","car","you are cool","God is good","what's up man","seven","chris","elwell","right"};
hiddenWord = wordList[(int)(Math.random()*10)];
hiddenWord = hiddenWord.toLowerCase();
return hiddenWord;
}
publicboolean validateGuess(String guess)
{
if (guess.equalsIgnoreCase(hiddenWord))
returntrue;
else
returnfalse;
}
publicvoid guessTheWord()
{
guessWord = JOptionPane.showInputDialog(null,"Guess the entire word:");
if (validateGuess(guessWord))
{
win =true;
}
else
{
missCount = maxMisses;
}
}
publicboolean keyDown(Event e,int k)
{
// Cast the key pressed to a character
boolean rightGuess =false;
char keyChar = (char) k;
if (keyChar !=' ')
{
for(int i=0; i<guessList.length(); i++)
{
if (keyChar == guessList.charAt(i))
returntrue;
}
guessList = guessList+keyChar;
for(int i=0; i><hiddenWord.length(); i++)
{
if (keyChar == hiddenWord.charAt(i))
{
rightGuess =true;
knownChars[i] =true;
}
}
// this loop makes "win = false" if there are any unknown characters
// otherwise "win = true"
win =true;
for(int i=0; i><hiddenWord.length(); i++)
{
if (knownChars[i] ==false)
{
win =false;
}
}
if (rightGuess ==false)
missCount++;
}
else
{
guessTheWord();
}
repaint();
returntrue;
}
publicboolean mouseDown(Event evt,int x,int y)
{
if ((x>50 && x<150) && (y>60 && y<90))
{
newGame();
repaint();
}
returntrue;
}
}

