From fe9a9fbecd31e08a00ad8762f85c677c1b735094 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 21 May 2008 21:23:10 -0400 Subject: [PATCH] Working on a new algorithm that includes wild card matching and that uses alphagrams * Source/AppController.m: (-[findWords:]) --- Source/AppController.m | 52 +++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/Source/AppController.m b/Source/AppController.m index 55fe550..a6541fe 100644 --- a/Source/AppController.m +++ b/Source/AppController.m @@ -41,47 +41,47 @@ { [wordlist removeObjects:[wordlist arrangedObjects]]; - NSString *tileString = [tilesField stringValue]; - int tileCount = [tileString length]; + Word *temp = [[Word alloc] initWithWord:[tilesField stringValue]]; + NSString *tiles = [temp alphagram]; + [temp release]; - NSMutableArray *tiles = [NSMutableArray arrayWithCapacity:tileCount - 1]; - for (int i = 0; i < tileCount; i++) - { - [tiles addObject:[tileString substringWithRange:NSMakeRange(i, 1)]]; - } - - // create character sets from the tiles - NSCharacterSet *charset = [NSCharacterSet characterSetWithCharactersInString:tileString]; - NSCharacterSet *charsetInverted = [charset invertedSet]; + NSRange wildRange = [tiles rangeOfString:@"?" options:NSBackwardsSearch]; + int numWild = wildRange.length + wildRange.location; + int length = [tiles length]; - // iterate over the dictionary to build our word list - for (NSString *word in dictionary) + for (int i = 0; i < [dictionary count]; i++) { - // word is larger than the number of tiles we have, remove - if ([word length] > tileCount) - { - continue; - } + Word *word = [dictionary objectAtIndex:i]; + int wildCounter = numWild; - // if the word contains characters we don't have, then remove it - if ([word rangeOfCharacterFromSet:charsetInverted].location != NSNotFound) + if ([word length] > length) { continue; } - BOOL success = YES; - for (NSString *tile in tiles) + BOOL add = YES; + for (int j = 0, k = numWild; j < [word length]; j++, k++) { - if ([word occurrenceOfChar:tile] > [tileString occurrenceOfChar:tile]) + if ([[word alphagram] characterAtIndex:j] != [tiles characterAtIndex:k]) { - success = NO; + wildCounter--; + if (wildCounter < 0) + { + add = NO; + break; + } + else + { + continue; + } } } - if (success) + if (add) { - [wordlist addObject:[[Word alloc] initWithWord:word]]; + [wordlist addObject:[word word]]; } + add = YES; } // resort -- 2.22.5