You've got a pretty nice infinite loop there I'd say.
Have you looked closely at your logic or stepped through the code at all?
But honestly, your variable names don't tell a whole lot. I have no idea what
guesNme
is.
Here's the way your logic should be:
1. Get the letter that was pressed.
2. See if the letter is in the word.
3. If it is, show the letter.
4. If not, add something to the person.
5. Disable the letter from being selected.
But seriously, your code is a mess.
In:
guesNme = ranWrd.substring(0, pos) + checkLetter guesNme.substring(pos + 1, guesNme.length());
is there a typo? how does this even compile? Look between checkLetter and guesNme.substring.
And every time you go to check a letter, you reset the number of tries.
I assume that
ranWrd
is the full word and
guesNme
is what is being displayed. Is that correct?
So, in the example above the starting values would be:
ranWrd = "flawed";
guesNme = "______";
So, you get the first letter to check, say "w".
So, you set
pos = 0
and
tries = 0
.
Then you start your infinite loop. As long as
guesNme
has a single "_" in it, it will keep going checking the same letter.
So, you set
pos = 3
(
ranWrd.IndexOf("w")
).
Since
pos != -1
, you then set
guesNme = "flaw"
and then I assume you meant to have a
+
after
checkLetter
so you try to add on the rest of
guesNme.Substring(4,6)
which should throw an error since guesNme has nothing at position 6.
But, let's say it did actually add on underscores, you then get back to your while loop which says if there is a single "_" then keep going.
So, all in all, you've reset tries, started an infinite loop, exposed any letters that were before the letter selected and threw an error.
Yeah, I'd say you have some problems with your logic.