Phrase Guessing Game Help

Hi! I'm trying to make a phrase guessing game.
The user enters a code, then it gets hidden. For example, if the phrase entered it "java rules", the computer displays it as "??? ???". Then, the user inputs a letter. If the letter is right, say "a", then it should display, "?a?a ???".
So far, after one letter is entered, it prints each letter of the secret phrase twice (so it prints "???a???a ???"). After two letters are entered, it prints each letter of the secret phrase three times(I guessed "r" and then got ???a???a? ???r???").

I think I know what I'm doing wrong, but I have no idea how to fix it! Also, I can't figure out how to make the program terminate when the secret phrase has been completly revealed. Any help would be greatly appreciated! I'm still pretty knew to this, so please explain your thoughts.

Side note: I can't use anything I haven't learned, which basically means I'm limited to loops and if statements.
Here's the code so far:

What we need for what we've got is a question. What does it do that it's not supposed to do? Is it always supposed to have one letter per un-guessed character?

rc

Nicole Miller
Greenhorn

Joined: Sep 08, 2011
Posts: 13

posted Oct 30, 2011 19:32:04

0

It does run, but incorrectly.

If I input the secret phrase to be "one two", then it outputs it as "??? ???", which is correct. However, then I'm supposed to guess the letters, so I input the letter "o". Instead of displaying "o?? ??o" like it should, it displays "?o??? ???o". It's printing each letter in the secret phrase twice. I know its doing this because the length of the guesses String is two, so it's going through the loop twice. I just have no idea how to rearrange my code so this doesn't happen.

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479

posted Oct 30, 2011 19:58:07

0

Ah. Now I see. I think this is a little tricky for a beginner. You've analyzed your problem correctly.

I suggest starting with moving the code that gets the first guess to BEFORE the loop that compares guess(es) to the phrase. It will go within the outermost loop, but outside the two others, since you want the guessing code to repeat.

Then I would alter my code so that, after each guess, for each letter in the phrase:

if the letter was in the string keeping correctly guessed letters, output that letter,
else if the letter was the one guessed, output THAT letter and add it to the string of correctly guessed letters,
else output a '?'

to terminate the program; perhaps create a boolean variable called, say, "done". Instead of the outermost loop, you can use a while loop:

if you're allowed to use those. Just before checking a new guess, set it to true; if you output a ?, indicating there are still unguessed letters, set it to false.

If you're not allowed to use while loops yet, then if you get through all the letters and done is still true, put in the line "System.exit();"

Let us know how it goes!

rc

Nicole Miller
Greenhorn

Joined: Sep 08, 2011
Posts: 13

posted Oct 30, 2011 20:33:54

0

Thanks! I think I did the boolean correctly - we can use them, but we haven't really gone over them in class.
I still have a problem running the loop, though I get your logic. I tried to add the changes, but it still doesn't work right.
Here's the new code: (I added the section in the beginning to display the hidden phrase before the user guesses - that's part of the needed program).

What is going wrong in the 3rd loop part?

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479

posted Oct 30, 2011 21:16:23

0

Pretty good work, especially considering I steered you slightly wrong. I've done more programming on this one than I normally would as a teaching exercise, because trying to do it the way I told you won't work, and fixing it is definitely not part of your assignment (since you didn't come up with it yourself).

First of all -- when you put code on JavaRanch, please enclose it in code tags. That means put openbracket, then "code", then closebracket, then all your code, then openBracket, slash, "code", and closebracket. That formats it to be easier to read on the website.

Second of all -- the reason your code doesn't enter your loop is the construct "if (done = false)" - in Java, the single equals sign is assignment, never equality testing, and what you meant was "done == false. (Incidentally, if someone says you should use "(!done)" instead, they aren't really wrong -- both these things return true if the boolean value is false, and yours is easier for a beginner to read.)

So -- the next problem is that you're altering "correctLetters" inside a loop that is looping on the length of correctLetters; that's generally a bad idea. And using the logic the way I laid it out is going to make that difficult to change that way. So let me alter a simpler alternative.

Keep a list of guessed letters instead of a list of correct letters. No matter what the user enters, add it to your string. We should change the name of it now; it is a string of guessed letters. Then your logic can go through all your guessed letters to see if they are in the string when you're doing your output.

You'll also need to know whether you output any letter in that loop; I've put in another boolean to be set true if I find and output a guessedLetter as part of the phrase. If I didn't, then I need to output a question mark.

I hesitate to do this, but it's late and I'm heading to bed after this post; because of that, and because I led you wrong, I'm just going to put my solution in here. I suggest you use it to alter your program instead of just copying this one, but no one can say you have not made an effort on the problem.

rc

Once you are happy with this, I have a challenge for you -- extra credit, so to speak -- there is a method on String named "indexOf()". Look at it and see if you can figure out how it might be used to simplify your program a bit.

Thanks! I'll look over everything and try to fix my old program with your advice to make sure I understand everything instead of using the solution. I'll look up the information on indexOf() to see how it works and would apply.

It would do almost as well without it. What is says to me as a programmer is "The code has been looping, but at this point it has done what it needs to do in the loop and now doesn't need to do it any more." It tells the reader that the loop is done, and saves some execution time (not significant here, and in fact hardly anywhere).

I have not thought through what happens if your user guesses the same letter twice -- I suspect that, with the break, the code is all right; without it, it might print the letter twice?

Nicole Miller wrote:I've read through your code several times now and have a couple quick questions: What is the significance of break at line 47? Wouldn't it work just as well without it? What exactly does it do here?

A tip from an old hand: when you're trying to solve a problem in Java, very often the best way to do it, oddly enough, is to avoid Java altogether.

Right now, you're worrying about the bits and bytes, and what a break means, and when to set variables, and when to exit a loop....DON'T.
Turn off your computer and sit down with a piece of paper and pencil, and concentrate on the problem.

Pick a phrase and go through the business of selecting a letter and filling it in. What's happening? What steps are you going through to fill the letter in in the correct places? Describe them in English (or your native language), and don't stop until you are absolutely sure of all the steps required to do the task.

THEN turn your computer back on and start writing Java. You'll find the whole process a lot easier when you understand the problem first.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here