forked from willmurnane/splits-happen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCalculate.java
61 lines (54 loc) · 1.96 KB
/
CCalculate.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
package splitshappen;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author peter
*/
public class CCalculate {
final int STRIKE=10;
final int SPARE=11;
final int MISS=0;
public int calculateScore(String sequence){
int score=0;
int sequenceLength=sequence.length();
// Make integer array
int[] integerArray=new int[sequenceLength];
for (int i=0; i<sequenceLength; ++i){
switch(sequence.charAt(i)){
case 'X':
case 'x': integerArray[i]=STRIKE;
break;
case '/': integerArray[i]=SPARE;
break;
case '-': integerArray[i]=MISS;
break;
default: integerArray[i]=Character.getNumericValue(sequence.charAt(i));
}
}
// Account for strike or spare in last frame
if (integerArray[sequenceLength-3]==STRIKE){
int end=sequenceLength-3;
int previousStrikeCount=0;
for (int i=0; i<end; ++i) if (integerArray[i]==STRIKE) ++previousStrikeCount;
int frame=(int)((end+previousStrikeCount)/2)+1;
if (frame==10) sequenceLength-=2;
}
else if (integerArray[sequenceLength-2]==SPARE) sequenceLength-=1;
// Determine score from integer array
for (int i=0; i<sequenceLength; ++i){
if (integerArray[i]==SPARE){
score+=10+integerArray[i+1]-integerArray[i-1];
}
else score+=integerArray[i];
if (integerArray[i]==STRIKE){
score+=(integerArray[i+2]==SPARE)? 10 :
integerArray[i+1]+integerArray[i+2];
}
}
return score;
}
}