First of all I want to thank everyone who helped me.. Dave, Greg, and K.
Ok guys.. I have one last problem then I can finally hand this thing in.
I dont think this is even a programming problem, more of a trouble shooting problem. but here goes.
I would like a counter that incraments the number of INCORRECT guesses. I can incrament all guesses right now but I have no way of incramenting the incorrect ones. Here is my code.. please let me know if it required a complete re write or just a minor adjustment.
import york.*; /* This class plays a game of Hang Man */
public class HangManGame { public static void main (String[] args) { char sentinel = 'y'; while (sentinel !='n') { York.println ("Please enter a secret word."); String secretWord = York.readLine(); char guess; String prevGuess = " "; int i = secretWord.length(); int l; int j; String answer = "*"; for (l=1; l<i ;l++) { answer = answer.concat("*"); } York.println(answer); York.println("Please choose a letter"); while (i!=0 && !answer.equals(secretWord))//I cant get it to recognize this word as being equal. { //counts the number of guesses York.println(); guess = York.readChar(); for (j=0; j<secretWord.length(); j++) { if (guess==secretWord.charAt(j))// I could not get the equalsIgnoreCase to work here. answer = answer.substring(0,j)+guess+answer.substring(j+1); }
York.println(); York.println(answer); prevGuess = prevGuess+" "+(guess); York.println("You guessed "+guess+ "."); York.println("Your previous guesses include "+prevGuess+" ."); York.println("You have " +(i)+ " guesses left"); } York.println("Game Over"); York.println("Do you want to play another game?"); sentinel = York.readChar(); York.println("Please enter y or n."); } } }
Since you're already testing to see if the character matches, just change your counter when it doesn't. And regarding your comment following that if statement, String's equalIgnoreCase method takes a String argument; since you want to compare two chars rather than two Strings, you might try something like
One note - I think your code would be simpler if you'd followed David's advice about using a StringBuffer (you wouldn't need those substrings anymore, and your initialization of answer could then be improved).
Wait K.. I spoke too soon.. for some reason the program will compile on my home machine, but I get one error on the machine at school(Via Telnet)..I get an error with this lin if ( secretWord.indexOf(Character.toString(guess))<0) I dont get the error at home.. in fact at home the program works perfectly... can anyone explain this..
I have initialized the Java enviroment at school as well
Below I have posted an alternative way to tackle this problem. Main difference in the program below is that it tries to be more true to OOP by separating the concepts of the word to guess and the game of hangman into separate classes. (The HangMan class uses an instance of the Word class via composition).
I wrote it quickly, so I am sure that there are plently of areas for improvement in logic and constructs.
I always find that it is a good learning experience to look at how others solved problems after I have solved them myself. Perhaps you will find the same.
import java.util.*;
import javax.swing.*;
class Word
{
private String word;
private StringBuffer displayWord = new StringBuffer();
Word(String s)
{
word = s;
int wordLength = s.length();
for(int i = 0 ; i < wordLength; i++)
{
displayWord.append("*");
}
}//end of constructor
public String getWord ()
{
return displayWord.toString();
}
publicboolean checkChar (char c)
{
int place = word.indexOf(c);
if(place == -1)
{
returnfalse;
} else {
char[] array = word.toCharArray();
ArrayList list = new ArrayList();
for(int i = 0 ; i < array.length; i++)
{
if(array[i] == c)
list.add(new Integer(i));
}
Object[] objArray = list.toArray();
for(int i = 0; i < objArray.length; i++)
{
int currentPlace = ((Integer)objArray[i]).intValue();
displayWord.setCharAt(currentPlace, c);
}
returntrue;
}
}//end of checkChar method
publicboolean wordGuessed()
{
int i = displayWord.indexOf("*");
if(i < 0)
returntrue;
returnfalse;
}//end of wordGuessed method
};//end of class word
publicclass Hangman
{
staticint correctGuesses = 0;
staticint incorrectGuesses = 0;
staticint totalGuesses = 0;
static Word word;
staticchar[] guessedAlready = newchar[26];
staticint guessedAlreadyCounter = 0;
publicstaticvoid main(String[] args)
{
try
{
//Get secret word from user and create "Word" object with it.
String input = JOptionPane.showInputDialog("Enter the secret word: ");
word = new Word(input);
//Use a while loop to keep having the user enter guesses until the word is complete
while(!word.wordGuessed())
{
input = JOptionPane.showInputDialog("Guess a letter: ");
char guess = input.charAt(0);
if(alreadyGuessed(guess))
{
System.out.println("You already guessed the character: " + guess + "\n");
continue;
}
totalGuesses++;
if(word.checkChar(guess))
{
correctGuesses++;
System.out.println("LETTER MATCHED");
} else {
incorrectGuesses++;
System.out.println("NOT A MATCH");
}
printState();
}
System.exit(0);
} catch (Exception e) {
System.exit(1);
}//end of try/catch block... used to catch errors related to the use of SWING components
}//end of main method
staticboolean alreadyGuessed(char c)
{
for(int i = 0 ; i < guessedAlready.length; i++)
{
if(guessedAlready[i] == c)
returntrue;
}
guessedAlready[guessedAlreadyCounter++] = c;
returnfalse;
}
staticvoid printState()
{
System.out.println("Current state of unknown word: " + word.getWord() );
System.out.println("Total guesses: " + totalGuesses);
System.out.println("Correct guesses: " + correctGuesses );
System.out.println("Incorrect guesses: " + incorrectGuesses );
System.out.println("");
}
};
I'm guessing the problem is a difference in the versions of java being used. The static toString method of Character looks like it was introduced in 1.4, so try this instead:
if ( secretWord.indexOf( ( new Character( guess ) ).toString() ) < 0 )