Skip to content

Instantly share code, notes, and snippets.

@tolbier
Created November 18, 2017 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolbier/1b7cf83efc2d53d00b17ba1d53f5bde7 to your computer and use it in GitHub Desktop.
Save tolbier/1b7cf83efc2d53d00b17ba1d53f5bde7 to your computer and use it in GitHub Desktop.
My BowlingGame Kata (still no OOP, but with some improves)
package com.tolbier.bowlinggamekata;
public class Game {
private int currentRoll = 0;
private int rolls[] = new int[21];
int rollIndex ;
public void roll(int pins) {
rolls[currentRoll++] = pins;
}
public int score() {
int score = 0;
rollIndex = 0;
for (int frame = 0; frame < 10; frame++) {
int frameScore = getFrameScore();
score += frameScore;
}
return score;
}
private int getFrameScore() {
int frameScore = 10;
if (isStrike()) {
frameScore += strikeBonus();
} else {
if (isSpare()) {
frameScore += spareBonus() ;
}else {
frameScore = sumOfBallsInFrame();
}
rollIndex++;
}
rollIndex++;
return frameScore;
}
private int sumOfBallsInFrame() {
return rolls[rollIndex] + rolls[rollIndex + 1];
}
private int strikeBonus() {
return rolls[rollIndex + 1] + rolls[rollIndex + 2];
}
private int spareBonus() {
return rolls[rollIndex + 2];
}
private boolean isSpare() {
return rolls[rollIndex] + rolls[rollIndex + 1] == 10;
}
private boolean isStrike() {
return rolls[rollIndex] == 10;
}
}
@tolbier
Copy link
Author

tolbier commented Nov 18, 2017

with a Java "structured programming" schema, and with respect and devotion for "UncleBob" Original Kata, but in favour of cleaning, I think it could be improved as it is here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment