-
Notifications
You must be signed in to change notification settings - Fork 550
[π μ¬μ΄ν΄2 - λ―Έμ (λΈλμ λ² ν )] μλ² λ―Έμ μ μΆν©λλ€. #1102
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
Changes from all commits
c15d501
5b1fa0f
6914d5e
54d9be6
219ee29
8e6ad0a
89911aa
3a0577d
fd6108d
7dd3ffe
0ce22d2
ccc2f7d
9e9bff0
414c5b3
18f2d9c
f58565d
a2b6b06
d855fc4
46807d6
e97ce9b
00f55da
0075c48
0fa789c
05dc9f3
7abdbed
b9ac11c
64a8d08
9719248
417f72f
19a74ec
a4cdc0e
32383b4
b0622b2
ded3f8b
b85d895
fcba505
556d1de
da880c7
6ba5301
08ce109
efdaba9
c375bae
991503c
d0398d1
ef6e0d2
a63f529
df9803a
b2d2d36
9ec41b6
e9ee2f0
05a9504
f397b1c
4b56ae8
0e7417d
9c6c404
ebf96c6
9c2cab0
7a3a4cd
f712193
5f0d2e3
d550531
9047d7a
1dffd25
a2712e2
ffa2e70
4b142e6
ba922a4
ff2a3a1
7174a93
104aea2
338152f
c783fbb
5e46963
da1e88f
58be5be
bb11f00
ee03f6c
26b13e7
ad9f3b4
fdf622c
6fe7b0b
e36f7b4
18aa52e
437c65a
98af4e9
04cde52
5ccc95f
7a2e591
b71faed
624da9e
56d49a8
27e753f
9293e35
7dbe783
b68752e
ef99444
a3dbc43
34e18b9
2bd6557
7e6dd4e
4f9db93
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,91 @@ | ||
| package blackjack; | ||
|
|
||
| import static blackjack.util.ExceptionHandler.retryUntilSuccess; | ||
|
|
||
| import blackjack.model.bet.BetAmount; | ||
| import blackjack.model.bet.BetAmounts; | ||
| import blackjack.model.card.CardProvider; | ||
| import blackjack.model.command.HitCommand; | ||
| import blackjack.model.gameresult.ProfitResult; | ||
| import blackjack.model.user.User; | ||
| import blackjack.model.user.Users; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Function; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public class BlackjackGame { | ||
|
|
||
| private final CardProvider cardProvider; | ||
|
|
||
| public BlackjackGame(CardProvider cardProvider) { | ||
| this.cardProvider = cardProvider; | ||
| } | ||
|
|
||
| public Users createUsers(Supplier<String> readUsername) { | ||
| String input = readUsername.get(); | ||
| return new Users(input); | ||
| } | ||
|
|
||
| public BetAmounts createBetAmount(Function<User, String> readBetAmount, Users users) { | ||
| Map<User, BetAmount> betAmounts = new HashMap<>(); | ||
| for (User user : users.getPlayers()) { | ||
| String input = readBetAmount.apply(user); | ||
| BetAmount betAmount = new BetAmount(input); | ||
| betAmounts.put(user, betAmount); | ||
| } | ||
| return new BetAmounts(betAmounts); | ||
| } | ||
|
|
||
| public void drawInitCards(Users users) { | ||
| cardProvider.drawInitCards(users); | ||
| } | ||
|
|
||
| public void hitPlayers(Users users, Function<User, String> readHitCommand, | ||
| Consumer<User> printPlayerCards, Runnable printCantHit) { | ||
| for (User player : users.getPlayers()) { | ||
| hitPlayer(player, readHitCommand, printPlayerCards, printCantHit); | ||
| } | ||
| } | ||
|
|
||
| public void hitDealer(Users users, Runnable printDealerHit) { | ||
| User dealer = users.getDealer(); | ||
| while (dealer.isHitAvailable()) { | ||
| cardProvider.drawOneCard(dealer); | ||
| printDealerHit.run(); | ||
| } | ||
| dealer.stay(); | ||
| } | ||
|
|
||
| public ProfitResult judgeWinner(Users users, BetAmounts betAmounts) { | ||
| return users.judgeWinner(betAmounts); | ||
| } | ||
|
|
||
| public void end(Runnable closeScanner) { | ||
| closeScanner.run(); | ||
| } | ||
|
|
||
| private void hitPlayer(User player, Function<User, String> readHitCommand, Consumer<User> printPlayerCards, | ||
| Runnable printCantHit) { | ||
| while (retryUntilSuccess(() -> checkY(player, readHitCommand)) && isHitAvailable(player, printCantHit)) { | ||
| cardProvider.drawOneCard(player); | ||
| printPlayerCards.accept(player); | ||
| } | ||
| player.stay(); | ||
| } | ||
|
|
||
| private boolean checkY(User player, Function<User, String> readHitCommand) { | ||
| String input = readHitCommand.apply(player); | ||
| HitCommand hitCommand = new HitCommand(input); | ||
| return hitCommand.isY(); | ||
| } | ||
|
|
||
| private boolean isHitAvailable(User player, Runnable printCantHit) { | ||
| if (player.isHitAvailable()) { | ||
| return true; | ||
| } | ||
|
Chocoding1 marked this conversation as resolved.
|
||
| printCantHit.run(); | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,86 +2,56 @@ | |
|
|
||
| import static blackjack.util.ExceptionHandler.retryUntilSuccess; | ||
|
|
||
| import blackjack.model.card.HitCommand; | ||
| import blackjack.model.card.CardProvider; | ||
| import blackjack.model.user.Dealer; | ||
| import blackjack.model.user.Player; | ||
| import blackjack.model.gameresult.PlayersGameResult; | ||
| import blackjack.BlackjackGame; | ||
| import blackjack.model.bet.BetAmounts; | ||
| import blackjack.model.gameresult.ProfitResult; | ||
| import blackjack.model.user.Users; | ||
| import blackjack.view.InputView; | ||
| import blackjack.view.OutputView; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| public class BlackjackController { | ||
|
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. 컨νΈλ‘€λ¬κ° λμ£ ? μ μ΄λ¦μ΄ 컨νΈλ‘€λ¬μ£ ?
Author
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. MVC ν¨ν΄μμ controllerλ viewμ model μ¬μ΄μμ λ°μ΄ν°λ€μ μ λ¬νλ μν μ ν΄λμ€λΌκ³ μκ°ν©λλ€. |
||
|
|
||
| private final CardProvider cardProvider; | ||
| private final BlackjackGame blackjackGame; | ||
|
|
||
| public BlackjackController(CardProvider cardProvider) { | ||
| this.cardProvider = cardProvider; | ||
| public BlackjackController(BlackjackGame blackjackGame) { | ||
| this.blackjackGame = blackjackGame; | ||
| } | ||
|
|
||
| public void run() { | ||
| Users users = retryUntilSuccess(this::createUsers); | ||
| Users users = retryUntilSuccess(() -> blackjackGame.createUsers(InputView::readPlayerName)); | ||
|
|
||
| provideInitCardsAndPrint(users); | ||
| BetAmounts betAmounts = retryUntilSuccess( | ||
| () -> blackjackGame.createBetAmount(InputView::readBetAmount, users)); | ||
|
|
||
| drawInitCardsAndPrint(users); | ||
|
|
||
| hit(users); | ||
|
|
||
| printHandStatus(users); | ||
|
|
||
| printGameResult(users); | ||
|
|
||
| InputView.closeScanner(); | ||
| } | ||
| printProfitResult(users, betAmounts); | ||
|
|
||
| private Users createUsers() { | ||
| String input = InputView.readPlayerName(); | ||
| return new Users(input); | ||
| blackjackGame.end(InputView::closeScanner); | ||
| } | ||
|
|
||
| private void provideInitCardsAndPrint(Users users) { | ||
| cardProvider.provideInitCards(users); | ||
| private void drawInitCardsAndPrint(Users users) { | ||
| blackjackGame.drawInitCards(users); | ||
| OutputView.printInitCards(users); | ||
| } | ||
|
|
||
| private void hit(Users users) { | ||
| List<Player> players = users.getPlayers(); | ||
| Dealer dealer = users.getDealer(); | ||
|
|
||
| for (Player player : players) { | ||
| while (retryUntilSuccess(() -> checkY(player)) && checkAddCard(player)) { | ||
| cardProvider.provideOneCard(player); | ||
| OutputView.printPlayerCards(player); | ||
| } | ||
| } | ||
|
|
||
| while (dealer.isHitAvailable()) { | ||
| cardProvider.provideOneCard(dealer); | ||
| OutputView.printDealerHit(); | ||
| } | ||
| blackjackGame.hitPlayers(users, InputView::readHitCommand, OutputView::printPlayerCards, | ||
| OutputView::printCantHit); | ||
| blackjackGame.hitDealer(users, OutputView::printDealerHit); | ||
| } | ||
|
|
||
| private void printHandStatus(Users users) { | ||
| OutputView.printHandStatus(users); | ||
| } | ||
|
|
||
| private void printGameResult(Users users) { | ||
| PlayersGameResult playersGameResult = users.determineWinner(); | ||
| OutputView.printGameResult(playersGameResult, users); | ||
| } | ||
|
|
||
| private boolean checkY(Player player) { | ||
| String input = InputView.readCardAdd(player).trim(); | ||
| HitCommand hitCommand = new HitCommand(input); | ||
| return hitCommand.isY(); | ||
| } | ||
|
|
||
| private boolean checkAddCard(Player player) { | ||
| if (player.isHitAvailable()) { | ||
| return true; | ||
| } | ||
| OutputView.printCantHit(); | ||
| return false; | ||
| private void printProfitResult(Users users, BetAmounts betAmounts) { | ||
| ProfitResult profitResult = blackjackGame.judgeWinner(users, betAmounts); | ||
| OutputView.printGameResult(profitResult, users); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package blackjack.model.bet; | ||
|
|
||
| public class BetAmount { | ||
|
|
||
| static final String ERROR_EMPTY_INPUT = "μ λ ₯κ°μ κ³΅λ°±μΌ μ μμ΅λλ€."; | ||
| static final String ERROR_BET_AMOUNT_NOT_INTEGER = "λ°°ν κΈμ‘μ μ«μ ννλ‘ μ λ ₯ν΄μΌ ν©λλ€ : %s"; | ||
| static final String ERROR_BET_AMOUNT_NOT_POSITIVE = "λ°°ν κΈμ‘μ 1μ μ΄μμ΄μ΄μΌ ν©λλ€ : %d"; | ||
|
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μμ΄ μλ 10μ μ΄μμΌλ‘ λ°κΏμΌνλ€λ©΄?
Author
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 int amount; | ||
|
|
||
| public BetAmount(String input) { | ||
| validateEmpty(input); | ||
| int amount = convertToInt(input); | ||
| validatePositive(amount); | ||
| this.amount = amount; | ||
| } | ||
|
|
||
| public int getAmount() { | ||
| return amount; | ||
| } | ||
|
|
||
| private void validateEmpty(String input) { | ||
| if (input.isBlank()) { | ||
| throw new IllegalArgumentException(ERROR_EMPTY_INPUT); | ||
| } | ||
| } | ||
|
|
||
| private int convertToInt(String input) { | ||
| try { | ||
| return Integer.parseInt(input); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException(ERROR_BET_AMOUNT_NOT_INTEGER.formatted(input)); | ||
| } | ||
| } | ||
|
|
||
| private void validatePositive(int amount) { | ||
| if (amount <= 0) { | ||
| throw new IllegalArgumentException(ERROR_BET_AMOUNT_NOT_POSITIVE.formatted(amount)); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package blackjack.model.bet; | ||
|
|
||
| import blackjack.model.user.User; | ||
| import java.util.Map; | ||
|
|
||
| public class BetAmounts { | ||
|
|
||
| private final Map<User, BetAmount> betAmounts; | ||
|
|
||
| public BetAmounts(Map<User, BetAmount> betAmounts) { | ||
| this.betAmounts = Map.copyOf(betAmounts); | ||
| } | ||
|
|
||
| public BetAmount findByUser(User user) { | ||
| return betAmounts.get(user); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,51 @@ | ||
| package blackjack.model.card; | ||
|
|
||
| import static blackjack.model.constant.Constant.INIT_CARDS_END_IDX; | ||
| import static blackjack.model.constant.Constant.INIT_CARDS_START_IDX; | ||
|
|
||
| import blackjack.model.user.Dealer; | ||
| import blackjack.model.user.Player; | ||
| import blackjack.model.user.User; | ||
| import blackjack.model.user.Users; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Queue; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class CardProvider { | ||
|
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. providerλΌκ³ μ΄λ¦ μ§μ μ΄μ κ° λκ°μ?
Author
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. μ²μμλ λ¨μν μΉ΄λλ₯Ό μ 곡νλ κ°μ²΄λκΉ CardProviderλΌκ³ μ§μμ΅λλ€. |
||
|
|
||
| private static final int INIT_CARDS_START_IDX = 0; | ||
| private static final int INIT_CARDS_END_IDX = 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. idx μ½μ΄ κΈμ§ |
||
|
|
||
| private final Queue<Card> deck = new LinkedList<>(); | ||
|
|
||
| public CardProvider() { | ||
| initDeck(); | ||
| } | ||
|
|
||
| private void initDeck() { | ||
| List<Card> cards = new ArrayList<>(); | ||
| for (Rank rank : Rank.values()) { | ||
| for (Suit suit : Suit.values()) { | ||
| cards.add(new Card(rank, suit)); | ||
| } | ||
| } | ||
| List<Card> cards = Arrays.stream(Rank.values()) | ||
| .flatMap(rank -> Arrays.stream(Suit.values()) | ||
|
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. stream μμμ λ streamμ μ²λ¦¬νλ©΄ μ½λκ° λ³΅μ‘ν΄μ§λλ€ |
||
| .map(suit -> new Card(rank, suit))) | ||
| .collect(Collectors.toCollection(ArrayList::new)); | ||
| Collections.shuffle(cards); | ||
| this.deck.addAll(cards); | ||
| } | ||
|
|
||
| public void provideInitCards(Users users) { | ||
| List<Player> players = users.getPlayers(); | ||
| Dealer dealer = users.getDealer(); | ||
| for (int i = INIT_CARDS_START_IDX; i < INIT_CARDS_END_IDX; i++) { | ||
| for (Player player : players) { | ||
| provideOneCard(player); | ||
| } | ||
| provideOneCard(dealer); | ||
| public void drawInitCards(Users users) { | ||
| for (User user : users.getUsers()) { | ||
| drawTwoCard(user); | ||
| } | ||
| } | ||
|
|
||
| public void provideOneCard(User user) { | ||
| public void drawOneCard(User user) { | ||
| if (deck.peek() == null) { | ||
| initDeck(); | ||
| } | ||
| user.addCard(deck.poll()); | ||
| user.draw(deck.poll()); | ||
| } | ||
|
|
||
| private void drawTwoCard(User user) { | ||
| for (int i = INIT_CARDS_START_IDX; i < INIT_CARDS_END_IDX; i++) { | ||
| drawOneCard(user); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.