Skip to content

Instantly share code, notes, and snippets.

@mariancerny
Created May 25, 2014 20:41
Show Gist options
  • Save mariancerny/eb570f711ac01246c26a to your computer and use it in GitHub Desktop.
Save mariancerny/eb570f711ac01246c26a to your computer and use it in GitHub Desktop.
Coding Dojo #11 (2014-05-22)
//
// BowlingKataTests.m
// BowlingKataTests
//
// Created by Marián Černý on 22.05.14.
//
//
#import <XCTest/XCTest.h>
const NSInteger numberOfRounds = 10;
const NSInteger spareBonus = 1;
const NSInteger strikeBonus = 2;
const NSInteger doubleStrikeBonus = 3;
@interface BowlingKataTests : XCTestCase
@end
@implementation BowlingKataTests
- (NSInteger)score:(NSArray *)rolls
{
__block NSInteger score = 0;
__block BOOL isSecondRollInRound = NO;
__block NSNumber *previousPinsCount;
__block NSInteger bonusCounter = 0;
__block NSInteger roundCount = 1;
[rolls enumerateObjectsUsingBlock:^(NSNumber *pinsCount, NSUInteger idx, BOOL *stop) {
NSInteger multiplier = 0;
if(roundCount <= numberOfRounds)
{
multiplier++;
}
if(bonusCounter > 0)
{
multiplier++;
if (bonusCounter == doubleStrikeBonus) {
multiplier++;
}
bonusCounter --;
}
score += [pinsCount integerValue] * multiplier;
NSLog(@"Round:%d (idx:%d) score:%d bonusCounter:%d multiplier:%d", roundCount, idx, score, bonusCounter, multiplier);
NSInteger sumOfLastTwoRolls = [pinsCount integerValue] + [previousPinsCount integerValue];
if([self isSpareForSecondRollInRound:isSecondRollInRound sumOfLastTwoRows:sumOfLastTwoRolls])
{
bonusCounter = spareBonus;
}
if([self isStrikeForSecondRollInRound:isSecondRollInRound pinsCount:pinsCount])
{
isSecondRollInRound = YES;
if(roundCount <= numberOfRounds)
{
if(bonusCounter >= spareBonus)
bonusCounter = doubleStrikeBonus;
else
bonusCounter = strikeBonus;
}
}
if(isSecondRollInRound)
{
roundCount++;
}
isSecondRollInRound = !isSecondRollInRound;
previousPinsCount = pinsCount;
}];
return score;
}
- (BOOL) isSpareForSecondRollInRound:(BOOL)isSecondRollInRound sumOfLastTwoRows:(NSInteger)sumOfLastTwoRolls
{
return isSecondRollInRound && sumOfLastTwoRolls == 10;
}
- (BOOL) isStrikeForSecondRollInRound:(BOOL)isSecondRollInRound pinsCount:(NSNumber *)pinsCount
{
return !isSecondRollInRound && [pinsCount integerValue] == 10;
}
- (void)testNoPinsHasZeroScore
{
NSInteger score = [self score:@[@0]];
XCTAssertEqual(score, 0, @"Unexpected score");
}
- (void) testSimpleRoundCalculatesResult
{
NSInteger score = [self score:@[@1, @1]];
XCTAssertEqual(score, 2, @"Unexpected score");
}
- (void)testSpareDoublesTheNextRoll
{
NSInteger score = [self score:@[@7, @3, @5, @1]];
XCTAssertEqual(score, 21, @"Unexpected score");
}
- (void)testSpareInSecondRoundDoublesTheNextRoll
{
NSInteger score = [self score:@[@1, @1, @3, @7, @1]];
XCTAssertEqual(score, 14, @"Unexpected score");
}
- (void)testThereIsNoSpareWhenTenIsThrownBetweenRounds
{
NSInteger score = [self score:@[@1, @3, @7, @2]];
XCTAssertEqual(score, 13, @"Unexpected score");
}
- (void)testSimpleStrike
{
NSInteger score = [self score:@[@10, @1, @1]];
XCTAssertEqual(score, 14, @"Unexpected score");
}
- (void)testStrikeInSecondRound
{
NSInteger score = [self score:@[@1, @1, @10, @1, @1]];
XCTAssertEqual(score, 16, @"Unexpected score");
}
- (void)testMoreStrikesInRow
{
NSInteger score = [self score:@[@10, @10, @1, @1]];
XCTAssertEqual(score, 35, @"Unexpected score");
}
- (void)testGameWithStrikeInTheEnd
{
NSInteger score = [self score:@[@0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @0, @10, @1, @1]];
XCTAssertEqual(score, 12, @"Unexpected score");
}
- (void)testComplexGame
{
NSInteger score = [self score:@[@1, @4, @4, @5, @6, @4, @5, @5, @10, @0, @1, @7, @3, @6, @4, @10, @2, @8, @6]];
XCTAssertEqual(score, 133, @"Unexpected score");
}
- (void)testGameWithFourStrikes
{
NSInteger score = [self score:@[@10, @10, @10, @10, @1, @1]];
XCTAssertEqual(score, 95, @"Unexpected score");
}
- (void)testGameWithAllStrikes
{
NSInteger score = [self score:@[@10, @10, @10, @10, @10, @10, @10, @10, @10, @10, @10, @10]];
XCTAssertEqual(score, 300, @"Unexpected score");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment