Need help with hangman game
Hi, this is my first time posting in this forum. I currently need some help with my assignment. I'm required to design a hangman game that has an interface menu, where I can choose a new game. Upon choosing to play, the game will load the lines from a text file and split them up into words and hints and randomized. A hint of the word will be displayed and spaces will be displayed to show the number of letters:
HINT: fruit
_ _ _ _ _
I'll be required to make a guess by entering a letter. If the wrong letter is chosen, it will display and a hangman figure:
Wrong Letters: k
If more letters entered by user are wrong, it will continue to accumulate:
Wrong Letters: ka
If the correct letter is entered, it will display the letter on one of the spaces in the word:
_ _ p _ _
But right now my problem is finding out how to convert string to character from input, and to compare the character with the characters in the word. I have currently written my code to the part of entering a letter to make a guess. If anyone knows how I should go about next, I would really aprreciate it, thx!
This is the game code:
Code:
import java.util.*;
import java.io.*;
public class Hangman {
private final int MAX_WORDS = 15;
private String[] wordAns;
private String[] wordHint;
private static String word;
private static String guess;
public void newGame() {
Scanner myScanner = null;
String aLine = null;
int i = 0;
wordAns = new String[MAX_WORDS];
wordHint = new String[MAX_WORDS];
try {
myScanner = new Scanner (new File("words.txt"));
// Loop until all words have been read from the file
while (myScanner.hasNextLine()) {
// read a line from file
aLine = myScanner.nextLine();
// Split the data into two parts as words and hints
String[] data = aLine.split(";");
wordAns[i] = data[0];
wordHint[i] = data[1];
i++;
}
// Randomize and display a hint from the data
Random generator = new Random();
int num = generator.nextInt(i);
System.out.print ("HINT: " + wordHint[num] + "\n\n");
word = wordAns[num];
char[] temp = word.toCharArray();
for (int j = 0; j < word.length(); j++ ) {
System.out.print ("_\t");
}
System.out.println ("\n\n");
System.out.println ("Make your guess [A-Z]: ");
guess = myScanner.nextLine();
char guessLetter = guess.charAt(0);
[B]// Code yet to be completed...[/B]
}
catch (FileNotFoundException e) {
System.out.println ("Game data file is not found!");
}
catch (IOException e) {
System.out.println ("Error reading game data!");
}
finally {
if (myScanner != null) myScanner.close();
}
}
}
This is the code for the main menu:
Code:
import java.util.*;
public class HangmanGame {
// define static variables
private static Hangman myGame;
public static void main(String[] args) {
boolean quitFlag = false;
int option;
Scanner console = new Scanner (System.in);
do {
// display user interface menu
System.out.println ("-------------------------");
System.out.println (" WELCOME TO PLAY HANGMAN");
System.out.println ("-------------------------");
System.out.println ("1.\t" + "Play NEW Game");
System.out.println ("2.\t" + "Quit");
System.out.println ("-------------------------");
System.out.print ("Choose an option (1 or 2): ");
// read user's entry from option
option = console.nextInt();
// Start a new game
if (option == 1) {
myGame = new Hangman();
myGame.newGame();
}
// Quits menu and exits program
else if (option == 2) {
quitFlag = true;
}
} while (!quitFlag);
System.out.println ("Game Over! Thanks for playing!");
}
}
This is the words.txt file:
Code:
apple;fruit
orange;fruit
mango;fruit
motorcycle;vehicle
lorry;vehicle
sofa;furniture
table;furniture
chair;furniture
spoon;eating utensil
fork;eating utensil
blue;colour
brown;colour
yellow;colour
tiger;animal
elephant;animal