-2

I am writing an iOS app for a game that is similar to Hangman, except that the player is required to guess the secret word one letter at a time, starting with the first letter. The secret word is displayed as asterisks (*) in a UITextField at the beginning of the game.

When the player guesses the first letter, the program should compare it against the secret word to see if the letter is correct. If the guess is correct, the app should replace the first asterisk with the correct letter. If the guess is incorrect, some other action will be taken. The player repeats this process one letter at a time until the secret word has been completely spelled out.

Here is the code I am currently using to check the guessed letter against the secret word, but it is not working properly.

-(void) checkGameLetter : (NSString *) letterToCheck{
    bool match = NO;
    NSRange gameLetterRange;
    char charToCheck = [letterToCheck characterAtIndex:0];
        for(int i = 0; i < self.correctWord.length; i++)
        {
            char tempString = [self.correctWord characterAtIndex:i];
            if(charToCheck == tempString){
                match = YES;
                gameLetterRange = NSMakeRange(i, 1);//location, length
                Screen.text =[Screen.text stringByReplacingCharactersInRange:gameLetterRange withString:letterToCheck];

        }

}
  • I'm having trouble understanding your question… Is this for a Hangman app? Are the "unknown" letters shown with an asterisk (*)? – aapierce Apr 21 '14 at 18:53
  • @aapierce yes the unknown letter shown by* it is for app like Hangman but I want the player to enter the letters in their order! for example if I have a word (book) the player should enter first 'b' then the game will check if it is correct and then the player should enter the second letter 'o' then the third letter 'o' and so on. – user3554683 Apr 21 '14 at 18:55
  • So all you are asking is how to extract the last char from the entered word? – vikingosegundo Apr 21 '14 at 19:02
  • @vikingosegundo I want the check the entered letters by their correct order(index)in the game. char at index 0 then char at index 1 then char at index 2 and so on – user3554683 Apr 21 '14 at 19:06
  • @user3554683 I completely re-wrote your question to try and make it more clear. If I got something wrong, please feel free to correct it. – aapierce Apr 21 '14 at 19:07
  • @aapierce thank you very much thats what I want to say exactly – user3554683 Apr 21 '14 at 19:15

1 Answers1

1

The thing that's wrong with your code is that nothing in it says which letter of the correct word we are checking against.

For example, suppose the word is "zork" and the user guesses "r". You are trying to walk through "zork" looking to see if "r" matches any letter. But according to your spec, if this is a guess at the first letter, we should just be checking against the first letter ("z") and stop, since the "r" is wrong in that position.

So what you want to write is much simpler than the code you have. You don't want this:

-(void) checkGameLetter : (NSString *) letterToCheck{

You want this:

-(void) checkGameLetter:(NSString*)letterToCheck againstPosition:(NSInteger)position {

And there will be no loop: you will just look right at the letter in that position and see if they are the same.

Finally notice this important fact: == does not compare two strings. It asks whether they are the same object, which they manifestly are not. You want isEqualToString:.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • thank you for help but I try to solve the code as you said but I have many mistakes! I am new to objective-c and try my best but I didn't have the right code! can you explain it again to me ? and why I don't need to use a loop! how can I change the position every correct answer? – user3554683 Apr 22 '14 at 00:56
  • can you or any one explain more,please. – user3554683 Apr 22 '14 at 00:58