-
Notifications
You must be signed in to change notification settings - Fork 550
[π μ¬μ΄ν΄2 - λ―Έμ (λΈλμ κ²μ μ€ν)] νλ―Έλ°₯ λ―Έμ μ μΆν©λλ€. #1112
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: gahyeonnni
Are you sure you want to change the base?
Changes from all commits
1da503b
cc058b4
b85a897
2bd3557
904efb2
ff51b03
1c569c5
7540373
ac7eb2e
d45e1dc
c181ba7
268fe7b
e0e7145
569ebfc
9a30ff3
21d97c6
d74b589
fa2598d
9dd0c9f
e2c573f
3e9e6c8
fa98d95
a4266b0
4ff24ce
d7945e4
2e9fd10
fcc7d80
a4ed4b0
67adfab
c524a39
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,24 +1,20 @@ | ||
| package controller; | ||
|
|
||
| import domain.Card; | ||
| import domain.Dealer; | ||
| import domain.Deck; | ||
| import domain.GameResult; | ||
| import domain.Outcome; | ||
| import domain.Participant; | ||
| import domain.Player; | ||
| import domain.Players; | ||
| import dto.AllOutcomeDto; | ||
| import domain.card.Deck; | ||
| import domain.card.DeckFactory; | ||
| import domain.participant.Dealer; | ||
| import domain.participant.Money; | ||
| import domain.participant.Player; | ||
| import domain.participant.Players; | ||
| import domain.result.Outcome; | ||
| import dto.FinalScoreDto; | ||
| import dto.InitialDto; | ||
| import dto.MoneyDTO; | ||
| import dto.ParticipantDto; | ||
| import dto.ParticipantScoreDto; | ||
| import dto.PlayerOutcomeDto; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
| import util.NameParser; | ||
| import view.InputView; | ||
|
|
@@ -34,46 +30,61 @@ public BlackJackGame(InputView inputView, ResultView resultView) { | |
| } | ||
|
|
||
| public void run() { | ||
| Deck deck = createDeck(); | ||
| Deck deck = DeckFactory.create(); | ||
| Dealer dealer = new Dealer(); | ||
| Players players = NameParser.makeNameList(inputView.getParticipant()); | ||
| Players players = setupPlayers(); | ||
| playGame(deck, dealer, players); | ||
| } | ||
|
|
||
| private void playGame(Deck deck, Dealer dealer, Players players) { | ||
| initialDraw(deck, dealer, players); | ||
| printInitialStatus(dealer, players); | ||
| players.getPlayers().forEach(player -> drawPlayerTurn(deck, player)); | ||
| if (dealer.isFirstBlackJack()) { | ||
| printResults(dealer, players); | ||
| return; | ||
| } | ||
| players.players().forEach(player -> drawPlayerTurn(deck, player)); | ||
| drawDealerTurn(deck, dealer); | ||
| printResults(dealer, players); | ||
| } | ||
|
|
||
| private void printResults(Dealer dealer, Players players) { | ||
| printFinalScore(dealer, players); | ||
| printFinalWinner(dealer, players); | ||
| printFinalProfit(dealer, players); | ||
| } | ||
|
|
||
| private Deck createDeck() { | ||
| List<Card> cards = IntStream.range(0, 52) | ||
| .mapToObj(Card::new) | ||
| .collect(Collectors.toList()); | ||
| return new Deck(cards); | ||
| private Players setupPlayers() { | ||
| List<String> playerNames = NameParser.makeNameList(inputView.getParticipant()); | ||
| List<Player> players = new ArrayList<>(); | ||
| for (String name : playerNames) { | ||
| String money = inputView.getMoney(name); | ||
| players.add(new Player(name, new Money(money))); | ||
| } | ||
| return new Players(players); | ||
| } | ||
|
|
||
| private void initialDraw(Deck deck, Dealer dealer, Players players) { | ||
| IntStream.range(0, 2).forEach(i -> { | ||
| dealer.drawCard(deck.draw()); | ||
| players.getPlayers().forEach(player -> player.drawCard(deck.draw())); | ||
| players.players().forEach(player -> player.drawCard(deck.draw())); | ||
| }); | ||
| } | ||
|
|
||
| private void printInitialStatus(Dealer dealer, Players players) { | ||
| ParticipantDto dealerDto = new ParticipantDto(dealer.getName(), getCardNames(dealer)); | ||
| List<ParticipantDto> participantDtos = players.getPlayers().stream() | ||
| .map(player -> new ParticipantDto(player.getName(), getCardNames(player))) | ||
| .collect(Collectors.toList()); | ||
| String joinedNames = players.getPlayers().stream() | ||
| .map(Player::getName) | ||
| .collect(Collectors.joining(", ")); | ||
| resultView.printGameStart(new InitialDto(joinedNames, dealerDto, participantDtos)); | ||
| ParticipantDto dealerDto = ParticipantDto.from(dealer); | ||
| List<ParticipantDto> participants = | ||
| players.players() | ||
| .stream() | ||
| .map(ParticipantDto::from) | ||
| .toList(); | ||
| String joinedNames = players.getJoinedNames(); | ||
| resultView.printGameStart(new InitialDto(joinedNames, dealerDto, participants)); | ||
| } | ||
|
|
||
| private void drawPlayerTurn(Deck deck, Player player) { | ||
| while (player.canDraw() && inputView.getMoreCards(player.getName()).equals("y")) { | ||
| player.drawCard(deck.draw()); | ||
| resultView.printParticipantMoreCard(new ParticipantDto(player.getName(), getCardNames(player))); | ||
| resultView.printParticipantMoreCard(ParticipantDto.from(player)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -84,39 +95,25 @@ private void drawDealerTurn(Deck deck, Dealer dealer) { | |
| } | ||
| } | ||
|
|
||
| private void printFinalWinner(Dealer dealer, Players players) { | ||
| Map<Player, Outcome> playerResults = new LinkedHashMap<>(); | ||
| List<PlayerOutcomeDto> outcomeDtos = new ArrayList<>(); | ||
| for (Player player : players.getPlayers()) { | ||
| Outcome outcome = Outcome.decideWinner(player.getScore(), dealer.getScore()); | ||
| playerResults.put(player, outcome); | ||
| outcomeDtos.add(new PlayerOutcomeDto(player.getName(), outcome.getName())); | ||
| } | ||
| GameResult gameResult = new GameResult(playerResults); | ||
| resultView.printWinner(new AllOutcomeDto(gameResult.getDealerResult(), outcomeDtos)); | ||
| } | ||
|
|
||
| private void printFinalScore(Dealer dealer, Players players) { | ||
| ParticipantScoreDto dealerScoreDto = createScoreDto(dealer); | ||
| List<ParticipantScoreDto> playerScoreDtos = players.getPlayers().stream() | ||
| .map(this::createScoreDto) | ||
| .collect(Collectors.toList()); | ||
| ParticipantScoreDto dealerScoreDto = ParticipantScoreDto.from(dealer); | ||
| List<ParticipantScoreDto> playerScoreDtos = | ||
| players.players() | ||
| .stream() | ||
| .map(ParticipantScoreDto::from) | ||
| .toList(); | ||
| resultView.printResult(new FinalScoreDto(dealerScoreDto, playerScoreDtos)); | ||
| } | ||
|
|
||
| private ParticipantScoreDto createScoreDto(Participant participant) { | ||
| return new ParticipantScoreDto( | ||
| participant.getName(), | ||
| getCardNames(participant), | ||
| participant.getScore().getGameScore() | ||
| ); | ||
| } | ||
|
|
||
| private List<String> getCardNames(Participant participant) { | ||
| List<String> cardNames = new ArrayList<>(); | ||
| for (Card card : participant.getHand().getCards()) { | ||
| cardNames.add(card.getCardName()); | ||
| private void printFinalProfit(Dealer dealer, Players players) { | ||
| int dealerProfit = 0; | ||
|
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. μ΄λ²μ μꡬμ¬ν λΏλ§ μλλΌ λ κ΄λ ¨ κ°μ κ°νκ² μκΈ°ν΄λ³΄λ©΄, λ¨μ§ ν΄λμ€λ₯Ό λ§λ€μλ€κ³ ν΄μ λμ΄ μλλλ€ :) |
||
| List<PlayerOutcomeDto> outcomeDtos = new ArrayList<>(); | ||
| for (Player player : players.players()) { | ||
| Outcome outcome = Outcome.decideWinner(player, dealer); | ||
| Money playerProfit = player.getMoney().multiply(outcome.getRate()); | ||
|
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. νλ―Έλ°₯μ getter μ λν μκ°μ΄ κΆκΈν΄μ. μ리λ₯Ό ν μ μλ κ°μ²΄λ‘λΆν° μ¬λ£λ₯Ό λΉΌμμ μ리λ₯Ό νμ§ μκ³ , κ·Έ κ°μ²΄κ° μ리λ₯Ό ν μ μλλ‘ νλκ±°μ£ . λ€λ₯Έ getter λ€λ λ€ νμΈν΄μ£ΌμΈμ. |
||
| dealerProfit -= playerProfit.getAmount(); | ||
| outcomeDtos.add(PlayerOutcomeDto.of(player, playerProfit)); | ||
| } | ||
| return cardNames; | ||
| resultView.printScore(new MoneyDTO(String.valueOf(dealerProfit), outcomeDtos)); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,6 @@ | ||
| package domain; | ||
|
|
||
| public class Card { | ||
| private final int card; | ||
|
|
||
| public Card(int card) { | ||
| this.card = card; | ||
| } | ||
| package domain.card; | ||
|
|
||
| public record Card(int card) { | ||
|
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 String getCardName() { | ||
| int shape = card / 13; | ||
| int number = card % 13; | ||
|
|
@@ -15,23 +9,16 @@ public String getCardName() { | |
|
|
||
| public int getScore() { | ||
| int cardNumber = card % 13; | ||
|
|
||
| if (1 <= cardNumber && cardNumber <= 9) { | ||
| return cardNumber + 1; | ||
| } | ||
|
|
||
| if (cardNumber >= 10) { | ||
| return 10; | ||
| } | ||
|
|
||
| return 11; | ||
| } | ||
|
|
||
| public boolean isAce() { | ||
| return card % 13 == 0; | ||
| } | ||
|
|
||
| public int getCard() { | ||
| return card; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,9 @@ | ||
| package domain; | ||
| package domain.card; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Deck { | ||
| private final List<Card> cards; | ||
|
|
||
| public record Deck(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.
μ¬κΈ°μλ μ¬λ°λ μ£Όμ κ° μλλ°μ, (κΌ λμ νμ§λ μμλ λμ§λ§, λμμ΄ λ μ£Όμ μμ.) |
||
| public Deck(List<Card> cards) { | ||
| this.cards = cards; | ||
| shuffle(); | ||
|
|
||
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.
READMEκ° μμ§ μ¬μ΄ν΄1 체ν¬λ¦¬μ€νΈ μ€μ¬μ΄λΌ μ΄λ² μ¬μ΄ν΄μμ μΆκ°λ λ°°ν κ·μΉμ΄ μ μ 보μ΄λ€μ.
λ¬Έμ κ΄λ¦¬λ κ½€λ μ€μν μΌμ΄λ, ν λ² μ λ°μ΄νΈ ν΄λ³ΌκΉμ?
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.
μꡬμ¬ν 체ν¬λ¦¬μ€νΈμ μ΅μ’ μμ΅ κ³μ° κ·μΉμ μ 리ν΄μ μ¬λ Έμ΅λλ€! ( c524a39 μ»€λ° )