-
Notifications
You must be signed in to change notification settings - Fork 388
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
[2단계 - 블랙잭 베팅] 몰리(김지민) 미션 제출합니다. #728
Changes from 57 commits
937c772
c33f8a9
acb508f
0f479e8
ded534e
390884d
119ca9f
eabc67f
19e4c6c
0276cf0
a43adea
8f60a47
b71033b
296da7e
9656899
f54664a
722fc6a
d28bd3a
b332dc0
5e593f0
5ea704a
251a10f
3511b10
5101980
5c7a475
388d0d7
3a8e53a
aa65ec9
a358fe6
2d1d6d9
3c710d1
8e422d6
b59b057
adaecfd
06b4cb4
71f4b2e
79b6622
2c9064a
b8fa92e
75afa78
043085e
834cd64
a9741ff
082ff50
6a70a6a
745424b
d9dbe1f
4f4adf3
7906cae
03672a4
a3e6abe
a4828c2
8ba766e
156b714
d49d52c
f59d0b0
1fe2c06
312de11
aa2e804
2ed0d82
b780a67
0cddc80
97eb24c
d6d92b9
46d7001
4b632f6
08405a9
26cdc61
dd1e387
686cbdb
bfd5083
18a426d
0081a9f
af2808d
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,18 +1,22 @@ | ||
package blackjack; | ||
|
||
import blackjack.dto.NameCardsScore; | ||
import blackjack.model.participant.Dealer; | ||
import blackjack.dto.NameProfit; | ||
import blackjack.model.betting.BettingRule; | ||
import blackjack.model.betting.Money; | ||
import blackjack.model.betting.MoneyStaff; | ||
import blackjack.model.deck.Deck; | ||
import blackjack.model.participant.Name; | ||
import blackjack.model.participant.Playable; | ||
import blackjack.model.participant.Dealer; | ||
import blackjack.model.participant.Players; | ||
import blackjack.model.participant.Gamer; | ||
import blackjack.model.participant.Player; | ||
import blackjack.model.participant.Participants; | ||
import blackjack.model.result.Referee; | ||
import blackjack.model.result.ResultCommand; | ||
import blackjack.model.result.Rule; | ||
import blackjack.model.result.ResultRule; | ||
import blackjack.util.ConsoleReader; | ||
import blackjack.view.InputView; | ||
import blackjack.view.OutputView; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
@@ -21,29 +25,42 @@ public class BlackJackGame { | |
|
||
public void run() { | ||
final Deck deck = Deck.createByRandomOrder(); | ||
final Participants participants = initPlayers(InputView.readPlayerNames(consoleReader), deck); | ||
final Dealer dealer = new Dealer(deck.distributeInitialCard()); | ||
final Referee referee = new Referee(new Rule(dealer), participants); | ||
final Players players = initPlayers(InputView.readPlayerNames(consoleReader), deck); | ||
final Referee referee = new Referee(new ResultRule(dealer), players); | ||
final MoneyStaff moneyStaff = initMoneyStaff(new BettingRule(), players); | ||
|
||
announceInitialCards(dealer, participants); | ||
play(dealer, participants.getPlayers(), deck); | ||
announceResult(dealer, participants, referee); | ||
announceInitialCards(dealer, players); | ||
play(dealer, players, deck); | ||
Map<Player, ResultCommand> nameResults = announceResult(dealer, players, referee); | ||
announceProfitMoney(dealer, moneyStaff, nameResults); | ||
} | ||
|
||
private Participants initPlayers(final List<String> names, final Deck deck) { | ||
return Participants.of(names, deck.distributeInitialCard(names.size())); | ||
private Players initPlayers(final List<String> names, final Deck deck) { | ||
return Players.of(names, deck.distributeInitialCard(names.size())); | ||
} | ||
|
||
private MoneyStaff initMoneyStaff(final BettingRule bettingRule, final Players players) { | ||
Map<Player, Money> playerBettingMoneys = new LinkedHashMap<>(); | ||
for (Player player : players.getPlayers()) { | ||
Money bettingAmount = new Money(InputView.readBettingAmount(player.getName(), consoleReader)); | ||
playerBettingMoneys.put(player, bettingAmount); | ||
} | ||
|
||
return new MoneyStaff(bettingRule, playerBettingMoneys); | ||
} | ||
|
||
private void announceInitialCards(final Dealer dealer, final Participants participants) { | ||
OutputView.printDistributionSubject(participants.getNames()); | ||
|
||
private void announceInitialCards(final Dealer dealer, final Players players) { | ||
OutputView.printDistributionSubject(players.getNames()); | ||
OutputView.printNameAndCards(dealer.getName(), dealer.openCard()); | ||
participants.collectCardsOfEachPlayer() | ||
players.collectCardsOfEachPlayer() | ||
.forEach(OutputView::printNameAndCards); | ||
OutputView.println(); | ||
} | ||
|
||
private void play(final Dealer dealer, final List<Player> players, final Deck deck) { | ||
players.forEach(player -> playPlayerTurn(player, deck)); | ||
private void play(final Dealer dealer, final Players players, final Deck deck) { | ||
players.getPlayers().forEach(player -> playPlayerTurn(player, deck)); | ||
playDealerTurn(dealer, deck); | ||
} | ||
|
||
|
@@ -69,33 +86,41 @@ private void playDealerTurn(final Dealer dealer, final Deck deck) { | |
} | ||
} | ||
|
||
private void distributeNewCard(final Playable playable, final Deck deck) { | ||
playable.receiveCard(deck.distribute()); | ||
private void distributeNewCard(final Gamer gamer, final Deck deck) { | ||
gamer.receiveCard(deck.distribute()); | ||
} | ||
|
||
private void announceResult(final Dealer dealer, final Participants participants, final Referee referee) { | ||
printFinalCardsAndScores(dealer, participants); | ||
printFinalResultCommand(referee); | ||
private Map<Player, ResultCommand> announceResult(final Dealer dealer, final Players players, | ||
final Referee referee) { | ||
printFinalCardsAndScores(dealer, players); | ||
return referee.judgePlayerResult(); | ||
} | ||
|
||
private void printFinalCardsAndScores(final Dealer dealer, final Participants participants) { | ||
private void printFinalCardsAndScores(final Dealer dealer, final Players players) { | ||
OutputView.println(); | ||
NameCardsScore dealerNameCardsScore = new NameCardsScore(dealer.getName(), dealer.openCards(), | ||
dealer.getScore()); | ||
OutputView.printFinalCardsAndScore(dealerNameCardsScore); | ||
OutputView.printFinalCardsAndScore(collectFinalResults(participants)); | ||
OutputView.printFinalCardsAndScore(collectFinalResults(players)); | ||
} | ||
|
||
private List<NameCardsScore> collectFinalResults(final Participants participants) { | ||
return participants.getPlayers().stream() | ||
private List<NameCardsScore> collectFinalResults(final Players players) { | ||
return players.getPlayers().stream() | ||
.map(player -> new NameCardsScore(player.getName(), player.openCards(), player.getScore())) | ||
.toList(); | ||
} | ||
|
||
private void printFinalResultCommand(final Referee referee) { | ||
Map<ResultCommand, Integer> dealerResults = referee.judgeDealerResult(); | ||
OutputView.printDealerFinalResult(dealerResults); | ||
Map<Name, ResultCommand> playerResults = referee.judgePlayerResult(); | ||
OutputView.printFinalResult(playerResults); | ||
private void announceProfitMoney(final Dealer dealer, final MoneyStaff moneyStaff, Map<Player, ResultCommand> playerResults) { | ||
final Map<Player, Money> playerAndBettingMoney = moneyStaff.calculateProfitMoneys(playerResults); | ||
final Money dealerProfit = moneyStaff.calculateDealerProfitAmount( | ||
playerAndBettingMoney.values().stream().toList()); | ||
OutputView.printDealerProfit(dealer.getName(), dealerProfit); | ||
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도 NameProfit을 사용할 수 있겠네요 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. 넵 그렇게 하면 따로 printDealerProfit를 선언하지 않아도 될 것 같네요 감사합니다!! |
||
OutputView.printPlayerProfit(collectNameProfit(playerAndBettingMoney)); | ||
} | ||
|
||
private List<NameProfit> collectNameProfit(final Map<Player, Money> playerAndBettingMoney) { | ||
return playerAndBettingMoney.keySet().stream() | ||
.map(player -> new NameProfit(player.getName(), playerAndBettingMoney.get(player))) | ||
.toList(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package blackjack.dto; | ||
|
||
import blackjack.model.betting.Money; | ||
import blackjack.model.participant.Name; | ||
import blackjack.model.participant.Player; | ||
|
||
public record NameProfit(Name name, Money profit) { | ||
public static NameProfit of(final Player player, final Money money) { | ||
return new NameProfit(player.getName(), money); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package blackjack.model.betting; | ||
|
||
import static blackjack.model.betting.ProfitRate.MINUS_100_PERCENT; | ||
import static blackjack.model.betting.ProfitRate.PLUS_100_PERCENT; | ||
import static blackjack.model.betting.ProfitRate.PLUS_150_PERCENT; | ||
import static blackjack.model.betting.ProfitRate.ZERO; | ||
import static blackjack.model.result.ResultCommand.DRAW; | ||
import static blackjack.model.result.ResultCommand.WIN; | ||
|
||
import blackjack.model.participant.Player; | ||
import blackjack.model.result.ResultCommand; | ||
|
||
public class BettingRule { | ||
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. BettingRule은 매번 생성되어야 하는 클래스인가요? 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. BettingRule이 상태를 가지지 않고 불변이기 때문에 매번 생성하는 것보단, 싱글톤으로 만드는 게 불필요한 객체 생성을 줄일 수 있을 것 같아요! |
||
|
||
public ProfitRate calculateProfitRate(final Player player, final ResultCommand resultCommand) { | ||
if (resultCommand.equals(WIN)) { | ||
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. enum은 |
||
return calculateProfitRateWhenPlayerWin(player); | ||
} | ||
|
||
return calculateProfitRateWhenPlayerNotWin(resultCommand); | ||
} | ||
|
||
private ProfitRate calculateProfitRateWhenPlayerWin(final Player player) { | ||
if (player.isBlackJack()) { | ||
return PLUS_150_PERCENT; | ||
} | ||
|
||
return PLUS_100_PERCENT; | ||
} | ||
|
||
private ProfitRate calculateProfitRateWhenPlayerNotWin(final ResultCommand resultCommand) { | ||
if (resultCommand.equals(DRAW)) { | ||
return ZERO; | ||
} | ||
return MINUS_100_PERCENT; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package blackjack.model.betting; | ||
|
||
import java.util.Objects; | ||
|
||
public class Money { | ||
private final int value; | ||
|
||
public Money(final int value) { | ||
this.value = value; | ||
} | ||
|
||
public Money multiple(final ProfitRate rate) { | ||
return new Money((int) (value * rate.getRate())); | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Money money = (Money) o; | ||
return value == money.value; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
} |
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.
ConsoleReader가 util에 있네요. util에는 어떤 클래스가 들어가나요?
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.
전역적으로 사용되는 파일이 들어가야 할 거 같아요 🥲
reader는 view 에서만 사용되기 때문에 view 패키지 안에 존재하는 것이 맞을 것 같네요!
감사합니다 파랑! 🌊