Skip to content

Commit

Permalink
refactor: blackjackGame 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
be-student committed Mar 11, 2023
1 parent dd80fd6 commit 6559e0a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 163 deletions.
74 changes: 49 additions & 25 deletions src/main/java/blackjack/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,77 @@

import static blackjack.util.Repeater.repeatUntilNoException;

import blackjack.domain.card.Deck;
import blackjack.domain.card.ShuffledDeckFactory;
import blackjack.domain.participant.Participants;
import blackjack.domain.participant.Player;
import blackjack.domain.service.BlackJackRule;
import blackjack.service.BlackJackGame;
import blackjack.view.DrawCommand;
import blackjack.view.InputView;
import blackjack.view.OutputView;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Application {

public static void main(final String[] args) {
final InputView inputView = new InputView();
final OutputView outputView = new OutputView();
final BlackJackGame blackJackGame = repeatUntilNoException(
() -> BlackJackGame.of(
inputView.inputPlayerNames(),
new ShuffledDeckFactory(),
new BlackJackRule()),
inputView::printInputError);
for (final String playerName : blackJackGame.getPlayerNames()) {
blackJackGame.addPlayerMoney(playerName, inputPlayerMoney(playerName, inputView));
final List<String> playerNames = inputPlayerNames(inputView);
final List<Integer> moneys = new ArrayList<>();
for (final String playerName : playerNames) {
inputPlayerMoney(inputView, moneys, playerName);
}
final Deck deck = new ShuffledDeckFactory().generate();
final Participants participants = Participants.of(playerNames, moneys);
participants.distributeInitialCards(deck);
outputView.printInitialCards(participants.getDealer().getFirstCard(), participants.getPlayersCards());

blackJackGame.distributeInitialCard();
outputView.printInitialCards(
blackJackGame.getDealerFirstCard(),
blackJackGame.getPlayersCards());

for (final String playerName : blackJackGame.getPlayerNames()) {
for (final String playerName : participants.getPlayerNames()) {
DrawCommand playerChoice = DrawCommand.DRAW;
while (blackJackGame.isPlayerDrawable(playerName) && playerChoice != DrawCommand.STAY) {
while (participants.isPlayerDrawable(playerName) && playerChoice != DrawCommand.STAY) {
playerChoice = inputPlayerChoice(playerName, inputView);
if (playerChoice == DrawCommand.DRAW) {
blackJackGame.drawPlayerCard(playerName);
participants.drawPlayerCard(playerName, deck.popCard());
}
outputView.printCardStatusOfPlayer(playerName, blackJackGame.getPlayerCards(playerName));
outputView.printCardStatusOfPlayer(playerName, participants.getPlayerCards(playerName));
}
}
while (blackJackGame.isDealerDrawable()) {
blackJackGame.drawDealerCard();
while (participants.isDealerDrawable()) {
participants.drawDealerCard(deck.popCard());
outputView.printDealerCardDrawMessage();
}

outputView.printFinalStatusOfDealer(blackJackGame.getDealerScore(),
blackJackGame.getDealerCards());
outputView.printFinalStatusOfPlayers(blackJackGame.getPlayersCards(),
blackJackGame.getPlayersScores());
outputView.printFinalMoney(blackJackGame.calculatePlayersMoney());
outputView.printFinalStatusOfDealer(participants.getDealerScore(),
participants.getDealerCards());
outputView.printFinalStatusOfPlayers(participants.getPlayersCards(),
participants.getPlayersScores());

final Map<String, Integer> playerMoney = new LinkedHashMap<>();
for (final Player player : participants.getPlayers()) {
final int resultMoney = new BlackJackRule().calculatePlayerProfit(player, participants.getDealer());
playerMoney.put(player.getName(), resultMoney);
}
outputView.printFinalMoney(playerMoney);
}

private static List<String> inputPlayerNames(final InputView inputView) {
return repeatUntilNoException(() -> {
final List<String> names = inputView.inputPlayerNames();
Participants.validatePlayerNames(names);
return names;
},
inputView::printInputError);
}

private static void inputPlayerMoney(final InputView inputView, final List<Integer> moneys,
final String playerName) {

final int amount = inputPlayerMoney(playerName, inputView);
Participants.validateBettingMoney(amount);
moneys.add(amount);
}

private static DrawCommand inputPlayerChoice(final String playerName, final InputView inputView) {
Expand Down
49 changes: 31 additions & 18 deletions src/main/java/blackjack/domain/participant/Participants.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@ public Participants(final Players players, final Dealer dealer) {
this.dealer = dealer;
}

public static Participants of(final List<String> playerNames, final List<Integer> bettingMoneys) {
return new Participants(Players.from(playerNames, bettingMoneys), new Dealer());
}

//이런 방식으로 검증을 하게 되면, 생성자에서 검증을 하기에, 로직상은 문제가 없지만
//new Names().validate()와 같이 호출하는 것이 직관적일 수도 있을 것 같은데 어떤 방향이 좋으신가요?
//애그리거트 루트로 생각하다보니, 여기에 검증하는 작업을 넣었는데, 생성하는 쪽에서 검증하는 것이 불가능하기에 static으로 작성하게 되었습니다
public static void validatePlayerNames(final List<String> playerNames) {
new Names(playerNames);
}

public static void validateBettingMoney(final int amount) {
new BettingMoney(amount);
}

public void distributeInitialCards(final Deck deck) {
players.distributeInitialCards(deck);
dealer.drawInitialCard(deck.popCard(), deck.popCard());
}

public Player findPlayerByName(final String playerName) {
return players.findPlayerByName(playerName);
}

public boolean isPlayerDrawable(final String playerName) {
return players.isDrawable(playerName);
}
Expand All @@ -45,29 +56,31 @@ public Dealer getDealer() {
return dealer;
}

public Players getPlayers() {
return players;
}

//이런 방식으로 검증을 하게 되면, 생성자에서 검증을 하기에, 로직상은 문제가 없지만
//new Names().validate()와 같이 호출하는 것이 직관적일 수도 있을 것 같은데 어떤 방향이 좋으신가요?
public void validatePlayerNames(final List<String> playerNames) {
new Names(playerNames);
}

public void validateBettingMoney(final int amount) {
new BettingMoney(amount);
public List<Player> getPlayers() {
return players.getPlayers();
}

public List<String> getPlayerNames() {
return players.getPlayerNames();
}

public Map<String, Integer> calculatePlayersScore() {
return players.calculatePlayersScore();
public List<CardResponse> getPlayerCards(final String playerName) {
return players.getPlayerCards(playerName);
}

public Map<String, List<CardResponse>> getPlayersCards() {
return players.getPlayersCards();
}

public int getDealerScore() {
return dealer.currentScore();
}

public List<CardResponse> getDealerCards() {
return dealer.getCards();
}

public Map<String, Integer> getPlayersScores() {
return players.calculatePlayersScore();
}
}
16 changes: 16 additions & 0 deletions src/main/java/blackjack/domain/participant/Players.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,26 @@ public List<String> getNames() {
.collect(Collectors.toList());
}

public int getPlayerProfit(final String name, final double rate) {
return players.stream()
.filter(player -> player.hasName(name))
.findFirst()
.map(player -> player.calculateProfit(rate))
.orElseThrow(PlayerNotFoundException::new);
}

public List<Player> getPlayers() {
return players;
}

public List<CardResponse> getPlayerCards(final String playerName) {
return players.stream()
.filter(player -> player.hasName(playerName))
.findFirst()
.map(Player::getCards)
.orElseThrow(PlayerNotFoundException::new);
}

Map<String, Integer> calculatePlayersScore() {
final Map<String, Integer> playerScore = new LinkedHashMap<>();
for (final Player player : players) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/blackjack/domain/service/BlackJackRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public int calculatePlayerProfit(final Player player, final Dealer dealer) {
return (int) (player.calculateProfit(result.getPlayerProfit()) * result.getPlayerProfit());
}

public ResultType calculateDealerResult(final Dealer dealer, final Player player) {
private ResultType calculateDealerResult(final Dealer dealer, final Player player) {
if (dealer.hasBlackJack()) {
return playWithBlackjack(player);
}
Expand Down
119 changes: 0 additions & 119 deletions src/main/java/blackjack/service/BlackJackGame.java

This file was deleted.

0 comments on commit 6559e0a

Please sign in to comment.