-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathBlackJack.java
More file actions
92 lines (79 loc) · 2.78 KB
/
BlackJack.java
File metadata and controls
92 lines (79 loc) · 2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package blackjack.controller;
import blackjack.domain.card.CardFactory;
import blackjack.domain.card.Deck;
import blackjack.domain.report.GameReports;
import blackjack.domain.request.DrawRequest;
import blackjack.domain.request.UserNamesRequest;
import blackjack.domain.user.Dealer;
import blackjack.domain.user.Player;
import blackjack.domain.user.Players;
import blackjack.view.InputView;
import blackjack.view.OutputView;
import java.util.List;
import java.util.stream.Collectors;
public class BlackJack {
private final Players players;
private final Deck deck;
private BlackJack(Players players, Deck deck) {
this.players = players;
this.deck = deck;
}
public static BlackJack init(UserNamesRequest playerNames) {
Dealer dealer = new Dealer();
Players candidates = playerNames.userNames()
.stream()
.map(Player::new)
.collect(Collectors.collectingAndThen(Collectors.toList(),
players -> new Players(dealer, players)));
CardFactory cardFactory = CardFactory.getInstance();
Deck deck = new Deck(cardFactory.createCards());
return new BlackJack(candidates, deck);
}
public void runGame() {
spreadStartCards();
if (!players.hasBlackJack()) {
drawPlayers();
drawDealer();
}
GameReports reports = getResult();
showResult(reports);
}
private void spreadStartCards() {
players.drawStartCards(deck);
OutputView.printAllPlayersCard(players);
}
private void drawPlayers() {
for (Player player : players.findOnlyPlayers()) {
drawEachPlayer(player);
}
}
private void drawEachPlayer(Player player) {
while (player.isDrawable() && getPlayerRequest(player)) {
player.drawCard(deck.spreadCard());
OutputView.printEachCardInfo(player);
}
}
private boolean getPlayerRequest(Player player) {
String drawRequest = InputView.getDrawRequest(player);
return DrawRequest.valueOf(drawRequest)
.isDrawable();
}
private void drawDealer() {
Dealer dealer = players.findDealer();
while (dealer.isDrawable()) {
dealer.drawCard(deck.spreadCard());
OutputView.printDealerGetCard();
}
}
private GameReports getResult() {
Dealer dealer = players.findDealer();
List<Player> candidates = players.findOnlyPlayers();
return candidates.stream()
.map(dealer::createReport)
.collect(Collectors.collectingAndThen(Collectors.toList(), GameReports::new));
}
private void showResult(GameReports reports) {
OutputView.printResultStatus(players.all());
OutputView.printReports(reports);
}
}