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