Applet not running: Stanford Engineering Everywhere Hangman
I've written working on a java applet, which seems to be opening sun.applet.AppletViewer when it is running. I'm using the acm libraries for the Stanford Engineering Everywhere Hangman assignment. The problem I'm having doesn't seem to be with the code, unless I've not done some implementation I should've. Basicly Applet Viewer is hanging just saying "starting applet..." and the program isn't running. I've not encountered this problem before, so I figured I'ld ask here in case anyone has encountered a problem like this before. I guess it might be the default exception case that is getting thrown, but I'm not getting an error message so I doubt it.
I think the code should be working, but I'll post it in case there is something obvious I've left out at this point. I'm not quite finished with the assignment, but I'm fairly confident it would work if I wasn't having this problem.
Hangman.java
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.awt.*;
import java.io.*;
public class Hangman extends ConsoleProgram {
//private static final int NUM_GUESSES=8;
/*new instance of HangmanLexicon.java */
private HangmanLexicon hangLex;
private HangmanCanvas canvas;
/*new random generator to get indexes of words in hangLex*/
//private RandomGenerator rgen = RandomGenerator.getInstance();
/*Stores word that use is trying to guess*/
private String hidden="";
/*Displayed version of hidden*/
private String displayHidden = "";
/*prompt for player to enter their guess, which gets stored
* as a string*/
private String guess = readLine("Your guess: ");
/*counts down until user has no more guesses*/
private int guessCounter = 8;
private String repeat = readLine("Play again? (y or n): ");
public void init() {
canvas = new HangmanCanvas();
add(canvas);
}
/*Runs through program, acts as main*/
public void run() {
/* You fill this in */
println("Welcome to the Hangman program!");
while(true) {
setUpWord();
displayWord();
guessesLeft();
guess = readLine("Your guess: ");
println(guess);
checkGuess();
if(displayHidden.equals(hidden)) {
println("You guessed the word: " + hidden);
println("You win.");
println(repeat);
if(!repeat.equals("n")) {
guessCounter=8;
canvas.reset();
}else {
break;
}
} else if (guessCounter==0) {
println("The word was: " + hidden);
println("You lose.");
println(repeat);
if(!repeat.equals("n")) {
guessCounter=8;
canvas.reset();
}else {
break;
}
}
}
}
/*sets hidden to a random word in hangLex*/
private void setUpWord() {
hangLex = new HangmanLexicon();
hidden=hidden.concat(hangLex.getWord(8));
}
/*Prints the updated version of the known word after a guess is entered*/
private void displayWord() {
if (displayHidden.equals("")) {
for (int i= 0; i<hidden.length(); i++) {
displayHidden = displayHidden.concat("-");
}
println("The word now looks like this: " + displayHidden);
} else if(guessCounter==0) {
} else {
println("The word now looks like this: " + displayHidden);
}
}
/*prints updates on the number of guesses the user has left to console*/
private void guessesLeft() {
if (guessCounter > 1) {
println("You have " + guessCounter + " guesses left.");
} else if (guessCounter == 1) {
println("You have only one guess left.");
} else if (guessCounter == 0) {
println("You're completely hung.");
}
}
private void checkGuess() {
int letterCounter =0;
while (guess.length()>1) {
println("Guesses must be single characters.");
println(guess);
}
char checkLetter = guess.charAt(0);开发者_StackOverflow社区
guessCounter--;
//
if(Character.isLowerCase(checkLetter)) {
Character.toUpperCase(checkLetter);
}
//
for(int i=0; i<hidden.length(); i++) {
if(hidden.charAt(i) == checkLetter) {
displayHidden = displayHidden.substring(0, i) + guess +
displayHidden.substring(i+1);
} else {
letterCounter++;
}
}
//
if (letterCounter==hidden.length()) {
println("There are no " + checkLetter + "'s in the word.");
} else if (letterCounter!=hidden.length()) {
println("That guess is correct.");
}
}
}
HangmanLexicon.java
import acm.util.*;
public class HangmanLexicon {
/** Returns the number of words in the lexicon. */
public int getWordCount() {
return 10;
}
/** Returns the word at the specified index. */
public String getWord(int index) {
switch (index) {
case 0: return "BUOY";
case 1: return "COMPUTER";
case 2: return "CONNOISSEUR";
case 3: return "DEHYDRATE";
case 4: return "FUZZY";
case 5: return "HUBBUB";
case 6: return "KEYHOLE";
case 7: return "QUAGMIRE";
case 8: return "SLITHER";
case 9: return "ZIRCON";
default: throw new ErrorException("getWord: Illegal index");
}
};
}
I've not yet finished with the other file, but I don't thing that should be an issue of concern until I've got the applet thing sorted out.
Nevermind. I figured out the problem. I should've been doing this in the run method.
guess = readLine("Your guess: ");
That was a really stupid mistake on my part. lol. I thought I'ld already tried that.
精彩评论