Skip to content

Instantly share code, notes, and snippets.

@philou
Created December 9, 2011 09:02
Show Gist options
  • Save philou/1450818 to your computer and use it in GitHub Desktop.
Save philou/1450818 to your computer and use it in GitHub Desktop.
Kata Bowling @ Key Consulting 8-12-2011
import java.util.ArrayList;
import java.util.List;
public class Game {
class Frame {
final int firstRoll;
final int secondRoll;
public Frame(int firstRoll, int secondRoll) {
this.firstRoll = firstRoll;
this.secondRoll = secondRoll;
}
public int getFirstRoll() {
return firstRoll;
}
public int getSecondRoll() {
return secondRoll;
}
private boolean isSpare() {
return score() == 10;
}
private boolean isStrike() {
return getFirstRoll() == 10;
}
public int score() {
return getFirstRoll() + getSecondRoll();
}
}
List<Integer> rolls = new ArrayList<Integer>();
public void roll(int i) {
rolls.add(i);
}
public int score() {
int result = 0;
List<Frame> frames = frames();
for(int iFrame = 0; iFrame < 10; iFrame++) {
Frame frame = frames.get(iFrame);
if (iFrame != 0) {
Frame previousFrame = frames.get(iFrame-1);
if (previousFrame.isStrike()) {
result += frame.score();
}
else if (previousFrame.isSpare()) {
result += frame.getFirstRoll();
}
}
result += frame.score();
}
return result;
}
private List<Frame> frames() {
List<Frame> frames = new ArrayList<Frame>();
int iRoll = 0;
for (int iFrame = 0; iFrame < 10; iFrame++) {
Integer firstRoll = rolls.get(iRoll);
if (firstRoll == 10) {
frames.add(new Frame(10,0));
iRoll++;
} else {
frames.add(new Frame(firstRoll, rolls.get(iRoll + 1)));
iRoll += 2;
}
}
return frames;
}
}
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class GameTest {
@Test
public void acceptance_test() {
Game game = new Game();
game.roll(5);
game.roll(2);
game.roll(2);
game.roll(6);
game.roll(10);
game.roll(5);
game.roll(2);
game.roll(5);
game.roll(5);
game.roll(3);
game.roll(1);
game.roll(0);
game.roll(2);
game.roll(0);
game.roll(1);
game.roll(0);
game.roll(1);
game.roll(2);
game.roll(7);
//game.roll(10);
assertEquals(79, game.score());
}
Game game;
@Before
public void before() {
game = new Game();
}
@Test
public void a_gutter_game_should_score_0() {
rollGutterFrames(10);
assertEquals(0, game.score());
}
@Test
public void a_game_with_1_skittle_down_scores_1() {
rollFrame(1, 0);
rollGutterFrames(9);
assertEquals(1, game.score());
}
@Test
public void a_spare_should_double_the_next_roll_score() {
rollFrame(5, 5);
rollFrame(2, 7);
rollGutterFrames(8);
assertEquals(21, game.score());
}
@Test
public void a_strike_should_double_the_two_next_rolls() {
game.roll(10);
rollFrame(2, 7);
rollGutterFrames(8);
assertEquals(28, game.score());
}
private void rollGutterFrames(int frameCount) {
for(int i = 0; i < frameCount; i++) {
game.roll(0);
game.roll(0);
}
}
private void rollFrame(int firstScore, int secondScore) {
game.roll(firstScore);
game.roll(secondScore);
}
}
+ Pierre a adoré la manière de faire au plus simple et progressivement
- Un peu dommage qu'on ait pas été au bout
+ pas de concession génante
- difficile de programmer avec toutes les idées de tout le monde
+ on a vu pas mal de refactoring
+ XP sans objet ? oui !
+ peu couteux d'implémenter les tests en premier et de refactorer en
permanence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment