Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java version #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions japaz/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.japaz.katas</groupId>
<artifactId>Abril-Bowling</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
</dependencies>
</dependencyManagement>
</project>
101 changes: 101 additions & 0 deletions japaz/src/main/java/com/japaz/katas/ab/BowlingScorer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.japaz.katas.ab;

public class BowlingScorer {
private int currentFrame = 0;
private boolean secondRollInAFrame = false;
private String sequenceOfRolls;
private int score;

public BowlingScorer(String sequenceOfRolls) {
currentFrame = 0;
secondRollInAFrame = false;
score = 0;
this.sequenceOfRolls = sequenceOfRolls;
}

public int calculateScore() {
int currentRoll = 0;

while (currentFrame < 10) {
int rollValue = sequenceOfRolls.charAt(currentRoll);

switch(rollValue) {
case 'X' : score+=10;
processNextRoll(currentRoll);
processTheRollAfterNext(currentRoll);
endFrame();
break;
case '9' :
case '8' :
case '7' :
case '6' :
case '5' :
case '4' :
case '3' :
case '2' :
case '1' :
case '0' : processNumericValue(rollValue);
if (secondRollInAFrame) {
endFrame();
} else {
secondRollInAFrame = true;
}
break;
case '/' : score=score+10-Character.digit(sequenceOfRolls.charAt(currentRoll-1),10);
processNextRoll(currentRoll);
endFrame();
break;
case '-' :
endFrame();
break;
}
currentRoll++;
}
return score;
}

private void processNextRoll(int currentRoll) {
int rollValue = sequenceOfRolls.charAt(currentRoll+1);
processXOrNumeric(rollValue);
}

private void processTheRollAfterNext(int currentRoll) {
int rollValue = sequenceOfRolls.charAt(currentRoll+2);
processXOrNumeric(rollValue);
processSlash(currentRoll, rollValue);
}

private void processXOrNumeric(int rollValue) {
switch (rollValue) {
case 'X' : score+=10;
break;
case '9' :
case '8' :
case '7' :
case '6' :
case '5' :
case '4' :
case '3' :
case '2' :
case '1' :
case '0' : processNumericValue(rollValue);
break;
}
}

private void processNumericValue(int rollValue) {
score+=Character.digit(rollValue, 10);
}

private void processSlash(int currentRoll, int rollValue) {
switch (rollValue) {
case '/' : score=score+10-Character.digit(sequenceOfRolls.charAt(currentRoll+1), 10);
break;
}
}

private void endFrame() {
currentFrame++;
secondRollInAFrame = false;
}
}
64 changes: 64 additions & 0 deletions japaz/src/test/java/com/japaz/katas/ab/TestFrameScorer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.japaz.katas.ab;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

public class TestFrameScorer {
@Test
public void test_12_Strikes_Is_300() {
String sequenceOfRolls = "XXXXXXXXXXXX";
int score = new BowlingScorer(sequenceOfRolls).calculateScore();
Assert.assertEquals("12 Strikes must be 300 points", 300, score);
}

@Test
public void test_10_Pairs_Of_9_And_Miss_Is_90() {
String sequenceOfRolls = "9-9-9-9-9-9-9-9-9-9-";
int score = new BowlingScorer(sequenceOfRolls).calculateScore();
Assert.assertEquals("10 Pairs of 9 and miss must be 90", 90, score);
}

@Test
public void test_10_Pairs_Of_5_And_Spare_With_Final_5_Is_150() {
String sequenceOfRolls = "5/5/5/5/5/5/5/5/5/5/5";
int score = new BowlingScorer(sequenceOfRolls).calculateScore();
Assert.assertEquals("10 Pairs of 5 and spare, with a final 5 must be 150", 150, score);
}

@Test
public void test_Strikes_Must_Count_Next_Two_Shoots_Double() {
String sequenceOfRolls = "X535-5-5-5-5-5-5-5-";
int score = new BowlingScorer(sequenceOfRolls).calculateScore();
Assert.assertEquals(66, score);
}

@Test
public void test_Strikes_Must_Count_Next_Two_Shoots_Double_Even_A_Miss() {
String sequenceOfRolls = "X5-5-5-5-5-5-5-5-5-";
int score = new BowlingScorer(sequenceOfRolls).calculateScore();
Assert.assertEquals(60, score);
}

@Test
public void test_Strikes_Must_Count_Next_Two_Shoots_Double_Even_An_Spare() {
String sequenceOfRolls = "X5/5-5-5-5-5-5-5-5-";
int score = new BowlingScorer(sequenceOfRolls).calculateScore();
Assert.assertEquals(75, score);
}

@Test
public void test_Strikes_Must_Count_Next_Two_Shoots_Double_Even_A_Miss_At_The_Last() {
String sequenceOfRolls = "1-1-1-1-1-1-1-1-1-X5-";
int score = new BowlingScorer(sequenceOfRolls).calculateScore();
Assert.assertEquals(24, score);
}

@Test
public void test_Strikes_Must_Count_Next_Two_Shoots_Double_Even_An_Spare_At_The_Last() {
String sequenceOfRolls = "1-1-1-1-1-1-1-1-1-X5/";
int score = new BowlingScorer(sequenceOfRolls).calculateScore();
Assert.assertEquals(29, score);
}
}