The Artima Developer Community
Sponsored Link

Java Answers Forum
stuck on a Word guessing game

1 reply on 1 page. Most recent reply: Nov 4, 2002 4:12 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
edwin mo

Posts: 7
Nickname: ct976
Registered: Oct, 2002

stuck on a Word guessing game Posted: Nov 3, 2002 11:25 AM
Reply to this message Reply
Advertisement
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


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: stuck on a Word guessing game Posted: Nov 4, 2002 4:12 PM
Reply to this message Reply

import java.io.*;


public class MysteryWordGuess{

private String mysteryWord = "monument";
private String displayWord = "";

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);

}
}
}

Flat View: This topic has 1 reply on 1 page
Topic: hashtables Previous Topic   Next Topic Topic: Connection to stored procedures from jsp

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use