Results 1 to 5 of 5
Thread: Need help with hangman game
- 04-25-2009, 01:55 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
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:
This is the code for the main menu:Java 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 words.txt file:Java 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!"); } }
Java 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
Last edited by kurt; 04-25-2009 at 01:58 PM.
- 04-25-2009, 02:28 PM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Try converting the input String to a char array us the toCharArray() method, and then taking the first Character from the array. Something like this...
As for checking if it contains the char, you could use a for loop from 0 up to AnswerString.length() - 1, and an if statement testing whether AnswerString.charAt(i) = guessed.Java Code:char guessed = guessString.toCharArray[0];
e.g
Java Code:public boolean containsChar(char guessed){ for(i=0; i<AnswerString.length; i++){ if(AnswerString.charAt(i) == guessed){ return true; } } return false; }If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 04-25-2009, 03:25 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
Hi, thx for the reply. I have successfully converted string to char from the input of letter, but I'm still having problems with the comparing of character from the input and the characters from the hidden word. I got an error "Cannot find symbol method charAt(int) at the for loop.
Java Code:public class Hangman { private final int MAX_WORDS = 15; private String[] wordAns; private String[] wordHint; private static String word; private static String guess; private static char guessed; 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"); // gets input from user, converts string to char System.out.println ("Make your guess [A-Z]: "); Scanner console = new Scanner (System.in); guess = console.nextLine(); char[] guessed = guess.toCharArray(); for(int m =0; m < temp.length; m++){ if(temp.charAt(m) == guessed){ System.out.println ("True!"); } } System.out.println ("False!"); // char guessLetter = guess.charAt(0); }
- 04-25-2009, 03:44 PM #4
Because temp is a char array, not a string. charAt() is a String method.Cannot find symbol method charAt(int) at the for loop.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-25-2009, 06:47 PM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
yes... should be if(temp[i] == guessed)
also, your loop will repeatedly print true... you may want to break the loop after determining that the specified character is contained within the correct answer.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
help with part of my hangman applet
By vouslavous in forum Java AppletsReplies: 11Last Post: 04-07-2009, 03:12 AM -
Need help with Hangman!!!
By chinasome in forum New To JavaReplies: 10Last Post: 11-09-2008, 04:42 AM -
Hangman Help!!!
By chinasome in forum New To JavaReplies: 5Last Post: 11-08-2008, 02:30 AM -
Hangman Game
By L23 in forum New To JavaReplies: 8Last Post: 07-03-2008, 01:56 PM -
Create the game Hangman
By barney in forum New To JavaReplies: 1Last Post: 08-06-2007, 06:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks