0

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 (
    }

}
1
  • You should probably just return true or false for the isPuzzleWord method Commented Apr 9, 2016 at 2:01

3 Answers 3

1

You simply put the function you are calling into the if clause:

if (isPuzzleWord(guess,words) == null) or whatever you want to test.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!
0

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.

Comments

0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.