-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathController.java
More file actions
65 lines (53 loc) · 1.9 KB
/
Controller.java
File metadata and controls
65 lines (53 loc) · 1.9 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
package blackjack.controller;
import blackjack.domain.Game;
import blackjack.domain.Winner;
import blackjack.domain.card.CardDeck;
import blackjack.domain.player.Player;
import blackjack.domain.state.Gameable;
import blackjack.domain.state.State;
import blackjack.view.InputView;
import blackjack.view.OutputView;
public class Controller {
private static void initGame(Game game) {
OutputView.printStartMessage(game);
OutputView.printDealerCard(game.getDealer());
OutputView.printPlayerCard(game.getPlayers());
}
public void run() {
Game game = new Game(InputView.inputPlayers());
initGame(game);
playGame(game);
finishGame(game);
}
private void playGame(Game game) {
game.getPlayers().forEach(this::receive);
if (game.giveCardToDealer()) {
OutputView.printMessageToGiveCardToDealer();
}
}
private void finishGame(Game game) {
Winner winner = new Winner(game);
OutputView.printGameResults(game.getDealer(), game.getPlayers());
OutputView.printGameWinOrLose(game.getDealer(), winner.calculateDealerGameResult());
game.getPlayers().forEach(
player -> OutputView.printGameWinOrLose(
player, winner.calculatePlayerGameResult(player)
)
);
}
public void receive(Player player) {
Gameable gameable = player.getCards();
String yesOrNo = "";
do {
yesOrNo = InputView.inputYesOrNo(player.getName());
if (yesOrNo.equals("y")) {
gameable.addCard(CardDeck.pop());
OutputView.printCurrentCardsState(player.getName(), player.getCards());
gameable = gameable.judge();
}
if (yesOrNo.equals("n")) {
gameable = new State(gameable.cards(), false);
}
} while (gameable.isEnd());
}
}