-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Lev1][미션3] 블랙잭 1단계 - 다즐 #5
base: woo-chang
Are you sure you want to change the base?
Changes from all commits
93adb74
4d669bd
ab04caa
76e478d
c5575b7
aeb41cc
660568b
5529812
f42b755
59212a2
365c189
14cb9da
d668b8c
07e5c72
36dacd5
fe3be1e
bf4991f
ed70701
4c494a0
43b6ec5
35fd4fc
d3a6af1
58f1fae
81f907a
96833fd
5138dc4
4e58c1d
e3ae0a0
4cdb38c
2370e13
7603a6b
d4b9c22
f4e414c
559693d
6976a0d
944cc85
dca7962
ec75c79
d27fd5c
9b81e50
6ad67e2
100a9cb
9511cc9
4e929ad
a7f6e85
c7b4459
7de79aa
dfa8280
f3808af
a0c0aa3
8fe6bf6
e0c208d
340f048
53f7d74
70230b2
0363e8e
bf2e4bf
0ad7a72
6518a49
24e9e03
57cb277
5f77913
f7eb062
90d09d3
9bddfc3
8f294bb
ed5db59
0223fca
7eeedc2
1a85ff7
cf364cf
b15c4c2
6506c31
60cd11c
2458f44
4daf6f8
24f34f1
be64ca7
601b98d
44bd964
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package blackjack; | ||
|
||
import blackjack.controller.BlackJackController; | ||
import blackjack.view.InputView; | ||
import blackjack.view.OutputView; | ||
|
||
public class BlackJackApplication { | ||
|
||
public static void main(String[] args) { | ||
final InputView inputView = new InputView(); | ||
final OutputView outputView = new OutputView(); | ||
|
||
final BlackJackController blackJackController = new BlackJackController(inputView, outputView); | ||
blackJackController.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package blackjack.controller; | ||
|
||
import blackjack.domain.card.Card; | ||
import blackjack.domain.card.Deck; | ||
import blackjack.domain.participant.Dealer; | ||
import blackjack.domain.participant.Participant; | ||
import blackjack.domain.participant.Participants; | ||
import blackjack.domain.participant.Player; | ||
import blackjack.domain.participant.Result; | ||
import blackjack.view.InputView; | ||
import blackjack.view.OutputView; | ||
import blackjack.view.dto.CardsResponse; | ||
import blackjack.view.dto.DealerResultResponse; | ||
import blackjack.view.dto.DealerStateResponse; | ||
import blackjack.view.dto.ParticipantResponse; | ||
import blackjack.view.dto.PlayerResultResponse; | ||
import java.util.EnumMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class BlackJackController { | ||
|
||
private static final int INITIAL_DRAW_COUNT = 2; | ||
|
||
private final InputView inputView; | ||
private final OutputView outputView; | ||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. View를 필드로 사용하신 이유가 있으실까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
제가 이해한 게 맞다면 제가 필드로 사용한 이유는 다음과 같습니다. 우선 하지만 필드로 사용한 이유는 |
||
|
||
public BlackJackController(final InputView inputView, final OutputView outputView) { | ||
this.inputView = inputView; | ||
this.outputView = outputView; | ||
} | ||
|
||
public void run() { | ||
final Participants participants = new Participants(new Dealer(), gatherPlayers()); | ||
final Deck deck = Deck.createUsingTrump(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저만 이 1이 이해가 잘 안될까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다시 한번 읽어보니 무슨 의미를 가지는지 명확하지 않은 것 같습니다 .. 매직 넘버를 사용하여 의미를 명확하게 드러낼 수 있도록 수정해보겠습니다! |
||
|
||
dealCards(participants, deck); | ||
|
||
drawCard(participants.getPlayers(), deck); | ||
drawCard(participants.getDealer(), deck); | ||
|
||
printResult(participants); | ||
} | ||
|
||
private List<Player> gatherPlayers() { | ||
final List<String> playerNames = inputView.readPlayerNames(); | ||
return playerNames.stream() | ||
.map(Player::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private void dealCards(final Participants participants, final Deck deck) { | ||
participants.drawCard(deck, INITIAL_DRAW_COUNT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. INITIAL_DRAW_COUNT 이거를 컨트롤러에서 관리하는 것이 맞을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 뷰와 도메인 사이에서 중간 매개체 역할을 수행한다고 생각합니다. |
||
|
||
final ParticipantResponse dealerResponse = getHiddenDealerResponse(participants.getDealer()); | ||
final List<ParticipantResponse> playerResponse = getParticipantResponses(participants.getPlayers()); | ||
|
||
outputView.printDealCards(dealerResponse, playerResponse, INITIAL_DRAW_COUNT); | ||
} | ||
|
||
private ParticipantResponse getHiddenDealerResponse(final Dealer dealer) { | ||
final List<Card> hiddenCards = dealer.getCards().subList(0, INITIAL_DRAW_COUNT - 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 코드는 Dealer의 자율성을 침해하는 것 같아요~! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그렇네요! 꼼꼼한 리뷰 감사합니다 👾 |
||
final CardsResponse cardsResponse = new CardsResponse(-1, getCardInfos(hiddenCards)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이곳의 -1 역시 처음 보는 사람 입장에서 불명확한 것 같아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분도 위와 동일하게 수정해 보겠습니다! |
||
return new ParticipantResponse(dealer.getName(), cardsResponse); | ||
} | ||
|
||
private List<String> getCardInfos(final List<Card> cards) { | ||
return cards.stream() | ||
.map(card -> card.getNumberName() + card.getSuitName()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private List<ParticipantResponse> getParticipantResponses(final List<? extends Participant> participants) { | ||
return participants.stream() | ||
.map(this::getParticipantResponse) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private ParticipantResponse getParticipantResponse(final Participant participant) { | ||
final CardsResponse cardsResponse = new CardsResponse( | ||
participant.getScore(), getCardInfos(participant.getCards()) | ||
); | ||
return new ParticipantResponse(participant.getName(), cardsResponse); | ||
} | ||
|
||
private void drawCard(final List<Player> players, final Deck deck) { | ||
for (final Player player : players) { | ||
drawCard(player, deck); | ||
} | ||
} | ||
|
||
private void drawCard(final Player player, final Deck deck) { | ||
while (player.isDrawable() && inputView.readMoreDraw(player.getName())) { | ||
player.drawCard(deck.draw()); | ||
outputView.printHandedCardsWithoutScore(getParticipantResponse(player)); | ||
} | ||
} | ||
|
||
private void drawCard(final Dealer dealer, final Deck deck) { | ||
if (dealer.isDrawable()) { | ||
dealer.drawCard(deck.draw()); | ||
outputView.printDealerDrawn(new DealerStateResponse(true, dealer.getMaximumDrawableScore())); | ||
} | ||
} | ||
|
||
private void printResult(final Participants participants) { | ||
final List<ParticipantResponse> participantResponses = getParticipantResponses(participants.getParticipants()); | ||
outputView.printHandedCardsWithScore(participantResponses); | ||
|
||
final Dealer dealer = participants.getDealer(); | ||
final List<Player> players = participants.getPlayers(); | ||
|
||
final List<PlayerResultResponse> playerResult = getPlayerResults(dealer, players); | ||
final DealerResultResponse dealerResult = getDealerResult(dealer, playerResult); | ||
|
||
outputView.printFinalResult(dealerResult, playerResult); | ||
} | ||
|
||
private List<PlayerResultResponse> getPlayerResults(final Dealer dealer, final List<Player> players) { | ||
return players.stream() | ||
.map(player -> new PlayerResultResponse(player.getName(), dealer.showResult(player.getScore()))) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private DealerResultResponse getDealerResult(final Dealer dealer, final List<PlayerResultResponse> playerResults) { | ||
final Map<Result, Integer> dealerResult = initResult(); | ||
for (final PlayerResultResponse playerResult : playerResults) { | ||
final Result result = playerResult.getResult().reverse(); | ||
dealerResult.put(result, dealerResult.get(result) + 1); | ||
} | ||
return new DealerResultResponse(dealer.getName(), dealerResult); | ||
} | ||
|
||
private Map<Result, Integer> initResult() { | ||
final Map<Result, Integer> initResult = new EnumMap<>(Result.class); | ||
for (final Result result : Result.values()) { | ||
initResult.put(result, 0); | ||
} | ||
return initResult; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package blackjack.domain.card; | ||
|
||
public class Card { | ||
|
||
private final Number number; | ||
private final Suit suit; | ||
|
||
public Card(final Number number, final Suit suit) { | ||
this.number = number; | ||
this.suit = suit; | ||
} | ||
|
||
public boolean isAce() { | ||
return number == Number.ACE; | ||
} | ||
|
||
public int getScore() { | ||
return number.getScore(); | ||
} | ||
|
||
public String getNumberName() { | ||
return number.getName(); | ||
} | ||
|
||
public String getSuitName() { | ||
return suit.getName(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package blackjack.domain.card; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class Cards { | ||
|
||
private static final int ACE_BONUS = 10; | ||
private static final int MAXIMUM_UPDATABLE_SCORE = 21; | ||
|
||
private final List<Card> cards; | ||
|
||
public Cards() { | ||
this(new ArrayList<>()); | ||
} | ||
|
||
public Cards(final List<Card> cards) { | ||
this.cards = new ArrayList<>(cards); | ||
} | ||
|
||
public void addCard(final Card card) { | ||
cards.add(card); | ||
} | ||
|
||
public int calculateTotalScore() { | ||
final int score = getTotalScore(); | ||
|
||
if (isExistAce() && isScoreUpdatable(score)) { | ||
return score + ACE_BONUS; | ||
} | ||
|
||
return score; | ||
} | ||
|
||
private int getTotalScore() { | ||
return cards.stream() | ||
.mapToInt(Card::getScore) | ||
.sum(); | ||
} | ||
|
||
private boolean isExistAce() { | ||
return cards.stream() | ||
.anyMatch(Card::isAce); | ||
} | ||
|
||
private boolean isScoreUpdatable(final int score) { | ||
return score + ACE_BONUS <= MAXIMUM_UPDATABLE_SCORE; | ||
} | ||
|
||
public int count() { | ||
return cards.size(); | ||
} | ||
|
||
public List<Card> getCards() { | ||
return Collections.unmodifiableList(cards); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package blackjack.domain.card; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Stack; | ||
import java.util.stream.Collectors; | ||
|
||
public class Deck { | ||
|
||
private static final Stack<Card> TRUMP; | ||
|
||
static { | ||
final Stack<Card> pack = new Stack<>(); | ||
final List<Card> cards = Arrays.stream(Suit.values()) | ||
.flatMap(suit -> Arrays.stream(Number.values()) | ||
.map(number -> new Card(number, suit)) | ||
) | ||
.collect(Collectors.toList()); | ||
pack.addAll(cards); | ||
TRUMP = pack; | ||
} | ||
|
||
private final Stack<Card> cards; | ||
|
||
public Deck(final Stack<Card> cards) { | ||
Collections.shuffle(cards); | ||
this.cards = cards; | ||
} | ||
|
||
public static Deck createUsingTrump(final int count) { | ||
final Stack<Card> pack = new Stack<>(); | ||
for (int i = 0; i < count; i++) { | ||
pack.addAll(TRUMP); | ||
} | ||
return new Deck(pack); | ||
} | ||
|
||
public Card draw() { | ||
if (cards.empty()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이런 예외가 생길 거라 생각을 못해봤는데 좋은 거 같네요! 👍 |
||
throw new IllegalStateException("덱에 더 이상의 카드가 없습니다."); | ||
} | ||
return cards.pop(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
전체적으로 Controller가 너무 많은 책임을 담당하고 있다고 생각해요~!
조금 더 개선해 볼 수 있을 것 같아요