-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFrame.java
More file actions
123 lines (93 loc) Β· 3.09 KB
/
Frame.java
File metadata and controls
123 lines (93 loc) Β· 3.09 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package domain;
import java.util.Objects;
import static domain.Frame.FrameStatus.*;
public class Frame implements Scorable {
public final static int DEFAULT_BOWLING_PIN = 10;
public final static Frame NONE_FRAME = new Frame();
private FallingPin first = FallingPin.NONE;
private FallingPin second = FallingPin.NONE;
private Frame nextFrame = NONE_FRAME;
public void fall(FallingPin pins) throws IllegalAccessException {
if (first.equals(FallingPin.NONE)) {
this.first = pins;
return;
}
this.second = pins;
if (pinCount() > DEFAULT_BOWLING_PIN) {
throw new IllegalAccessException();
}
}
private int pinCount() {
return first.value() + second.value();
}
public void setNextFrame(Frame nextFrame) {
this.nextFrame = nextFrame;
}
public boolean isEnd() {
if (FrameStatus.of(this).equals(STRIKE)) {
return true;
}
return !second.equals(FallingPin.NONE);
}
@Override
public Score getScore() { // λ€λ¬μ μ¬μ§ μμ
if (Objects.isNull(nextFrame) || !isEnd()) {
return Score.NOT_DETERMINED;
}
Score nextFrameScore = Score.of(0);
if (FrameStatus.of(this).equals(STRIKE)) {
if (!nextFrame.isEnd()) {
return Score.NOT_DETERMINED;
}
nextFrameScore = nextFrame.getFallingPinCount();
}
if (FrameStatus.of(this).equals(SPARE)) {
if (nextFrame.isEndFirstTry()) {
return Score.NOT_DETERMINED;
}
nextFrameScore = nextFrame.getFallingPinCountAtFirstTry();
}
return getFallingPinCount().add(nextFrameScore);
}
private boolean isEndFirstTry() {
return first.equals(FallingPin.NONE);
}
private Score getFallingPinCountAtFirstTry() {
return Score.of(first.value());
}
private Score getFallingPinCount() {
return Score.of(first.value() + second.value());
}
@Override
public String toString() {
switch (FrameStatus.of(this)) {
case STRIKE:
return " " + STRIKE.symbol + " ";
case SPARE:
return " " + first.getSymbol() + "|" + SPARE.symbol + " ";
default:
return " " + first.getSymbol() + "|" + second.getSymbol() + " ";
}
}
public enum FrameStatus {
STRIKE("X"), SPARE("/"), MISS("-"), HIT(""), NONE(" ");
String symbol;
FrameStatus(String symbol) {
this.symbol = symbol;
}
public static FrameStatus of(Frame frame) {
FallingPin first = frame.first;
if (first.equals(FallingPin.NONE)) {
return NONE;
}
if (first.value() == DEFAULT_BOWLING_PIN) {
return STRIKE;
}
FallingPin second = frame.second;
if (first.value() + second.value() == DEFAULT_BOWLING_PIN) {
return SPARE;
}
return HIT;
}
}
}