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];
}
}