Created
November 2, 2010 05:11
-
-
Save abyx/659272 to your computer and use it in GitHub Desktop.
Bowling Kata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Game { | |
private static final int MAX_ROLLS_IN_GAME = 21; | |
private static final int FRAMES_IN_GAME = 10; | |
private int rolls[] = new int [MAX_ROLLS_IN_GAME]; | |
private int currRoll; | |
public void roll(int pins) { | |
rolls[currRoll] = pins; | |
currRoll++; | |
} | |
public int score() { | |
int score = 0; | |
int frameIndex = 0; | |
for (int i = 0; i < FRAMES_IN_GAME; i++) { | |
if (isStrike(frameIndex)) { | |
score += strikeFrameScore(frameIndex) + strikeBonus(frameIndex); | |
frameIndex++; | |
} else if (isSpare(frameIndex)) { | |
score += fullFrameScore(frameIndex) + spareBonus(frameIndex); | |
frameIndex += 2; | |
} else { | |
score += fullFrameScore(frameIndex); | |
frameIndex += 2; | |
} | |
} | |
return score; | |
} | |
private int strikeBonus(int frameIndex) { | |
return rolls[frameIndex + 1] + rolls[frameIndex + 2]; | |
} | |
private int strikeFrameScore(int frameIndex) { | |
return rolls[frameIndex]; | |
} | |
private boolean isStrike(int frameIndex) { | |
return rolls[frameIndex] == 10; | |
} | |
private int spareBonus(int frameIndex) { | |
return rolls[frameIndex + 2]; | |
} | |
private int fullFrameScore(int frameIndex) { | |
return rolls[frameIndex] + rolls[frameIndex + 1]; | |
} | |
private boolean isSpare(int frameIndex) { | |
return fullFrameScore(frameIndex) == 10; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.*; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class GameTest { | |
private Game g; | |
@Before | |
public void setUp() { | |
g = new Game(); | |
} | |
@Test | |
public void gutterGameScoreIsZero() throws Exception { | |
rollMany(0, 20); | |
assertEquals(0, g.score()); | |
} | |
@Test | |
public void gameWithOnlyOnesScoreIs20() throws Exception { | |
rollMany(1, 20); | |
assertEquals(20, g.score()); | |
} | |
private void rollMany(int pins, int n) { | |
for (int i = 0; i < n; i++) { | |
g.roll(pins); | |
} | |
} | |
@Test | |
public void oneSpareShouldGetBonusForThatSpare() throws Exception { | |
rollSpare(); | |
g.roll(3); | |
rollMany(0, 17); | |
assertEquals(10 + 3 + 3, g.score()); | |
} | |
private void rollSpare() { | |
g.roll(5); | |
g.roll(5); | |
} | |
@Test | |
public void oneStrikeShouldGetBonusForThatStrike() throws Exception { | |
rollStrike(); | |
g.roll(3); | |
g.roll(4); | |
rollMany(0, 16); | |
assertEquals(10 + 3 + 4 + 3 + 4, g.score()); | |
} | |
private void rollStrike() { | |
g.roll(10); | |
} | |
@Test | |
public void perfectGame() throws Exception { | |
rollMany(10, 12); | |
assertEquals(300, g.score()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment