THIS IS THE QUESTION:
The player must then guess a letter (ch, say). If ch is not in the word, the player is
one step closer to being hanged. If ch is in the word, the string of *s is displayed
with the letter ch replacing a * wherever it occurs in the word. For instance, if the
player guesses e, the string is displayed as follows:
* e * * e * *
This procedure is repeated until the player guesses the word or he is hanged,
whichever comes first. The player gets hanged if he makes a predetermined
number (7, say) of unsuccessful guesses. Each time the player makes a guess, the
computer will inform him of the word obtained so far and the number of wrong
guesses he can still make before being hanged. A sample run might look like this
(the underlined letters are typed by the player):
Try to guess my secret word
Countdown to hang: 7
* * * * * * *
Guess a letter: n
Sorry, try again
* * * * * * *
Countdown to hang: 6
Guess a letter: s
s * * * * * s
Countdown to hang: 6
Guess a letter: r
s * * r * * s
Countdown to hang: 6
Guess a letter: a
Sorry, try again
s * * r * * s
Countdown to hang: 5
Guess a letter: e
s e * r e * s
Countdown to hang: 5
Guess a letter: d
Sorry, try again
s e * r e * s
Countdown to hang: 4
Guess a letter: t
s e * r e t s
Countdown to hang: 4
Guess a letter: c
s e c r e t s
Countdown to hang: 4
You've got it!!
Write the program so that the computer chooses a word at random from a list of words stored in a file.
THIS IS MY SOLUTION:
import java.io.*; import java.util.Scanner; public class Test { public static void main (String[] args) throws IOException { Scanner in = new Scanner(new FileReader("list.txt")); //Declares variables String word= " "; int misses = 0; int exposed = 0 ; String word= " "; int countdown = 7; char guess; boolean correctGuess= false; //char [] display = new char [10]; for (int i = 0; i < display.length; i++) { display[i] = "*"; } System.out.printf("Try to guess my secret word"); //System.out.printf("Countdown to hang: 7"); while (exposed < word.length()) { System.out.printf ("Countdown to hang: ", countdown - misses); System.out.printf ("Enter a guess "); guess = in.read(); //Checks to see if letter guessed is correct //Correct guess is true for (int i= 0; i < word.length(); i ++) { if (guess == word[i]) { display [i] = word [i]; exposed ++; correctGuess = true; } } //Correct guess is false if (!correctGuess) { misses++; countdown--; System.out.printf ("Sorry try again\n"); } }//end while System.out.printf ("You've got it!"); in.close(); } }
MY PROBLEMS:
I have problems declaring arrays>> how do i declare display and how to i declare word?
I have problems reading a single character from a file>>>>how do i read the guess entered correctly?
Please help me.