Reputation: 11
I'm creating a word search game program for a project, and want to know if what I want to do is possible. Below is the isPuzzleWord method that followed the guidelines of my project, namely that it has to return the word class object from an array if the word is right, or null if it is not. My isPuzzleWord method works fine.
public static Word isPuzzleWord (String guess, Word[] words) {
for(int i = 0; i < words.length; i++) {
if(words[i].getWord().equals(guess)) {
return words[i];
}
}
return null;
}
My question is how I can incorporate both of these responses into an if statement so that I can continue the game if the guess was right or provide feedback to the user if the guess was wrong.
public static void playGame(Scanner console, String title, Word[] words, char[][] puzzle) {
System.out.println("");
System.out.println("See how many of the 10 hidden words you can find");
for (int i = 1; i <= 10; i++) {
displayPuzzle(title, puzzle);
System.out.print("Word " + i + ": ");
String guess = console.next().toUpperCase();
isPuzzleWord(guess,words);
if (
}
}
Upvotes: 0
Views: 73
Reputation: 253
You could store reference of returned word to use after if
statement.
Word word = isPuzzleWord(guess,words);
if (word == null) {
System.out.println("its not a Puzzle Word");
} else {
//you could access `word` here
}
Upvotes: 0
Reputation: 407
Try the following if-else function:
if (isPuzzleWord(guess, words) == null){
System.out.println("Your Feedback"); //this could be your feedback or anything you want it to do
}
If the return from the isPuzzleWord is null, then you can provide your feedback, otherwise it would mean that the words matched and you can continue with the play without further action.
Upvotes: 0
Reputation: 6395
You simply put the function you are calling into the if clause:
if (isPuzzleWord(guess,words) == null)
or whatever you want to test.
Upvotes: 1