Created
December 9, 2011 08:57
-
-
Save philou/1450801 to your computer and use it in GitHub Desktop.
La solution que j'avais réalisée avant
This file contains hidden or 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
package test; | |
import static org.junit.Assert.*; | |
import main.Game; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class BowlingTest { | |
private Game _game; | |
@Before | |
public void before() { | |
_game = new Game(); | |
} | |
@Test | |
public void a_gutter_game_should_score_0() { | |
rollMany(_game, 20, 0); | |
assertEquals(0, _game.score()); | |
} | |
@Test | |
public void a_game_with_one_ninepin_down_should_score_1() { | |
_game.roll(1); | |
rollMany(_game, 19, 0); | |
assertEquals(1, _game.score()); | |
} | |
@Test | |
public void a_spare_should_double_the_next_roll() { | |
_game.roll(5); | |
_game.roll(5); | |
_game.roll(4); | |
rollMany(_game, 17, 0); | |
assertEquals(18, _game.score()); | |
} | |
@Test | |
public void a_strike_should_double_the_2_next_rolls() { | |
_game.roll(10); | |
_game.roll(2); | |
_game.roll(3); | |
rollMany(_game, 16, 0); | |
assertEquals(20, _game.score()); | |
} | |
@Test | |
public void a_strike_is_not_a_spare() { | |
_game.roll(10); | |
_game.roll(0); | |
_game.roll(4); | |
rollMany(_game, 16, 0); | |
assertEquals(18, _game.score()); | |
} | |
@Test | |
public void a_spare_should_not_span_multiple_frames() { | |
_game.roll(0); | |
_game.roll(5); | |
_game.roll(5); | |
_game.roll(2); | |
rollMany(_game, 16, 0); | |
assertEquals(12, _game.score()); | |
} | |
@Test | |
public void a_game_with_only_ones_should_score_20() { | |
rollMany(_game, 20, 1); | |
assertEquals(20, _game.score()); | |
} | |
@Test | |
public void a_second_roll_10_should_not_be_a_strike() { | |
_game.roll(0); | |
_game.roll(10); | |
_game.roll(5); | |
_game.roll(2); | |
rollMany(_game, 16, 0); | |
assertEquals(22, _game.score()); | |
} | |
@Test | |
public void a_perfect_game_should_score_300() | |
{ | |
rollMany(_game, 12, 10); | |
assertEquals(300, _game.score()); | |
} | |
private void rollMany(Game game, int count, int score) { | |
for(int i = 0; i < count; i++) { | |
game.roll(score); | |
} | |
} | |
} |
This file contains hidden or 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
package main; | |
import java.util.ArrayList; | |
public class Game { | |
private ArrayList<Integer> _rolls = new ArrayList<Integer>(); | |
public void roll(int score) { | |
_rolls.add(score); | |
} | |
public int score() { | |
int score = 0; | |
int frameIndex = 0; | |
for(int frame = 0; frame < 10; frame++) { | |
if (isStrike(frameIndex)) { | |
score += 10 + strikeBonus(frameIndex); | |
frameIndex++; | |
} | |
else if (isSpare(frameIndex)) { | |
score += 10 + spareBonus(frameIndex); | |
frameIndex += 2; | |
} | |
else { | |
score += frameScore(frameIndex); | |
frameIndex += 2; | |
} | |
} | |
return score; | |
} | |
private boolean isStrike(int frameIndex) { | |
return _rolls.get(frameIndex) == 10; | |
} | |
private boolean isSpare(int frameIndex) { | |
return _rolls.get(frameIndex) + _rolls.get(frameIndex+1) == 10; | |
} | |
private int strikeBonus(int frameIndex) { | |
return _rolls.get(frameIndex+1) + _rolls.get(frameIndex+2); | |
} | |
private int spareBonus(int frameIndex) { | |
return _rolls.get(frameIndex+2); | |
} | |
private int frameScore(int frameIndex) { | |
return _rolls.get(frameIndex) + _rolls.get(frameIndex+1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment