-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathJudgement.java
More file actions
63 lines (52 loc) · 1.92 KB
/
Judgement.java
File metadata and controls
63 lines (52 loc) · 1.92 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
package blackjack.domain;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class Judgement {
private static final String DEALER = "딜러";
private static final int THRESHOLD = 21;
private static final String WIN = "승";
private static final String LOSE = "패";
private Map<String, Integer> playerScores;
private Map<String, String> playerResults;
public Judgement(List<Player> players) {
this.playerScores = new LinkedHashMap<>();
this.playerResults = new LinkedHashMap<>();
updatePlayersMap(players);
}
private void updatePlayersMap(List<Player> players) {
players.forEach(player -> {
playerScores.put(player.getName(), player.calculateScore());
});
}
public Map<String, String> findWinners() {
int dealerScore = playerScores.get(DEALER);
playerScores.remove(DEALER);
playerResults.put(DEALER, "");
playerScores.forEach((name, score) -> {
playerResults.put(name, checkWinOrLose(dealerScore, score));
});
playerResults.put(DEALER, getDealerResult());
return Collections.synchronizedMap(new LinkedHashMap<>(playerResults));
}
private String checkWinOrLose(int dealerScore, Integer score) {
if (score > THRESHOLD) {
return LOSE;
}
if (dealerScore > THRESHOLD || dealerScore <= score) { // 동점이면 플레이어가 이기게 설정
return WIN;
}
return LOSE;
}
private String getDealerResult() {
long winCount = getCount(LOSE);
long loseCount = getCount(WIN);
return winCount + WIN + " " + loseCount + LOSE;
}
private long getCount(String indicator) {
return playerResults.values().stream()
.filter(result -> Objects.equals(result, indicator)).count();
}
}