Created
November 18, 2017 15:35
-
-
Save tolbier/1b7cf83efc2d53d00b17ba1d53f5bde7 to your computer and use it in GitHub Desktop.
My BowlingGame Kata (still no OOP, but with some improves)
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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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