Skip to content

Commit

Permalink
feat: 점수 저장 기능 BlackJackGame 으로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
be-student committed Mar 7, 2023
1 parent 9dbe90f commit c769a1e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/main/java/blackjack/service/BlackJackGame.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package blackjack.service;

import blackjack.domain.BlackJackRule;
import blackjack.domain.BlackJackRuleImpl;
import blackjack.domain.ResultType;
import blackjack.domain.card.Deck;
import blackjack.domain.card.DeckFactory;
Expand All @@ -15,6 +17,7 @@
import blackjack.response.PlayersCardsResponse;
import blackjack.response.PlayersCardsResponse.CardsScore;
import blackjack.response.ResultTypeResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -90,7 +93,14 @@ public PlayersCardsResponse getPlayersCardsResponse() {
}

public FinalResultResponse createFinalResultResponse() {
final Map<String, ResultType> playersToResult = participants.calculateFinalResult();
final BlackJackRule blackJackRule = new BlackJackRuleImpl();
final ParticipantResults participantResults = new ParticipantResults();
final Dealer dealer = participants.getDealer();
participants.getPlayers().getPlayers().forEach(player -> {
final ResultType resultType = blackJackRule.calculateDealerResult(dealer, player);
participantResults.addPlayerResult(player.getName(), resultType);
});
final Map<String, ResultType> playersToResult = participantResults.getPlayerNameToResultType();
final Map<String, ResultTypeResponse> playersToResultResponse = generatePlayersResult(playersToResult);
final Map<ResultTypeResponse, Long> dealerResult = generateDealerResult(playersToResult);
return new FinalResultResponse(playersToResultResponse, dealerResult);
Expand All @@ -114,4 +124,17 @@ private Map<ResultTypeResponse, Long> generateDealerResult(final Map<String, Res
result -> ResultTypeResponse.from(result.getValue()),
Collectors.counting()));
}

private static class ParticipantResults {

private final Map<String, ResultType> playerNameToResultType = new HashMap<>();

void addPlayerResult(final String playerName, final ResultType resultType) {
playerNameToResultType.put(playerName, resultType);
}

Map<String, ResultType> getPlayerNameToResultType() {
return playerNameToResultType;
}
}
}

0 comments on commit c769a1e

Please sign in to comment.