-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathGameResult.java
More file actions
45 lines (36 loc) · 1.37 KB
/
GameResult.java
File metadata and controls
45 lines (36 loc) · 1.37 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
package blackjack.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class GameResult {
private static final int SCORE_CRITERIA = 21;
private final List<Participant> participants;
private GameResult(List<Participant> participants) {
this.participants = new ArrayList<>(participants);
calculateGameResult(this.participants);
}
public static GameResult of(Dealer dealer, Players players) {
List<Participant> participants = new ArrayList<>();
participants.add(dealer);
participants.addAll(players.getPlayers());
return new GameResult(participants);
}
private void calculateGameResult(List<Participant> participants) {
int criteria = calculateCriteria(participants);
for(Participant participant: participants){
participant.judgeScore(criteria);
}
}
private int calculateCriteria(List<Participant> participants){
return participants.stream()
.mapToInt(Participant::sumCardScore)
.map(score-> Math.abs(score - SCORE_CRITERIA))
.min()
.orElseThrow(() -> {
throw new IllegalStateException("최대값을 구할 수 없습니다.");
});
}
public List<Participant> getParticipants() {
return Collections.unmodifiableList(participants);
}
}