-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathWinner.java
More file actions
57 lines (47 loc) · 1.75 KB
/
Winner.java
File metadata and controls
57 lines (47 loc) · 1.75 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
package blackjack.domain;
import blackjack.domain.player.Player;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Winner {
private final int BLACKJACK = 21;
private final Game game;
public Winner(final Game game) {
this.game = game;
}
public List<Integer> calculateDealerGameResult() {
int dealerScore = game.getDealer().getCards().cards().sumScore();
return calculateGameResult(dealerScore);
}
public List<Integer> calculatePlayerGameResult(final Player player) {
int playerScore = player.getCards().cards().sumScore();
return calculateGameResult(playerScore);
}
private List<Integer> calculateGameResult(final int sourceScore) {
List<Integer> targetScores = game.getScoresOfPlayers().stream()
.map(this::convertZeroScore)
.collect(Collectors.toList());
return Arrays.asList(countWin(sourceScore, targetScores),
countLose(sourceScore, targetScores));
}
private int convertZeroScore(final int score) {
if (score > BLACKJACK) {
return 0;
}
return score;
}
private int countWin(final int sourceScore, final List<Integer> targetScores) {
if (sourceScore > BLACKJACK) {
return 0;
}
return Long.valueOf(targetScores.stream()
.filter(targetScore -> sourceScore > targetScore).count()).intValue();
}
private int countLose(final int sourceScore, final List<Integer> targetScores) {
if (sourceScore > BLACKJACK) {
return targetScores.size();
}
return Long.valueOf(targetScores.stream()
.filter(targetScore -> sourceScore < targetScore).count()).intValue();
}
}