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

# jaishreedhage'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/ScrabbleScoreTest/ScrabbleScore.h

``````//
//  ScrabbleScore.h
//  ScrabbleScore
//
//  Created by Jaishree Dhage on 09/12/17.
//

#import <Foundation/Foundation.h>

@interface ScrabbleScore : NSObject

@property (nonatomic,strong,readonly) NSString *word;

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

@end``````

### ScrabbleScore/ScrabbleScoreTest/ScrabbleScore.m

``````//
//  ScrabbleScore.m
//  ScrabbleScore
//
//  Created by Jaishree Dhage on 09/12/17.
//

#import "ScrabbleScore.h"

@interface ScrabbleScore ()

@property (nonatomic,strong,readwrite) NSString *word;

@end

@implementation ScrabbleScore

-(instancetype)initWithWord:(NSString *)word{
self = [super init];
if(self){
_word = [word uppercaseString];
}
return self;
}

+(NSUInteger)score:(NSString *)word{
ScrabbleScore *scrabbleScore = [[ScrabbleScore alloc]init];
scrabbleScore.word = [word uppercaseString];
return [scrabbleScore score];
}

-(NSUInteger)score{
if(self.word==nil || self.word.length==0)
return 0;
NSUInteger score = 0;
for(int i=0;i<self.word.length;i++){
unichar c = [self.word characterAtIndex:i];
if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U' || c=='L' || c=='N' || c=='R' || c=='S' ||c=='T')
score += 1;
else if(c=='D' || c=='G')
score += 2;
else if(c=='B' || c=='C' || c=='M' || c=='P')
score += 3;
else if(c=='F' || c=='H' || c=='V' || c=='W' || c=='Y')
score += 4;
else if(c=='K')
score += 5;
else if(c=='J' || c=='X')
score += 8;
else if(c=='Q' || c=='Z')
score += 10;
else
score += 0;
}
return score;
}

@end``````