-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathLadderResult.java
More file actions
56 lines (46 loc) · 1.78 KB
/
Copy pathLadderResult.java
File metadata and controls
56 lines (46 loc) · 1.78 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
package model;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LadderResult {
private static final String NO_RESULT = "결과 없음";
private final Map<String, String> results;
private final Ladder ladder;
public LadderResult(Ladder ladder) {
this.ladder = ladder;
this.results = new HashMap<>();
}
public void calculateResults(List<String> playerNames, Prizes prizes) {
List<Line> lines = ladder.getLines();
List<String> prizeValues = prizes.getPrize();
for (String playerName : playerNames) {
int playerIndex = playerNames.indexOf(playerName);
for (Line line : lines) {
playerIndex = getNewIndexMove(playerIndex, line.getPointGroups());
}
results.put(playerName, prizeValues.get(playerIndex));
}
}
private int getNewIndexMove(int currentIndex, List<Point> points) {
if (canMoveLeft(currentIndex, points)) {
return currentIndex - 1;
}
if (canMoveRight(currentIndex, points)) {
return currentIndex + 1;
}
return currentIndex;
}
private boolean canMoveLeft(int currentIndex, List<Point> points) {
return currentIndex > 0 && points.get(currentIndex - 1) == Point.HAS_POINT;
}
private boolean canMoveRight(int currentIndex, List<Point> points) {
return currentIndex < points.size() && points.get(currentIndex) == Point.HAS_POINT;
}
public String getResultForPlayer(String name) {
return results.getOrDefault(name, NO_RESULT);
}
public Map<String, String> getValue() {
return Collections.unmodifiableMap(results);
}
}