Created
May 29, 2014 18:45
-
-
Save jirkapenzes/80f6788e0c61b7ac6d4f to your computer and use it in GitHub Desktop.
CodingDojo #12
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 java.util.ArrayList; | |
import java.util.List; | |
public class Bowling { | |
private List<Round> rounds; | |
public Bowling() { | |
rounds = new ArrayList<>(); | |
} | |
public void addRound(Round newRound) { | |
if (rounds.size() > 0) { | |
Round lastRound = rounds.get(rounds.size() - 1); | |
lastRound.setNextRound(newRound); | |
} | |
rounds.add(newRound); | |
} | |
public int getScore(long roundNumber) { | |
int score = 0; | |
for (int index = 0; index < roundNumber; index++) { | |
score += rounds.get(index).getScore(); | |
} | |
return score; | |
} | |
public int getScore() { | |
return getScore(rounds.size()); | |
} | |
} |
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 junitparams.JUnitParamsRunner; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
@RunWith(JUnitParamsRunner.class) | |
public class CodingDojo { | |
private Bowling getGameWithRound(int roll1, int roll2) { | |
Bowling bowling = new Bowling(); | |
Round round = new Round(roll1, roll2); | |
bowling.addRound(round); | |
return bowling; | |
} | |
@Test | |
public void should_roll_zero_pins_in_round_then_score_is_zero() { | |
Bowling game = getGameWithRound(0, 0); | |
Assert.assertEquals(0, game.getScore()); | |
} | |
@Test | |
public void should_roll_one_pins_in_round_then_score_is_one() { | |
Bowling game = getGameWithRound(0, 1); | |
Assert.assertEquals(1, game.getScore()); | |
} | |
@Test | |
public void when_spare_then_next_roll_is_added_to_round_score() { | |
Bowling game = getGameWithRound(9, 1); | |
// TODO assert score at firt round is not finished? | |
game.addRound(new Round(1, 1)); | |
Assert.assertEquals(13, game.getScore()); | |
} | |
@Test | |
public void when_strike_then_next_roll_is_added_to_round_score() { | |
Bowling game = getGameWithRound(10, 0); | |
game.addRound(new Round(1, 1)); | |
Assert.assertEquals(14, game.getScore()); | |
} | |
@Test | |
public void when_two_strikes_then_two_next_rolls_are_added_to_round_score() { | |
Bowling game = getGameWithRound(10, 0); | |
game.addRound(new Round(10, 0)); | |
game.addRound(new Round(1, 1)); | |
// Assert.assertEquals([21, 33, 35], game.getScores()); | |
Assert.assertEquals(35, game.getScore()); | |
} | |
@Test | |
public void when_two_strikes_then_two_next_rolls_are_added_to_round_score1() { | |
Bowling game = getGameWithRound(10, 0); | |
Assert.assertEquals(0, game.getScore(1)); | |
game.addRound(new Round(10, 0)); | |
Assert.assertEquals(0, game.getScore(2)); | |
game.addRound(new Round(1, 1)); | |
Assert.assertEquals(35, game.getScore(3)); | |
game.addRound(new Round(5, 5)); | |
Assert.assertEquals(35, game.getScore(4)); | |
game.addRound(new Round(2, 2)); | |
Assert.assertEquals(51, game.getScore()); | |
} | |
@Test | |
public void when_two_strikes_then_score_not_calculate() { | |
Bowling game = getGameWithRound(10, 0); | |
game.addRound(new Round(10, 0)); | |
// Assert.assertEquals([21, 33, 35], game.getScores()); | |
Assert.assertEquals(0, game.getScore()); | |
} | |
} |
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 Round { | |
private final int roll1; | |
private final int roll2; | |
private int score; | |
private boolean isSpare; | |
private boolean isStrike; | |
private boolean scoreFinished; | |
private Round nextRound; | |
private boolean finishedRound; | |
public Round(int roll1, int roll2) { | |
this.roll1 = roll1; | |
this.roll2 = roll2; | |
this.isSpare = (roll1 + roll2 == 10) && (roll1 != 10); | |
this.isStrike = (roll1 == 10); | |
} | |
public int getScore() { | |
calculateScore(); | |
return score; | |
} | |
public boolean isSpare() { | |
return isSpare; | |
} | |
public boolean isStrike() { | |
return isStrike; | |
} | |
public void setNextRound(Round nextRound) { | |
this.nextRound = nextRound; | |
} | |
public Round getNextRound() { | |
return nextRound; | |
} | |
private void calculateScore() { | |
score = sumOfRolls(); | |
if (!isFinishedRound()) { | |
score = 0; | |
return; | |
} | |
if (isStrike()) { | |
score += getNextRound().sumOfRolls(); | |
if (getNextRound().isStrike()) { | |
score += getNextRound().getNextRound().roll1; | |
} | |
} | |
if (isSpare()) { | |
score += getNextRound().roll1; | |
} | |
} | |
private int sumOfRolls() { | |
return roll1 + roll2; | |
} | |
public boolean isFinishedRound() { | |
if (!isStrike() && ! isSpare()) | |
return true; | |
if (getNextRound() != null && !getNextRound().isStrike()) { | |
return true; | |
} | |
if (getNextRound() != null) { | |
if (getNextRound().getNextRound() != null) | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment