forked from willmurnane/splits-happen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrame.java
92 lines (73 loc) · 2.21 KB
/
Frame.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main.java;
/**
* Created by collinm on 6/6/17.
*/
public class Frame {
// Each array position represents one ball down the lane
private int[] balls;
private int rawTotal; // The sum of the instantiated frame only, strikes and spares return 10
private int numPins;
public Frame(int[] balls, int numPins) {
this.balls = balls;
this.numPins = numPins;
this.calcTotal();
}
// String input indicates parse requirement
public Frame(String parseMe, int numPins) {
// Parse logic separated in case of future changes to this type of constructor
this.numPins = numPins;
this.parse(parseMe);
this.calcTotal();
}
private void parse(String parseMe) {
int[] result = new int[parseMe.length()];
for (int i = 0; i < parseMe.length(); i++) {
if (parseMe.charAt(i) == '-') {
// Check miss
result[i] = 0;
} else if (parseMe.charAt(i) == 'X') {
// Check strike
result[i] = this.numPins;
} else if (parseMe.charAt(i) == '/') {
// Check spare
result[i] = this.numPins - result[i-1]; // Out of scope #1 assumption
} else {
String ballString = Character.toString(parseMe.charAt(i)); // Out of scope #1 assumption
result[i] = Integer.parseInt(ballString);
}
}
this.balls = result;
}
private void calcTotal() {
int total = 0;
for (int i: this.balls) {
total += i;
}
this.rawTotal = total;
}
public String toString() {
String result = "[ ";
for (int i: this.getBalls()) {
result += i + " ";
}
return result + "]";
}
public int[] getBalls() {
return balls;
}
public void setBalls(int[] balls) {
this.balls = balls;
}
public int getRawTotal() {
return rawTotal;
}
public void setRawTotal(int rawTotal) {
this.rawTotal = rawTotal;
}
public int getNumPins() {
return numPins;
}
public void setNumPins(int numPins) {
this.numPins = numPins;
}
}