-
Notifications
You must be signed in to change notification settings - Fork 738
Expand file tree
/
Copy pathLadderGame.java
More file actions
48 lines (39 loc) · 1.3 KB
/
LadderGame.java
File metadata and controls
48 lines (39 loc) · 1.3 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
package nextstep.ladder.domain;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class LadderGame {
private Names names;
private Lines lines;
private final List<LadderResult> results;
public LadderGame(Names names, Lines lines, List<LadderResult> results) {
this.names = names;
this.lines = lines;
validateResults(results);
this.results = results;
}
public LadderResults play() {
Map<Name, LadderResult> gameResults = generateGameResults();
return new LadderResults(gameResults);
}
public Names getNames() {
return names;
}
public Lines getLines() {
return lines;
}
private Map<Name, LadderResult> generateGameResults() {
return IntStream.range(0, names.size())
.boxed()
.collect(Collectors.toMap(names.getNames()::get, this::calculateResult));
}
private LadderResult calculateResult(int index) {
return results.get(lines.move(index));
}
private void validateResults(List<LadderResult> results) {
if (results.size() != names.size()) {
throw new IllegalArgumentException("참가자 수와 결과 수가 일치하지 않습니다.");
}
}
}