-
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 60 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,118 @@ | ||
package blackjack.controller; | ||
|
||
import blackjack.domain.card.Deck; | ||
import blackjack.domain.card.DeckFactory; | ||
import blackjack.domain.participant.Dealer; | ||
import blackjack.domain.participant.Participant; | ||
import blackjack.domain.participant.Participants; | ||
import blackjack.domain.participant.Player; | ||
import blackjack.domain.result.Score; | ||
import blackjack.view.InputView; | ||
import blackjack.view.OutputView; | ||
import blackjack.view.dto.DealerStateResponse; | ||
import blackjack.view.dto.ParticipantResponse; | ||
import blackjack.view.dto.PlayerResultResponse; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class BlackJackController { | ||
|
||
private static final int INIT_DRAW_COUNT = 2; | ||
private static final int DEALER_LIMIT = 16; | ||
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. 이 부분은 도메인이 알고 있어야 하는 룰이지 않을까요? 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. 동의합니다 ,,, 딜러가 알아야 할 정보인 것 같습니다 '^' |
||
|
||
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 = gatherParticipants(); | ||
final Deck deck = DeckFactory.createWithCount(Deck.TRUMP, 1); | ||
|
||
deck.shuffle(); | ||
dealCards(participants, deck); | ||
|
||
cardDraw(participants.getPlayers(), deck); | ||
cardDraw(participants.getDealer(), deck); | ||
|
||
printResult(participants); | ||
} | ||
|
||
private Participants gatherParticipants() { | ||
final List<Participant> participants = new ArrayList<>(); | ||
|
||
participants.add(new 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.
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. 너무 좋은 것 같습니다 👍 생성자에서 생성하면 클래스의 역할이 많아지게 되는 것 같아서, 딜러와 플레이어를 매개변수로 가지는 생성자를 사용하는건 어떻게 생각하시나요? 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. 아주 좋은데요? |
||
participants.addAll(createPlayers()); | ||
|
||
return new Participants(participants); | ||
} | ||
|
||
private List<Player> createPlayers() { | ||
final List<String> playerNames = inputView.readPlayerNames(); | ||
return playerNames.stream() | ||
.map(Player::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private void dealCards(Participants participants, Deck deck) { | ||
participants.drawCard(deck, INIT_DRAW_COUNT); | ||
|
||
final ParticipantResponse dealerResponse = ParticipantResponse.from(participants.getDealer()); | ||
final List<ParticipantResponse> playerResponse = getPlayerResponse(participants.getPlayers()); | ||
|
||
outputView.printDealCards(dealerResponse, playerResponse, INIT_DRAW_COUNT); | ||
} | ||
|
||
private List<ParticipantResponse> getPlayerResponse(final List<Player> players) { | ||
return players.stream() | ||
.map(ParticipantResponse::from) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private void cardDraw(final List<Player> players, final Deck deck) { | ||
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. 메소드는 동사로 시작하는게 좋지 않을까요? 그리고 같은 메서드명이 3개나 있네요 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. 급하게 작성한 코드는 다 티가 나는 군요 😂 같은 메서드명은 오버로딩을 사용해서 도메인별로 동작을 보여주려고 했습니다! |
||
for (final Player player : players) { | ||
cardDraw(player, deck); | ||
} | ||
} | ||
|
||
private void cardDraw(final Player player, final Deck deck) { | ||
while (player.isDrawable() && inputView.readMoreDraw(player.getName())) { | ||
player.drawCard(deck.draw()); | ||
outputView.printHandedCardsWithoutScore(ParticipantResponse.from(player)); | ||
} | ||
} | ||
|
||
private void cardDraw(final Dealer dealer, final Deck deck) { | ||
if (dealer.isDrawable()) { | ||
dealer.drawCard(deck.draw()); | ||
outputView.printDealerCardDrawn(new DealerStateResponse(true, DEALER_LIMIT)); | ||
} | ||
} | ||
|
||
private void printResult(final Participants participants) { | ||
final List<ParticipantResponse> participantResponses = getParticipantResponses(participants.getParticipants()); | ||
participantResponses.forEach(outputView::printHandedCardsWithScore); | ||
|
||
final Dealer dealer = participants.getDealer(); | ||
|
||
final List<PlayerResultResponse> playerResultResponses = getPlayerResultResponses( | ||
dealer.getScore(), participants.getPlayers()); | ||
outputView.printResult(dealer.getName(), playerResultResponses); | ||
} | ||
|
||
private List<ParticipantResponse> getParticipantResponses(final List<Participant> participants) { | ||
return participants.stream() | ||
.map(ParticipantResponse::from) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<PlayerResultResponse> getPlayerResultResponses(final Score dealerScore, final List<Player> players) { | ||
return players.stream() | ||
.map(player -> new PlayerResultResponse(player.getName(), player.getWinningStatus(dealerScore))) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
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,66 @@ | ||
package blackjack.domain.card; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class Cards { | ||
|
||
private static final int MAXIMUM_SCORE = 21; | ||
private static final int ACE_BONUS = 10; | ||
|
||
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_SCORE; | ||
} | ||
|
||
public boolean isTotalScoreOver() { | ||
return calculateTotalScore() > MAXIMUM_SCORE; | ||
} | ||
|
||
public boolean isMaximumScore() { | ||
return calculateTotalScore() == MAXIMUM_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,36 @@ | ||
package blackjack.domain.card; | ||
|
||
import java.util.Collections; | ||
import java.util.Stack; | ||
|
||
public class Deck { | ||
|
||
public static final Stack<Card> TRUMP; | ||
|
||
static { | ||
final Stack<Card> pack = new Stack<>(); | ||
for (final Suit suit : Suit.values()) { | ||
for (final Number number : Number.values()) { | ||
pack.add(new Card(number, suit)); | ||
} | ||
} | ||
TRUMP = pack; | ||
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. 1depth가 요구사항이라 stream의 flatmap이나 foreach를 사용하는 방법으로 구현해볼 수 도 있을 것 같아! 😄 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. 저도 같은 생각입니다 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. 좋은 의견 너무너무 감사합니다 👍👍 |
||
} | ||
|
||
private final Stack<Card> cards; | ||
|
||
public Deck(final Stack<Card> cards) { | ||
this.cards = cards; | ||
} | ||
|
||
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(); | ||
} | ||
|
||
public void shuffle() { | ||
Collections.shuffle(cards); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package blackjack.domain.card; | ||
|
||
import java.util.Stack; | ||
|
||
public class DeckFactory { | ||
|
||
public static Deck createWithCount(final Stack<Card> pack, int count) { | ||
final Stack<Card> cards = new Stack<>(); | ||
|
||
while (count-- > 0) { | ||
cards.addAll(pack); | ||
} | ||
|
||
return new Deck(cards); | ||
} | ||
} |
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가 너무 많은 책임을 담당하고 있다고 생각해요~!
조금 더 개선해 볼 수 있을 것 같아요