Jack Chase

Greenhorn
+ Follow
since Dec 20, 2017
Jack likes ...
Java
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jack Chase

Dave Tolls wrote:So can you post a run through of the code?

And the new (current) code?

As for accessing the values inside the HangmanGame instance (newgame), you are already doing that in lines 16 and 17.
You ought to be doing that in the if statement, rather than storing the state in a local variable.

At the moment, you store the value of isComplete and numOfTries as they stand before the game starts (so false, and 0).
Those values never change.
You actually want to use the values from newgame directly.

And then this points out a logic error (I've made the fix to use the newgame values):

Now, that says "continue playing while we have had fewer than 7 tries and the game isComplete".
Does that sound correct to you?



The if statement I had was not correct. I had it continue even if the game was complete. I fixed it and I call the values directly from the game class. However, the problem still persists. If I put any letter I automatically win. I've gone over the current game class and cannot figure it for the life of me.

Here is the code

Main


Game



Obviously if it wins in any input the problem should be in the checkYourself method but I cannot notice anything that would break the loop:
6 years ago

Dave Tolls wrote:This highlights one thing about how to write code using booleans.


You should never do if (boolean_value == true).
It should just be if (boolean_value).

This would prevent the above error.
You are assigning the value true to isComplete, not checking if the value is true.

This is why it always thinks the user has won.

There are other issues (some of which Tim has pointed out), but I expect you'll find those out as you test the program.



I fixed the if statement, however, it still shows the same bug. I've also found another bug where the user would have more than 7 tries since I declared the number of tries before the loop. I think I can fix this by using the values from Game class but I cannot find anywhere how to do it. If I do just if (numOfTries<7 %% isComplete) it won't compile since it cannot find the values. How can I use the values from Game class in the main class?
6 years ago

Tim Moores wrote:I notice that you're comparing strings with the "==" operator - you should be using the "equals" method instead.



Okay, thank you. I will take a look at it.
6 years ago

Tim Moores wrote:What happens if you run this code? At what point does its execution start to deviate from what you were expecting?



Any input the user gives, be it correct or false the program returns the output for the correct guess and ends the game. For example, if I enter "z" I win the game even though the word is cat.
6 years ago
Hello everyone!

I have a problem with my Hangman code. It asks for your input and no matter which letter you input it says you won even though you didn't.

I suspect I have messed up something in my main class with the if statement and the boolean value isComplete.

Main Class


Game class



Thank you, anyone who takes a look at it. Also if you have any other comments (such as my code structure) feel free to criticise me!
6 years ago