forked from RasPat1/practice-codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBowling.java
63 lines (53 loc) · 1.39 KB
/
Bowling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.*;
public class Bowling {
public static int bowling_score(String frames) {
String[] framesArr = frames.split(" ");
List<Character> balls = new ArrayList<>();
List<Integer> ballValue = new ArrayList<>();
int ballsBeforeLastFrame = frames.lastIndexOf(" ") - 9;
for (char c : frames.toCharArray()) {
if (c != ' ') {
balls.add(c);
}
}
int lastBallValue = 0;
for (char c: balls) {
int currentBallValue;
if (c == '/') {
currentBallValue = 10 - lastBallValue;
} else {
currentBallValue = getBallScore(c);
}
ballValue.add(currentBallValue);
lastBallValue = currentBallValue;
}
int sum = 0;
for (int i = 0; i < balls.size(); i++) {
char c = balls.get(i);
int val = ballValue.get(i);
if (c == 'X' || c == '/') {
if (i + 1 < ballValue.size()) {
val += ballValue.get(i+1);
}
}
if (c == 'X') {
if (i + 2 < ballValue.size()) {
val += ballValue.get(i+2);
}
}
sum += val;
// if we're in the last frame ignore the bonus balls
if (i > ballsBeforeLastFrame && (c == 'X' || c == '/')) {
break;
}
}
return sum;
}
public static int getBallScore(char c) {
if (c == 'X' || c == '/') {
return 10;
} else {
return Character.getNumericValue(c);
}
}
}