Line | |
---|
1 | package dojo.java; |
---|
2 | |
---|
3 | /** |
---|
4 | * @author Norbert Wigbels (norbert@wigbels.de) |
---|
5 | */ |
---|
6 | public class BowlingGame { |
---|
7 | |
---|
8 | private int[] rolls = new int[20 + 1]; |
---|
9 | private int currentRoll = 0; |
---|
10 | |
---|
11 | |
---|
12 | public int calcScore() { |
---|
13 | int score = 0; |
---|
14 | int subFrame = 0; |
---|
15 | |
---|
16 | for (int frame = 1; frame <= 10; frame++) { |
---|
17 | if (rolls[subFrame] == 10) { // Frame with Strike |
---|
18 | score += 10 + rolls[subFrame + 1] + rolls[subFrame + 2]; |
---|
19 | subFrame++; |
---|
20 | } else if (rolls[subFrame] + rolls[subFrame + 1] == 10) { // Frame with Spare |
---|
21 | score += 10 + rolls[subFrame + 2]; |
---|
22 | subFrame += 2; |
---|
23 | } else { // normal Frame |
---|
24 | score += rolls[subFrame] + rolls[subFrame + 1]; |
---|
25 | subFrame += 2; |
---|
26 | } |
---|
27 | } |
---|
28 | |
---|
29 | return score; |
---|
30 | } |
---|
31 | |
---|
32 | public void roll(int pinsDown) { |
---|
33 | rolls[currentRoll] = pinsDown; |
---|
34 | currentRoll++; |
---|
35 | } |
---|
36 | |
---|
37 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.