Algorithm updates. We no longer crash/get raise:'d but we miss words.
[scrabbalize.git] / Source / AppController.m
1 /*
2 * Scrabbalize
3 * Copyright (c) 2007-2008, 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 * Destructor
48 */
49 - (void)dealloc
50 {
51 [dictionary release];
52 [super dealloc];
53 }
54
55 /**
56 * Action that filters through all the words with the given tiles and then produces
57 * the list
58 */
59 - (IBAction)findWords:(id)sender
60 {
61 [wordlist removeObjects:[wordlist arrangedObjects]];
62
63 Word *temp = [[Word alloc] initWithWord:[tilesField stringValue]];
64 NSString *tiles = [temp alphagram];
65 [temp release];
66
67 NSRange wildRange = [tiles rangeOfString:@"?" options:NSBackwardsSearch];
68 int numWild = (wildRange.location == NSNotFound ? 0 : wildRange.length + wildRange.location);
69 int length = [tiles length];
70
71 for (int i = 0; i < [dictionary count]; i++)
72 {
73 Word *word = [dictionary objectAtIndex:i];
74 int wildCounter = numWild;
75
76 if ([word length] > length)
77 {
78 continue;
79 }
80
81 BOOL add = YES;
82 int j, k;
83 for (j = 0, k = numWild; k < length && j < [word length]; j++, k++)
84 {
85 if ([[word alphagram] characterAtIndex:j] != [tiles characterAtIndex:k])
86 {
87 wildCounter--;
88 if (wildCounter < 0)
89 {
90 add = NO;
91 break;
92 }
93 else
94 {
95 continue;
96 }
97 }
98 }
99
100 if (j < [word length] && j + wildCounter < [word length])
101 {
102 add = NO;
103 }
104
105 if (add)
106 {
107 [wordlist addObject:word];
108 }
109 add = YES;
110 }
111
112 // resort
113 [wordlistView setSortDescriptors:nil];
114 [wordlistView setSortDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"length" ascending:NO]]];
115 }
116
117 @end