Hello, all. I'm creating a program that lets a user play Hangman. I've attached the directions for the program for clarification. My code is pasted below. I've set up a couple methods for it, but I'm having difficulty in understanding how to call these methods in the main class (assuming my methods are set up correctly to begin with).
package hangman;
import java.util.Scanner;
/**
*
* @author Andrew
*/
public class Hangman
{
int MistakesAllowed = 6;
String SecretWord;
int NumberOfMistakes;
int Chances = MistakesAllowed - NumberOfMistakes;
String rightLetters = "";
public void newGame()
{
rightLetters = "";
NumberOfMistakes = 0;
System.out.println();
System.out.println("* * * * * * * * * * NEW GAME * * * * * * * * * *");
System.out.println();
System.out.println("Player 1, please enter a secret word: ");
Scanner input = new Scanner(System.in);
SecretWord = input.nextLine();
System.out.print("The word is: ");
for (int i = 1; i <= SecretWord.length(); i++)
System.out.print("_");
System.out.println();
System.out.println();
System.out.println("Player 2, you have 6 chances to find the word.");
System.out.println("Use method \"guess(char c)\" to guess a character");
System.out.println("or use method \"giveUp()\" to quit");
}
public void giveUp()
{
System.out.println();
System.out.println("* * * * * * * * * * * * PLAYER 2 GIVES UP * * * * * * * * * * *");
System.out.println();
System.out.println("The hidden word was " + SecretWord);
System.out.println();
System.out.println("Use method \"newGame()\"to start a new game.");
}
public void guess(char c)
{
// if the guess isn't a letter, tell the user and return
if (!Character.isLetter(c))
{
System.out.println("Only enter letters!");
return;
}
// If the guess is wrong:
if (SecretWord.indexOf(c) == -1)
{
NumberOfMistakes++;
System.out.println("Wrong! Try again!");
System.out.println("Your remaining chances are " + Chances);
if (NumberOfMistakes == MistakesAllowed)
{
System.out.println();
System.out.println("* * * * * * * * * * * * PLAYER 2 LOSES * * * * * * * * * * *");
System.out.println();
System.out.println("The hidden word was " + SecretWord);
System.out.println();
System.out.println("Use method \"newGame()\"to start a new game.");
}
}
else // The guess is correct
{
System.out.println("Correct!");
rightLetters = rightLetters + c;
for (int i = 0; i < SecretWord.length(); i++)
{
char ch = SecretWord.charAt(i);
if (rightLetters.contains(String.valueOf(ch)))
{
System.out.print(ch);
}
else
{
System.out.print("-");
}
}
System.out.println();
}
}
public static void main(String[] args)
{
newGame game = new newGame();
}
}
I dont have any errors displayed in the code I've writen, but when I run it I get:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class newGame
location: class hangman.Hangman
at hangman.Hangman.main(Hangman.java:98)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Any direction would be greatly appreciated. Thanks!