From a7aa2abc426c3b717a4620bdd7e1ff8614b68107 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 17 Dec 2007 15:37:11 -0500 Subject: [PATCH] Finished the algorithm for checking the words. We now make sure the word isn't too long, does not contain other characters, and the occurrence of the characters in the word is less than or equal to the number in the tile string * Source/NSStringAdditions.h: * Source/NSStringAdditions.m: ([NSString(NSStringAdditions) occurenceOfChar:]): New Method * Scrabbalize.xcodeproj: Project changes for NSStringAdditions * Source/AppController.m: Use the new algorithm and occurence method --- Scrabbalize.xcodeproj/project.pbxproj | 6 +++++ Source/AppController.m | 16 ++++++++++++-- Source/NSStringAdditions.h | 24 ++++++++++++++++++++ Source/NSStringAdditions.m | 32 +++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 Source/NSStringAdditions.h create mode 100644 Source/NSStringAdditions.m diff --git a/Scrabbalize.xcodeproj/project.pbxproj b/Scrabbalize.xcodeproj/project.pbxproj index 06b1bd1..93361aa 100644 --- a/Scrabbalize.xcodeproj/project.pbxproj +++ b/Scrabbalize.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 1EAAC7970D16E9950058A23D /* dictionary.ka in Resources */ = {isa = PBXBuildFile; fileRef = 1EAAC7960D16E9950058A23D /* dictionary.ka */; }; 1EAAC79B0D16E9CE0058A23D /* AppController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EAAC79A0D16E9CE0058A23D /* AppController.m */; }; 1EAAC8420D16F4A40058A23D /* Word.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EAAC8410D16F4A40058A23D /* Word.m */; }; + 1EAAC93C0D17129D0058A23D /* NSStringAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EAAC93B0D17129D0058A23D /* NSStringAdditions.m */; }; 8D11072A0486CEB800E47090 /* MainMenu.nib in Resources */ = {isa = PBXBuildFile; fileRef = 29B97318FDCFA39411CA2CEA /* MainMenu.nib */; }; 8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; }; 8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; }; @@ -25,6 +26,8 @@ 1EAAC79A0D16E9CE0058A23D /* AppController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AppController.m; path = Source/AppController.m; sourceTree = ""; }; 1EAAC8400D16F4A40058A23D /* Word.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Word.h; path = Source/Word.h; sourceTree = ""; }; 1EAAC8410D16F4A40058A23D /* Word.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Word.m; path = Source/Word.m; sourceTree = ""; }; + 1EAAC93A0D17129D0058A23D /* NSStringAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSStringAdditions.h; path = Source/NSStringAdditions.h; sourceTree = ""; }; + 1EAAC93B0D17129D0058A23D /* NSStringAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSStringAdditions.m; path = Source/NSStringAdditions.m; sourceTree = ""; }; 29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 29B97319FDCFA39411CA2CEA /* English */ = {isa = PBXFileReference; lastKnownFileType = wrapper.nib; name = English; path = English.lproj/MainMenu.nib; sourceTree = ""; }; 29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = ""; }; @@ -53,6 +56,8 @@ 1EAAC79A0D16E9CE0058A23D /* AppController.m */, 1EAAC8400D16F4A40058A23D /* Word.h */, 1EAAC8410D16F4A40058A23D /* Word.m */, + 1EAAC93A0D17129D0058A23D /* NSStringAdditions.h */, + 1EAAC93B0D17129D0058A23D /* NSStringAdditions.m */, ); name = Classes; sourceTree = ""; @@ -200,6 +205,7 @@ 8D11072D0486CEB800E47090 /* main.m in Sources */, 1EAAC79B0D16E9CE0058A23D /* AppController.m in Sources */, 1EAAC8420D16F4A40058A23D /* Word.m in Sources */, + 1EAAC93C0D17129D0058A23D /* NSStringAdditions.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Source/AppController.m b/Source/AppController.m index 056c3fb..7b59c85 100644 --- a/Source/AppController.m +++ b/Source/AppController.m @@ -16,7 +16,7 @@ #import "AppController.h" #import "Word.h" - +#import "NSStringAdditions.h" @implementation AppController @@ -73,7 +73,19 @@ continue; } - [wordlist addObject:[[Word alloc] initWithWord:word]]; + BOOL success = YES; + for (NSString *tile in tiles) + { + if ([word occurrenceOfChar:tile] > [tileString occurrenceOfChar:tile]) + { + success = NO; + } + } + + if (success) + { + [wordlist addObject:[[Word alloc] initWithWord:word]]; + } } // resort diff --git a/Source/NSStringAdditions.h b/Source/NSStringAdditions.h new file mode 100644 index 0000000..a34110c --- /dev/null +++ b/Source/NSStringAdditions.h @@ -0,0 +1,24 @@ +/* + * Scrabbalize + * Copyright (c) 2007, Blue Static + * + * This program is free software; you can redistribute it and/or modify it under the terms of the GNU + * General Public License as published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program; if not, + * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#import + + +@interface NSString (NSStringAdditions) + +- (int)occurrenceOfChar:(NSString *)aChar; + +@end diff --git a/Source/NSStringAdditions.m b/Source/NSStringAdditions.m new file mode 100644 index 0000000..aaae6e7 --- /dev/null +++ b/Source/NSStringAdditions.m @@ -0,0 +1,32 @@ +// +// NSStringAdditions.m +// Scrabbalize +// +// Created by Robert Sesek on 12/17/07. +// Copyright 2007 __MyCompanyName__. All rights reserved. +// + +#import "NSStringAdditions.h" + + +@implementation NSString (NSStringAdditions) + +/** + * Counts the number of times a given character is in the string + */ +- (int)occurrenceOfChar:(NSString *)aChar +{ + int count = 0; + + for (int i = 0; i < [self length]; i++) + { + if ([[self substringWithRange:NSMakeRange(i, 1)] isEqualToString:aChar]) + { + count++; + } + } + + return count; +} + +@end -- 2.43.5