Removing the extra dictionary tool because it's not really needed. Instead, we will...
[scrabbalize.git] / Source / AppController.m
1 /*
2 * Scrabbalize
3 * Copyright (c) 2007, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "AppController.h"
18 #import "Word.h"
19 #import "NSStringAdditions.h"
20
21 @implementation AppController
22
23 /**
24 * Initializes the application and loads the dictionary archive
25 */
26 - (id)init
27 {
28 if (self = [super init])
29 {
30 NSString *path = [NSString stringWithFormat:@"%@/dictionary.txt", [[NSBundle mainBundle] resourcePath]];
31 NSString *file = [NSString stringWithContentsOfFile:path];
32 NSArray *strings = [file componentsSeparatedByString:@"\n"];
33
34 dictionary = [[NSMutableArray alloc] init];
35 for (int i = 0; i < [strings count]; i++)
36 {
37 NSString *str = [strings objectAtIndex:i];
38 Word *word = [[Word alloc] initWithWord:str];
39 [dictionary addObject:word];
40 [word release];
41 }
42 }
43 return self;
44 }
45
46 /**
47 * Action that filters through all the words with the given tiles and then produces
48 * the list
49 */
50 - (IBAction)findWords:(id)sender
51 {
52 [wordlist removeObjects:[wordlist arrangedObjects]];
53
54 Word *temp = [[Word alloc] initWithWord:[tilesField stringValue]];
55 NSString *tiles = [temp alphagram];
56 [temp release];
57
58 NSRange wildRange = [tiles rangeOfString:@"?" options:NSBackwardsSearch];
59 int numWild = wildRange.length + wildRange.location;
60 int length = [tiles length];
61
62 for (int i = 0; i < [dictionary count]; i++)
63 {
64 Word *word = [dictionary objectAtIndex:i];
65 int wildCounter = numWild;
66
67 if ([word length] > length)
68 {
69 continue;
70 }
71
72 BOOL add = YES;
73 for (int j = 0, k = numWild; j < length; j++, k++)
74 {
75 if ([[word alphagram] characterAtIndex:j] != [tiles characterAtIndex:k])
76 {
77 wildCounter--;
78 if (wildCounter < 0)
79 {
80 add = NO;
81 break;
82 }
83 else
84 {
85 continue;
86 }
87 }
88 }
89
90 if (add)
91 {
92 [wordlist addObject:[word word]];
93 }
94 add = YES;
95 }
96
97 // resort
98 [wordlistView setSortDescriptors:nil];
99 [wordlistView setSortDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"length" ascending:NO]]];
100 }
101
102 @end