Bumping the copyright year to 2008
[scrabbalize.git] / Source / Word.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 "Word.h"
18
19 @interface Word (Private)
20 - (void)createAlphagram;
21 @end
22
23
24 @implementation Word
25
26 /**
27 * Creates a new word object
28 */
29 - (id)initWithWord:(NSString *)aWord
30 {
31 if (self = [super init])
32 {
33 word = [aWord retain];
34 [self createAlphagram];
35 }
36 return self;
37 }
38
39 /**
40 * Destructor
41 */
42 - (void)dealloc
43 {
44 [word release];
45 [alphagram release];
46 [super dealloc];
47 }
48
49 /**
50 * Initialize an object from a keyed archive
51 */
52 - (id)initWithCoder:(NSCoder *)coder
53 {
54 self = [super init];
55 word = [[coder decodeObjectForKey:@"KAWord"] retain];
56 alphagram = [[coder decodeObjectForKey:@"KAAlphagram"] retain];
57 return self;
58 }
59
60 /**
61 * Encodes an object for serialization in a keyed archiver
62 */
63 - (void)encodeWithCoder:(NSCoder *)coder
64 {
65 [coder encodeObject:word forKey:@"KAWord"];
66 [coder encodeObject:alphagram forKey:@"KAAlphagram"];
67 }
68
69 /**
70 * Returns the word
71 */
72 - (NSString *)word
73 {
74 return word;
75 }
76
77 /**
78 * Gets the length of the word
79 */
80 - (int)length
81 {
82 return [word length];
83 }
84
85 /**
86 * Returns the alphagram
87 */
88 - (NSString *)alphagram
89 {
90 return alphagram;
91 }
92
93 /**
94 * Generates the alphagram for the word
95 */
96 - (void)createAlphagram
97 {
98 char *str = (char *)[word UTF8String];
99
100 int j;
101 for (int i = 1; i < [word length]; i++)
102 {
103 char temp = str[i];
104 for (j = i; j > 0 && temp < str[j - 1]; j--)
105 {
106 str[j] = str[j - 1];
107 }
108 str[j] = temp;
109 }
110
111 alphagram = [[NSString stringWithUTF8String:str] retain];
112 }
113
114 /**
115 * Description method
116 */
117 - (NSString *)description
118 {
119 return [NSString stringWithFormat:@"<Word:%@/%@>", word, alphagram];
120 }
121
122 @end