{
[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