Första committen
This commit is contained in:
72
BowlingKata/src/session2/BowlingScoreCalculator.java
Normal file
72
BowlingKata/src/session2/BowlingScoreCalculator.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package session2;
|
||||
|
||||
public class BowlingScoreCalculator {
|
||||
|
||||
private int countFrames = 0;
|
||||
private boolean firstRoll = true;
|
||||
private int sum = 0;
|
||||
|
||||
public int calculateScore(String rolls) {
|
||||
for (int rollIndex = 0; rollIndex < rolls.length() && countFrames < 10; rollIndex++) {
|
||||
char roll = rolls.charAt(rollIndex);
|
||||
if (rollIsStrike(roll)) {
|
||||
handleStrike(rolls, rollIndex);
|
||||
} else if (rollIsSpare(roll)) {
|
||||
handleSpare(rolls, rollIndex);
|
||||
} else {
|
||||
handleRegularRoll(rolls, rollIndex);
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
private void handleRegularRoll(String rolls, int i) {
|
||||
sum += getValueForRolls(rolls, i);
|
||||
if (firstRoll == true) {
|
||||
firstRoll = false;
|
||||
} else {
|
||||
countFrames++;
|
||||
firstRoll = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleSpare(String rolls, int i) {
|
||||
if (rolls.charAt(i - 1) != '-') {
|
||||
sum -= getValueForRolls(rolls, i - 1);
|
||||
}
|
||||
sum += 10;
|
||||
if (rolls.charAt(i + 1) != '-') {
|
||||
sum += getValueForRolls(rolls, i + 1);
|
||||
}
|
||||
countFrames++;
|
||||
firstRoll = true;
|
||||
}
|
||||
|
||||
private boolean rollIsSpare(char roll) {
|
||||
return roll == '/';
|
||||
}
|
||||
|
||||
private boolean rollIsStrike(char roll) {
|
||||
return roll == 'X';
|
||||
}
|
||||
|
||||
private void handleStrike(String rolls, int i) {
|
||||
sum += 10;
|
||||
sum += getValueForRolls(rolls, i + 1);
|
||||
sum += getValueForRolls(rolls, i + 2);
|
||||
countFrames++;
|
||||
firstRoll = true;
|
||||
}
|
||||
|
||||
private int getValueForRolls(String rolls, int rollIndex) {
|
||||
char roll = rolls.charAt(rollIndex);
|
||||
if (rollIsStrike(roll)) {
|
||||
return 10;
|
||||
} else if (roll == '-') {
|
||||
return 0;
|
||||
} else {
|
||||
return Integer.parseInt(roll + "");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
108
BowlingKata/test/session2/BowlingScoreCalculatorTest.java
Normal file
108
BowlingKata/test/session2/BowlingScoreCalculatorTest.java
Normal file
@@ -0,0 +1,108 @@
|
||||
package session2;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BowlingScoreCalculatorTest {
|
||||
|
||||
// FIXME 10 frames with 9 and a miss = 90
|
||||
// FIXME 10 frames with 5 and spare, and a bonus roll of 5 = 150
|
||||
|
||||
// FIXME 5 with spare, strike, rest miss = 30
|
||||
// FIXME 2 strikes then rest miss = 30
|
||||
// FIXME Last roll is strike
|
||||
|
||||
private BowlingScoreCalculator scoreCalculator = new BowlingScoreCalculator();
|
||||
|
||||
@Test
|
||||
public void test_10_frames_of_misses_equals_0() throws Exception {
|
||||
String rolls = "--------------------";
|
||||
|
||||
int result = scoreCalculator.calculateScore(rolls);
|
||||
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void one_pin_down_rest_is_miss_equals_1() throws Exception {
|
||||
String rolls = "1-------------------";
|
||||
|
||||
int result = scoreCalculator.calculateScore(rolls);
|
||||
|
||||
assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void one_strike_rest_is_miss_equals_10() throws Exception {
|
||||
String rolls = "X-----------------";
|
||||
|
||||
int result = scoreCalculator.calculateScore(rolls);
|
||||
|
||||
assertEquals(10, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void one_spare_rest_is_miss_equals_10() throws Exception {
|
||||
String rolls = "-/------------------";
|
||||
|
||||
int result = scoreCalculator.calculateScore(rolls);
|
||||
|
||||
assertEquals(10, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void one_spare_with_privious_number_rest_is_miss_equals_10() throws Exception {
|
||||
String rolls = "5/------------------";
|
||||
|
||||
int result = scoreCalculator.calculateScore(rolls);
|
||||
|
||||
assertEquals(10, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void twelve_strikes_two_bonus_equals_300() throws Exception {
|
||||
String rolls = "XXXXXXXXXXXX";
|
||||
|
||||
int result = scoreCalculator.calculateScore(rolls);
|
||||
|
||||
assertEquals(300, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_one_spare_five_rest_miss_equals_20() throws Exception {
|
||||
String rolls = "-/5-----------------";
|
||||
|
||||
int result = scoreCalculator.calculateScore(rolls);
|
||||
|
||||
assertEquals(20, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void five_with_spare_then_strike_rest_miss_equals_30() throws Exception {
|
||||
String rolls = "5/X----------------";
|
||||
|
||||
int result = scoreCalculator.calculateScore(rolls);
|
||||
|
||||
assertEquals(30, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void five_with_spare_then_strike_the_five_rest_miss_equals_40() throws Exception {
|
||||
String rolls = "5/X5---------------";
|
||||
|
||||
int result = scoreCalculator.calculateScore(rolls);
|
||||
|
||||
assertEquals(40, result);
|
||||
}
|
||||
|
||||
// FIXME Last roll is spare
|
||||
@Test
|
||||
public void all_miss_last_roll_is_spare_equals_14() throws Exception {
|
||||
String rolls = "------------------5/4";
|
||||
|
||||
int result = scoreCalculator.calculateScore(rolls);
|
||||
|
||||
assertEquals(14, result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user