ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

stevenhuey's solution

to Scrabble Score in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Given a word, compute the scrabble score for that word.

Letter Values

You'll need these:

``````Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10
``````

Examples

"cabbage" should be scored as worth 14 points:

• 3 points for C
• 1 point for A, twice
• 3 points for B, twice
• 2 points for G
• 1 point for E

And to total:

• `3 + 2*1 + 2*3 + 2 + 1`
• = `3 + 2 + 6 + 3`
• = `5 + 9`
• = 14

Extensions

• You can play a double or a triple letter.
• You can play a double or a triple word.

Setup

There are two different methods of getting set up to run the tests with Objective-C:

• Create an Xcode project with a test target which will run the tests.
• Use the ruby gem `objc` as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

``````exercism submit <path-to-exercism-workspace>/objective-c/scrabble-score/ScrabbleScore.m
``````

You can find the Exercism workspace by running `exercism debug` and looking for the line beginning with Workspace.

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ScrabbleScoreTest.m

``````#import <XCTest/XCTest.h>

#if __has_include("ScrabbleScoreExample.h")
# import "ScrabbleScoreExample.h"
# else
# import "ScrabbleScore.h"
#endif

@interface ScrabbleScoreTest : XCTestCase

@end

@implementation ScrabbleScoreTest

- (void)testEmptyWordScoresZero {
XCTAssertEqual(0, [[[ScrabbleScore alloc] initWithWord:@""] score]);
}

- (void)testWhitespaceScoresZero {
XCTAssertEqual(0, [[[ScrabbleScore alloc] initWithWord:@" \t\n"] score]);
}

- (void)testNilScoresZero {
XCTAssertEqual(0, [[[ScrabbleScore alloc] initWithWord:nil] score]);
}

- (void)testScoresVeryShortWord {
XCTAssertEqual(1, [[[ScrabbleScore alloc] initWithWord:@"a"] score]);
}

- (void)testScoresOtherVeryShortWord {
XCTAssertEqual(4, [[[ScrabbleScore alloc] initWithWord:@"f"] score]);
}

- (void)testSimpleWordScoresTheNumberOfLetters {
XCTAssertEqual(6, [[[ScrabbleScore alloc] initWithWord:@"street"] score]);
}

- (void)testComplicatedWordScoresMore {
XCTAssertEqual(22, [[[ScrabbleScore alloc] initWithWord:@"quirky"] score]);
}

- (void)testScoresAreCaseInsensitive {
XCTAssertEqual(41, [[[ScrabbleScore alloc] initWithWord:@"OXYPHENBUTAZONE"] score]);
}

- (void)testConvenientScoring {
XCTAssertEqual(13, [ScrabbleScore score:@"alacrity"]);
}

@end``````
``````//
//  ScrabbleScore.m
//  ScrabbleScore
//
//  Created by Steve Huey on 6/13/17.
//

#import "ScrabbleScore.h"

@interface ScrabbleScore ()
@property (nonatomic, copy) NSString* word;

- (NSUInteger)scoreForLetter:(unichar)c;
- (NSDictionary*)letterValues;
@end

@implementation ScrabbleScore
+ (NSUInteger)score:(NSString*)word
{
ScrabbleScore* scrabble = [[ScrabbleScore alloc] initWithWord:word];

if (scrabble)
{
return [scrabble score];
}
else
{
return 0;
}
}

- (instancetype)initWithWord:(NSString*)word
{
self = [super init];

if (self)
{
_word = [word copy];
}

return self;
}

- (NSUInteger)score
{
NSUInteger score = 0;

for (NSUInteger i = 0; i < [self.word length]; ++i)
{
score += [self scoreForLetter:[self.word characterAtIndex:i]];
}

return score;
}

- (NSUInteger)scoreForLetter:(unichar)c
{
NSString* letter = [[NSString stringWithFormat:@"%c", c] uppercaseString];

NSUInteger score = 0;
NSDictionary* letterValues = [self letterValues];

for (NSNumber* value in letterValues)
{
NSString* letters = letterValues[value];

if ([letters containsString:letter])
{
score = [value unsignedIntegerValue];
break;
}
}

return score;
}

- (NSDictionary*)letterValues
{
static NSDictionary* letterValues;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
letterValues = @{@(1): @"AEIOULNRST",
@(2): @"DG",
@(3): @"BCMP",
@(4): @"FHVWY",
@(5): @"K",
@(8): @"JX",
@(10): @"QZ"};
});

return letterValues;
}

@end``````