Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

This is what I have so far:

Java
public void checkWord(String checkLetter)
 {
     int pos = 0, tries = 0;
     while(guesNme.indexOf('_') != -1)
     {
         pos = ranWrd.indexOf(checkLetter);

         if(pos == -1)
         {
             System.out.println(pos);

             help objHelp = new help();
             objHelp.repaint();
             tries += 1;
         }
         else
         {
             guesNme = ranWrd.substring(0, pos) + checkLetter guesNme.substring(pos + 1, guesNme.length());
             lblWrd.setText(guesNme);
         }


I am doing hang man and I need to check the correct letters in the word and disable clicked letter and substitute the dash with correct letter and repaint the image to a new image.

Thank you.
Posted
Updated 3-Sep-10 5:11am
v3
Comments
Sandeep Mewara 3-Sep-10 11:09am    
So, whats the question here? You want us to verify your code? Your logic?
Dalek Dave 3-Sep-10 11:11am    
Minor Edit for Grammar.

1 solution

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:
C#
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.
 
Share this answer
 
Comments
allaparaclatus 6-Sep-10 1:10am    
Thank you William i saw the infinate loop, and i solved my logic now it workes perfectly.
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900