For a class project, we have to create a hangman game in Java (we’re starting off with object oriented programming, so this is getting us used to it) and I have the class file then the main file.
Everything’s working great, but I want one of my methods in the class file to tell the player if they’ve already guessed a particular character.
Basically, the method uses a for loop to add the guessed characters into a character array, and every time the player guesses it checks to see if that character is there (if it is, it breaks out of the loop) and if not, and the index value is 0 (this means it’s unwritten, right?) it will write the character that was guessed to that index value.
The only other thing that I don’t think is self explanatory in the code, is that if the player has yet to make any guesses, it makes the first value the guess, so it has something to start with in the array.
Any help would be appreciated, and if anyone has any input on how to improve my code or whatever, I’d be happy to hear that as well. Thanks so much. 🙂
public void makeGuess(char c) {
boolean alreadyGuessed = false, anyMatches = false;
matches = 0;
guesses++;
if (guesses == 1) {
guessedChars[0] = c;
}
for (int i = 0; i < guessedChars.length; i++) { //it goes through it and will see that it was already guessed
if (guessedChars[i] == c) {
alreadyGuessed = true;
break;
}
else if (guessedChars[i] != c && guessedChars[i] == 0) {
guessedChars[i] = c;
}
}
if (alreadyGuessed == false) {
for (int i = 0; i < word.length; i++) {
if (word[i] == c) {
anyMatches = true;
matches++;
disguisedWord[i] = c;
}
}
}
if (anyMatches == true) {
System.out.println("You guessed correctly!");
System.out.println("There were " + matches + " matches.");
}
else if (alreadyGuessed == true) {
System.out.println("You already guessed this letter, derp.");
}
else {
System.out.println("Sorry, that character is not in the word to be guessed.");
wrongGuesses++;
}
// for (int i = 0; i < guessedChars.length; i++) {
// System.out.print(guessedChars[i] + " ");
// }
}
MAIN METHOD:
import java.util.Scanner;
class HangmanDemo {
public static void main(String[] args) {
//Object initialization
Scanner keyboard = new Scanner(System.in);
Hangman word = new Hangman("jordan");
//Variable declaration
char letterGuess;
int limit;
//Interact with user
System.out.print("What would you like the limit of guesses to be: ");
limit = keyboard.nextInt();
//BEGIN ZE GAME
while (word.isFound() == false && word.getWrongGuessCount() <= limit) {
System.out.println();
System.out.print("Letter guess: ");
letterGuess = keyboard.next().charAt(0);
word.makeGuess(letterGuess);
System.out.println(word.getDisguisedWord());
}
if (word.getGuessCount() == limit) {
System.out.println("\nSorry, too many guesses.");
}
else {
System.out.println("\nCongratulations! You succesfully solved the word!");
}
}
}
So, check this out:
First time you call the method, both alreadyGuessed and and anyMatches will stay false… and that’s obviously not good.
Second: OOP is NOT procedural P. More concretely: a method is not supposed to do as many things as you do. A method should have a lot of explaining to do it if plans to be larger than 10 lines. That’s so full of language constructs you can’t go over it fast. Split your code… less local variables, more methods.
Hope that helps.