forked from woowacourse/java-blackjack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameController.java
More file actions
119 lines (98 loc) · 3.33 KB
/
Copy pathGameController.java
File metadata and controls
119 lines (98 loc) · 3.33 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package controller;
import domain.deck.Deck;
import domain.participant.Dealer;
import domain.participant.Participant;
import domain.participant.Player;
import domain.result.ResultJudge;
import ui.InputView;
import ui.ResultView;
import java.util.ArrayList;
import java.util.List;
public class GameController {
private final ResultView resultView;
private final InputView inputView;
public GameController(ResultView resultView, InputView inputView){
this.resultView = resultView;
this.inputView = inputView;
}
public void run(){
Deck deck = new Deck();
ResultJudge judge = new ResultJudge();
// 참가자 입력
List<Participant> participants = joinParticipant();
// 초기 패 설정
initialAllPlayersHand(participants, deck);
// 플레이어마다 Hit/Stand 여부 확인
askHitOrStand(participants, deck);
// 참가자 패와 점수 공개
resultView.printScores(participants);
// 참가자 승/패 공개
resultView.printGameResult(participants, judge);
}
/***
* 1. 모든 플레이어들에게 Hit/Stand 여부를 묻기 위한 함수 실행
* 2. 딜러 draw 여부 확인 (16점 이하인지)
*/
private void askHitOrStand(List<Participant> participants, Deck deck){
Participant dealer = participants.getFirst();
for(Participant player : participants.subList(1, participants.size())){
askPlayerHit(player, deck);
}
while(dealer.canReceiveCard()){
dealer.receive(deck.draw());
resultView.printDealerDraw();
}
}
/***
* 플레이어에게 Hit/Stand 여부 확인
*/
private void askPlayerHit(Participant player, Deck deck){
while(inputView.askHit(player.getName())){
canHit(player, deck);
}
}
/***
* Hit 선택시 실제로 플레이어가 Bust가 아닌지 확인 후 카드 지급
*/
private void canHit(Participant player, Deck deck){
if(player.canReceiveCard()){
player.receive(deck.draw());
resultView.printHand(player);
return;
}
resultView.printIsBust();
}
/***
* 딜러와 입력 받은 플레이어 추가
*/
private List<Participant> joinParticipant(){
Participant dealer = new Dealer("딜러");
List<String> playerNames;
List<Participant> participants = new ArrayList<>();
participants.add(dealer);
playerNames = inputView.inputPlayers();
for(String playerName : playerNames){
Participant player = new Player(playerName);
participants.add(player);
}
return participants;
}
/***
* 모든 참가자에게 2장의 카드를 분배하기 위한 함수 호출 및 패 공개
*/
private void initialAllPlayersHand(List<Participant> participants, Deck deck){
for(Participant participant : participants){
initialHand(participant, deck);
}
resultView.printInitialDeal(participants);
resultView.printAllHands(participants);
}
/**
* 카드를 2장씩 분배
*/
private void initialHand(Participant participant, Deck deck){
for(int i = 0; i < 2; i++){
participant.receive(deck.draw());
}
}
}