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

# KiligWY'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.h

#import <Foundation/Foundation.h>

@interface ScrabbleScore : NSObject

@property (nonatomic, assign) NSInteger score;

- (instancetype)initWithWord:(NSString *)word;
+ (NSInteger)score:(NSString *)word;

@end

### ScrabbleScore.m

#import "ScrabbleScore.h"

static NSDictionary *letterValues;

@implementation ScrabbleScore

- (instancetype)initWithWord:(NSString *)word {
if (self = [super init]) {
if (!letterValues) {
letterValues = @{
@"A" : @1,
@"E" : @1,
@"I" : @1,
@"O" : @1,
@"U" : @1,
@"L" : @1,
@"N" : @1,
@"R" : @1,
@"S" : @1,
@"T" : @1,
@"D" : @2,
@"G" : @2,
@"B" : @3,
@"C" : @3,
@"M" : @3,
@"P" : @3,
@"F" : @4,
@"H" : @4,
@"V" : @4,
@"W" : @4,
@"Y" : @4,
@"K" : @5,
@"J" : @8,
@"X" : @8,
@"Q" : @10,
@"Z" : @10
};
}
self.score = 0;
for (NSInteger i = 0; i < word.length; ++i) {
self.score += [letterValues[[[word substringWithRange:NSMakeRange(i, 1)] uppercaseString]] integerValue];
}
}
return self;
}

+ (NSInteger)score:(NSString *)word {
return [[[self alloc] initWithWord:word] score];
}

@end