Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Thanked 0 Times in 0 Posts

    Help with loop, hangman game.

    Hello, im a beginner programmer and I have been assigned to code a hangman game. Its taken me quite a bit of time to get to where im at now, and although my code is not really well organized, at this point im just trying to make it work. The class im in is about data structures so Im sure ill get a better understanding at the end of the semester.
    My problem is this:
    I made a loop to compare the guessed character against the "mystery word" (which is given by one of the users(2 player game), and it is working great so far except for how to make characters appear more that once in the word? ex. good --> *oo*
    I've done a search looks like some people asked the same question, yet I dont see how the answer works in here:

    for (int i = 0; i < word.length(); i++) {
    if (word.charAt(i) == c) {
    hiddenWord = hiddenWord.substring(0, i) + c + hiddenWord.substring(i + 1);
    theOutput = hiddenWord;
    answer = "Good guess!";

    heres the code of my loop:

    public void GetInput(int lengthOfWord){
    while (numberOfTries>=0){
    int i=0;
    for (int x =0; x<=lengthOfWord;x++ ){
    while(i < lengthOfWord){
    hiddenWord+='*';//Creates Asterisks"****"
    System.out.println(" " +
    "Guesses: "+guessedCharacters);
    System.out.println("\n"+"Enter a character in UPPERCASE for your guess");
    String charGuess = keyboard.nextLine().toUpperCase();
    char character = charGuess.charAt(0);
    //Takes only the first character if more than one
    m_character = character;
    if (m_mysteryWord.charAt(x)==m_character){
    System.out.println("in word");//Temporary test to see if comparison works
    indexOfCharacter = x;//find out where the match is
    guessedCharacters = guessedCharacters+ character;
    x= lengthOfWord;//stops for loop
    hiddenWord= hiddenWord.substring(0,indexOfCharacter)+character+
    //"unveils" asterisks with matched character

    System.out.println("Character not in word");
    //Temporary test to see if comparison works
    guessedCharacters = guessedCharacters+ character;
    x= lengthOfWord;

    System.out.println("Number of Tries remaining: "+ numberOfTries);


    Any help from some of you experienced programmers will be greatly APPRECIATED.Thanks!
    Last edited by DaVisionary; 06-20-2010 at 11:37 AM. Reason: updated code


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts