-
Notifications
You must be signed in to change notification settings - Fork 550
[🚀 사이클2 - 미션 (블랙잭 게임 실행)] 코코 미션 제출합니다. #1116
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
base: yejinkimis
Are you sure you want to change the base?
Changes from all commits
5882e1a
ffc8fa6
d023f75
41ac81f
26f6b74
7a5e58e
ef1ae2c
a84a4c9
63cf184
5b00d90
1262dac
12d28c9
4422cab
7a76f59
e707762
d7235ed
3ccae12
cbd521b
7a3f30c
481010e
4f92400
2b5ca1f
d941c5d
88a8436
d353c14
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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,25 +1,25 @@ | ||||||
| package controller; | ||||||
|
|
||||||
| import static util.Constants.COMMA_DELIMITER; | ||||||
| import static util.Constants.DEALER_NAME; | ||||||
| import static util.Constants.DEFAULT_CARD_SET; | ||||||
| import static util.Constants.HIT; | ||||||
| import static util.Constants.STAND; | ||||||
|
|
||||||
| import domain.betting.Betting; | ||||||
| import domain.betting.BettingAmount; | ||||||
| import domain.card.GameCards; | ||||||
| import domain.game.GamblersGameResult; | ||||||
| import domain.game.Game; | ||||||
| import domain.player.Dealer; | ||||||
| import domain.player.Gambler; | ||||||
| import dto.AgreementRequestDto; | ||||||
| import dto.DealerResultDto; | ||||||
| import dto.ParticipantHandResponseDto; | ||||||
| import dto.ParticipantsGameInfoDto; | ||||||
| import dto.ParticipantsHandResponseDto; | ||||||
| import view.requestDto.AgreementRequestDto; | ||||||
| import view.requestDto.BettingAmountRequestDto; | ||||||
| import view.responseDto.DealerResultDto; | ||||||
| import view.responseDto.ParticipantHandResponseDto; | ||||||
| import view.responseDto.ParticipantsGameInfoDto; | ||||||
| import view.responseDto.ParticipantsHandResponseDto; | ||||||
| import java.util.List; | ||||||
| import util.Parser; | ||||||
| import java.util.Map; | ||||||
| import view.InputView; | ||||||
| import view.OutputView; | ||||||
|
|
||||||
| public class BlackJackController { | ||||||
|
|
||||||
| private final InputView inputView; | ||||||
| private final OutputView outputView; | ||||||
|
|
||||||
|
|
@@ -30,74 +30,82 @@ public BlackJackController(InputView inputView, OutputView outputView) { | |||||
|
|
||||||
| public void run() { | ||||||
| List<String> names = inputGamblersInfo(); | ||||||
| Game game = initializeGame(names); | ||||||
| Map<String, BettingAmount> gamblerNameAndBettingInfo = betByName(names); | ||||||
| Game game = initializeGame(gamblerNameAndBettingInfo); | ||||||
|
|
||||||
| playGame(game); | ||||||
| checkDealerHand(game); | ||||||
|
|
||||||
| printParticipantsResult(game); | ||||||
|
|
||||||
| determineFinalGameResult(game.getResult()); | ||||||
| determineFinalGameProfit(game.getResult()); | ||||||
| } | ||||||
|
|
||||||
| private List<String> inputGamblersInfo() { | ||||||
| String name = inputView.askGamblerNames().name(); | ||||||
| return Parser.parse(name, COMMA_DELIMITER); | ||||||
| return inputView.askGamblerNames().names(); | ||||||
| } | ||||||
|
|
||||||
| private Game initializeGame(List<String> names) { | ||||||
| Game game = new Game(DEALER_NAME, names, DEFAULT_CARD_SET); | ||||||
| private Map<String, BettingAmount> betByName(List<String> names) { | ||||||
| Betting betting = new Betting(names); | ||||||
| for (String name : names) { | ||||||
| BettingAmountRequestDto bettingAmountRequestDto = inputView.askBettingAmount(name); | ||||||
| betting.betBettingAmount(name, | ||||||
| new BettingAmount(bettingAmountRequestDto.getBettingAmount())); | ||||||
| } | ||||||
| return betting.getBettingAmounts(); | ||||||
| } | ||||||
|
|
||||||
| outputView.printInitialDeal(names); | ||||||
| private Game initializeGame(Map<String, BettingAmount> gamblerNameAndBettingInfo) { | ||||||
| Game game = new Game(Dealer.DEALER_NAME, gamblerNameAndBettingInfo, | ||||||
| GameCards.DEFAULT_CARD_SET); | ||||||
|
|
||||||
| outputView.printInitialDeal(gamblerNameAndBettingInfo.keySet().stream().toList()); | ||||||
| game.initializeGame(); | ||||||
| outputView.printParticipantsInfo( | ||||||
| new ParticipantsHandResponseDto(game.getInitialParticipantsHandInfo()) | ||||||
| ); | ||||||
|
|
||||||
| new ParticipantsHandResponseDto(game.getInitialParticipantsHandInfo())); | ||||||
| return game; | ||||||
| } | ||||||
|
|
||||||
| private void playGame(Game game) { | ||||||
| List<Gambler> gamblers = game.getGamblersList(); | ||||||
| for(Gambler gambler : gamblers) { | ||||||
| for (Gambler gambler : gamblers) { | ||||||
| if (gambler.isBlackJack()) { | ||||||
| outputView.printBlackJackMessage(gambler.getName()); | ||||||
| continue; | ||||||
| } | ||||||
| playTurn(game, gambler); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private void playTurn(Game game, Gambler gambler) { | ||||||
| while(!gambler.isBust()) { | ||||||
| while (!gambler.isBust()) { | ||||||
| AgreementRequestDto agreementRequestDto = inputView.askHitOrStand(gambler.getName()); | ||||||
| if (agreementRequestDto.agreement().equals(HIT)) { | ||||||
| gambler.addCard(game.pickCard()); | ||||||
| if (agreementRequestDto.agreement().equals(InputView.HIT)) { | ||||||
|
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.
Suggested change
값을 꺼내 쓰지 않고 메시지를 보내 확인할 수 있을 것 같아요 |
||||||
| game.drawCardTo(gambler); | ||||||
| outputView.printParticipantInfo( | ||||||
| new ParticipantHandResponseDto(gambler.getName(), gambler.getHandInfo())); | ||||||
| } | ||||||
| if (agreementRequestDto.agreement().equals(STAND)) { | ||||||
| if (agreementRequestDto.agreement().equals(InputView.STAND)) { | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private void checkDealerHand(Game game) { | ||||||
| if (game.shouldDealerDraw()) { | ||||||
| outputView.printDealerCardIsUnder16(); | ||||||
| game.addDealerCard(); | ||||||
| outputView.printDealerCardIsBelowDrawThreshold(); | ||||||
| game.drawCardTo(game.getDealer()); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private void printParticipantsResult(Game game) { | ||||||
| outputView.printParticipantsGameInfo(new ParticipantsGameInfoDto( | ||||||
| game.getParticipantGameInfos() | ||||||
| )); | ||||||
| outputView.printParticipantsGameInfo( | ||||||
| new ParticipantsGameInfoDto(game.getParticipantGameInfos())); | ||||||
| } | ||||||
|
|
||||||
| private void determineFinalGameResult(GamblersGameResult gamblersGameResult) { | ||||||
| private void determineFinalGameProfit(GamblersGameResult gamblersGameResult) { | ||||||
| outputView.printDealerResult( | ||||||
| new DealerResultDto(gamblersGameResult.countDealerWin(), | ||||||
| gamblersGameResult.countDealerLose(), | ||||||
| gamblersGameResult.countDealerDraw())); | ||||||
| outputView.printGamblerResult( | ||||||
| gamblersGameResult.getResultInfo() | ||||||
| ); | ||||||
| new DealerResultDto(gamblersGameResult.getDealerProfit().getProfit())); | ||||||
| outputView.printGamblerResult(gamblersGameResult.getParticipantProfits()); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package domain.betting; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Betting { | ||
|
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.
좋은 고민이네요 👍 저는 가능하면 "객체를 생성하기 위해 외부에서 모든 데이터를 수집한 뒤 주입하는 방식" 을 선호하긴 하는데요, 그 이유는 객체를 생성한 후 상태변경을 최소화 하기 위함입니다! 완성된 객체를 생성했기 떄문에 Gambler 의 bettingAmount 을 불변으로 선언할 수 있었죠 ㅎㅎ |
||
|
|
||
| private final Map<String, BettingAmount> values = new LinkedHashMap<>(); | ||
|
|
||
| public Betting(List<String> names) { | ||
| validateDuplicateNames(names); | ||
| for (String name : names) { | ||
| values.put(name, null); | ||
| } | ||
| } | ||
|
|
||
| public void betBettingAmount(String name, BettingAmount bettingAmount) { | ||
| values.put(name, bettingAmount); | ||
| } | ||
|
|
||
| public BettingAmount getBettingAmountByName(String name) { | ||
| return values.get(name); | ||
| } | ||
|
|
||
| public Map<String, BettingAmount> getBettingAmounts() { | ||
| return values; | ||
| } | ||
|
|
||
| public void validateDuplicateNames(List<String> names) { | ||
| List<String> distinctNamesCount = names.stream(). | ||
| distinct().collect(Collectors.toList()); | ||
| if (distinctNamesCount.size() != names.size()) { | ||
| throw new IllegalArgumentException("중복된 이름이 입력됩니다."); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package domain.betting; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class BettingAmount { | ||
|
|
||
| private final int bettingAmount; | ||
|
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. int, double, BigInteger 각각에 대해 알아보고 돈과 관련된 연산에는 어떤 자료구조가 적합한지 고민해보면 좋겠네요 ㅎㅎ |
||
|
|
||
| public BettingAmount(int bettingAmount) { | ||
| this.bettingAmount = bettingAmount; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| BettingAmount that = (BettingAmount) o; | ||
| return bettingAmount == that.bettingAmount; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(bettingAmount); | ||
| } | ||
|
|
||
| public int getBettingAmount() { | ||
| return bettingAmount; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package domain.card; | ||
|
|
||
| public class Card { | ||
|
|
||
| private final CardScore score; | ||
| private final CardKind kind; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,13 +9,16 @@ | |||||
|
|
||||||
| public class GameCards { | ||||||
|
|
||||||
| public static final int DEFAULT_CARD_SET = 1; | ||||||
| public static final int DEFAULT_START_CARD_COUNT = 2; | ||||||
|
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. DEFAULT_START_CARD_COUNT 는 외부에서 사용되고 있는데 해당 클래스에 있는 것이 맞을까요!? |
||||||
|
|
||||||
| private List<Card> cards; | ||||||
|
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.
Suggested change
final 로 선언 가능해보여요 |
||||||
|
|
||||||
| public GameCards(int amount) { | ||||||
| public GameCards(int DEFAULT_CARD_SET) { | ||||||
| this.cards = Arrays.stream(CardKind.values()) | ||||||
| .flatMap(cardKind -> Arrays.stream(CardScore.values()) | ||||||
| .flatMap(cardScore -> | ||||||
| IntStream.range(0, amount) | ||||||
| IntStream.range(0, DEFAULT_CARD_SET) | ||||||
| .mapToObj(card -> new Card(cardScore.getScore(), | ||||||
| cardKind.getKind())) | ||||||
| )) | ||||||
|
|
||||||
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.
딜러가 알고 있는 DEALER_NAME 을 외부에서 알려줄 필요가 있을까요~?