I need help with my java code for the game hangman... we are using eclipse to write the game and using three main files to start...hangman.java hangmanlexicon.java and hangmancanvas.java.

the first part of the assignment, is to create a ConsoleProgram for the game and this is what i have so far.

/*
 * File: Hangman.java
 * ------------------
 * This program will eventually play the Hangman game from
 * Assignment #4.
 */

import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.lang.*;
import java.awt.*;

public class Hangman extends ConsoleProgram {
    /*
     * Constants
     */
    public static final int N_TRYS = 13;
    public static final String guessText = "Your Guess: ";

    /*
     * Create a random generator
     * this will grab a random word from the lexicon 
     * or from a text file.
     */

    private RandomGenerator rgen = RandomGenerator.getInstance();

    /*
    * create instance variables
    * this is the word list
    */

    public HangmanLexicon word = new HangmanLexicon();

    /*
     * String Variables
     */

    public String welcome = "Welcome to Hangman!";
    public String nGame = "The New Word Looks Like This: ";
    public String newWord = "";
    public String guess = "";
    public String b = "";
    public String c = ""; //variable to set starting dashes

    /*
     * Create Integer Variables
     */
    public int nGuesses = N_TRYS;
    public int count = word.getWordCount() - 1;
    public int nw;


    public void run() {
        /* You fill this in */
        println(welcome);

        nw = rgen.nextInt(0, count); // generate a random number
        b = word.getWord(nw); // grab a word from the lexicon using the random number we generated
        b = b.toLowerCase(); // convert that word to lowercase

        for (int dash = 0; dash < b.length(); dash++) {
            c = c + "*";
        }
        print(nGame + c + '\n');

        /*
         * starts the loop for the game
         */
        while (nGuesses <= 13 && nGuesses != 0) {
            println("You have " + nGuesses + " Guesses left");
            guess = readLine("Your Guess: ");

            if (guess.length() > 1) {
            println("Invalid Guess. Please guess 1 letter at a time");
            print(nGame + c + '\n');
            } else {
                hangIt(guess);
            }
        }

    }

    private void hangIt(String letter) {
        int index = b.indexOf(letter);
        char d = b.charAt(index);
        if (index == -1) {
            println("Invalid Guess. Try again");
            print(nGame + c + '\n');
            nGuesses--;
        } else {
            c = c.replace(index, letter);
            print(nGame + c + '\n');
        }
    }

}

where im running into trouble is figureikng out how to replace the proper "*" with the guessed letter
any help would be appreciated

With the original string for example:

"Daniweb";

Say if the user guesses a 'w';

Loop through each letter in the string and if letter is found print letter otherwise print a star.

Simple enough logic yes?

how to replace the proper "*" with the guessed letter

Where are the "*"s for the letters in the word stored?
If they are in a String like this: "****", you could use methods in the String class to get the substring before the letter to replace and the substring after the letter to replace and build a new String with: before + guessedletter + after.

Edited 4 Years Ago by NormR1

the asterisks for the letters are stored in the c variable
im having issues with the replace function.... wont let me do char, string or vice versa

im aorry my brain is blank at the moment....having troubles thinking of how or what i need to do

letters are stored in the c variable

Having a variable with a name of a single letter: c makes it hard to find where the varable is used in the code. There are 136 c's on this page. A more uniquely spelled name would be better.

having issues with the replace function

How were you using the replace() method?
Can you post an example of what you were trying to do?
Post the before and after Strings to show it.

Did you look at using substrings?

ok so i figured out the syntax error, however now im getting and index out of bounds error when i try and run the program...i made changes to the variable names to make them more descriptive and i just dont understand what im missing here....please help..... heres the new code...the section im getting hte arror on is the hangIt(String letter) method

/*
 * File: Hangman.java
 * ------------------
 * This program will eventually play the Hangman game from
 * Assignment #4.
 */

import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.lang.*;
import java.awt.*;

public class Hangman extends ConsoleProgram {
    /*
     * Constants
     */
    public static final int N_TRYS = 13;
    public static final String guessText = "Your Guess: ";

    /*
     * Create a random generator
     * this will grab a random word from the lexicon 
     * or from a text file.
     */

    private RandomGenerator rgen = RandomGenerator.getInstance();

    /*
    * create instance variables
    * this is the word list
    */

    public HangmanLexicon word = new HangmanLexicon();

    /*
     * String Variables
     */

    public String welcome = "Welcome to Hangman!";
    public String nGame = "The New Word Looks Like This: ";
    public char newWord;
    public String hiddenWord = ""; //variable to set starting dashes
    public String guess = "";
    public String secretWord = "";


    /*
     * Create Integer Variables
     */
    public int nGuesses = N_TRYS;
    public int count = word.getWordCount() - 1;
    public int random;


    public void run() {
        /* You fill this in */
        println(welcome);

        random = rgen.nextInt(0, count); // generate a random number
        secretWord = word.getWord(random); // grab a word from the lexicon using the random number we generated
        secretWord = secretWord.toLowerCase(); // convert that word to lowercase

        for (int dash = 0; dash < secretWord.length(); dash++) {
            hiddenWord = hiddenWord + "*";
        }
        print(nGame + hiddenWord + '\n');

        /*
         * starts the loop for the game
         */
        while (nGuesses <= 13 && nGuesses != 0) {
            println("You have " + nGuesses + " Guesses left");
            guess = readLine("Your Guess: ");
            if (guess.length() > 1) {
            println("Invalid Guess. Please guess 1 letter at a time");
            print(nGame + hiddenWord + '\n');
            } else {
                hangIt(guess);
            }
        }

    }

    private void hangIt(String letter) {
        int index = secretWord.indexOf(letter);
        char d = secretWord.charAt(index);
        char nletter = hiddenWord.charAt(index);
        if (index == -1) {
            println("Invalid Guess. Try again");
            print(nGame + hiddenWord + '\n');
            nGuesses--;
        } else {
            hiddenWord = hiddenWord.replace(nletter, d);
            print(nGame + hiddenWord + '\n');
        }
    }

}

duh me i solved the logic error was where i had the chars declared in the method...however im still having a small issue of instead of it replacing one char it replaces all chars....ill work on it awhile and if i cant figure it out i will be back.

instead of it replacing one char it replaces all chars.

Did you read the API doc for the replace() method?

This article has been dead for over six months. Start a new discussion instead.