hi, i am creaeting a word guessing game. a mystery word is "hidden" by underscores. the number of uunderscores is generated by counting the number of characters in the mymstery word. the uundnerscores then are displayed on the screen that is: Computer is shown as: _ _ _ _ _ _ _ _ the user then inputs a character, and the program searches through the mystery word to find the indexes of the occurance of that character. my question is: how do i replace multiple occurance of that letter in a counter generated series of uunderscores??? the underscores are not strrings, i dont know how to handle them. please help. thanks
public static void main(String[] args){ MysteryWordGuess wordGuess = new MysteryWordGuess(); wordGuess.init(); wordGuess.playGame(); }
public void init(){ for (int i = 0; i < mysteryWord.length(); i++){ displayWord = displayWord + "-"; } }
public void playGame(){ boolean done = false; while(!done){ char c = getCharacter(); guess(c); done = (mysteryWord.compareTo(displayWord) == 0); } }
public char getCharacter(){ char c = ' '; try{ System.out.println("Guess the word"); System.out.println(displayWord); InputStream is = System.in; c = (char)is.read(); }catch(IOException ioe){ System.err.println("IOException: " + ioe.getMessage()); } return c;
}
public void guess(char character){ for (int i = 0; i < mysteryWord.length(); i++){ char nextChar = mysteryWord.charAt(i); if (character == nextChar) displayWord = displayWord.substring(0, i) + character + displayWord.substring(i+1);