-
Notifications
You must be signed in to change notification settings - Fork 389
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
[1단계 - 블랙잭 게임 실행] 저문(유정훈) 미션 제출합니다. #472
Changes from all commits
69c8cf2
7a5adac
daf7e71
73e5fe5
fc0cafb
aeea695
8b3c12a
40ce6c5
e626287
56d9a4d
f52efb3
d7643bf
ccc0fe4
c638486
cc7451f
6360d9b
c961cd6
38bc19c
4c9aa21
909a867
94547aa
5c642cd
fdbacf9
2bc1271
00918a7
d84dace
a9f4a69
ebfab91
5a6226e
c16b76e
c3508fa
33138b2
a0cb83a
311fb32
52d41bf
9e675af
a1a1094
bf54b76
2185196
56785fb
a6ec90f
15a0621
6f5c2ec
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,12 @@ | ||
package blackjack; | ||
|
||
import blackjack.controller.BlackJackGameController; | ||
import blackjack.domain.card.ShufflingMachine; | ||
|
||
public class Application { | ||
|
||
public static void main(final String[] args) { | ||
final BlackJackGameController blackJackGameController = new BlackJackGameController(new ShufflingMachine()); | ||
blackJackGameController.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package blackjack.controller; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import blackjack.domain.game.BlackJackGame; | ||
import blackjack.domain.card.ShufflingMachine; | ||
import blackjack.domain.game.GameResult; | ||
import blackjack.domain.game.ResultType; | ||
import blackjack.domain.participant.Dealer; | ||
import blackjack.domain.participant.Player; | ||
import blackjack.domain.participant.Players; | ||
import blackjack.view.InputView; | ||
import blackjack.view.OutputView; | ||
|
||
public class BlackJackGameController { | ||
|
||
private static final String YES_COMMAND = "y"; | ||
private static final String NO_COMMAND = "n"; | ||
private static final int DEALER_DRAWING_BOUNDARY = 16; | ||
private static final int PLAYER_BUST_BOUNDARY = 21; | ||
|
||
private final ShufflingMachine shufflingMachine; | ||
|
||
public BlackJackGameController(final ShufflingMachine shufflingMachine) { | ||
this.shufflingMachine = shufflingMachine; | ||
} | ||
|
||
public void run() { | ||
final BlackJackGame blackJackGame = generateBlackJackGame(); | ||
final Dealer dealer = blackJackGame.getDealer(); | ||
final Players players = blackJackGame.getPlayers(); | ||
|
||
final Map<Player, ResultType> playerResult = playBlackJackGame(blackJackGame, dealer, players); | ||
final GameResult gameResult = new GameResult(playerResult); | ||
|
||
printFinalResult(dealer, players, gameResult); | ||
} | ||
|
||
private BlackJackGame generateBlackJackGame() { | ||
Optional<BlackJackGame> blackJackGame; | ||
do { | ||
blackJackGame = checkNames(); | ||
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. checkName인데, Return값이 있는게 어색하게 느껴져요. 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. 네이밍 수정하였습니다! |
||
} while (blackJackGame.isEmpty()); | ||
return blackJackGame.get(); | ||
} | ||
|
||
private Optional<BlackJackGame> checkNames() { | ||
try { | ||
final String inputNames = InputView.readNames(); | ||
return Optional.of(new BlackJackGame(new Dealer(), inputNames)); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private Map<Player, ResultType> playBlackJackGame(final BlackJackGame blackJackGame, final Dealer dealer, | ||
final Players players) { | ||
blackJackGame.handOutCards(shufflingMachine); | ||
|
||
OutputView.printInitCard(players.getPlayers(), dealer.getFirstCard()); | ||
handOutCardToPlayers(blackJackGame, players); | ||
handOutCardToDealer(blackJackGame, dealer); | ||
|
||
return blackJackGame.makePlayerResult(); | ||
} | ||
|
||
private void printFinalResult(final Dealer dealer, final Players players, final GameResult gameResult) { | ||
OutputView.printCardsWithSum(players.getPlayers(), dealer); | ||
OutputView.printFinalResult(gameResult.getPlayerResult(), gameResult.findDealerResult()); | ||
} | ||
|
||
private void handOutCardToPlayers(final BlackJackGame blackJackGame, final Players players) { | ||
for (final Player player : players.getPlayers()) { | ||
handOutCardToEachPlayer(blackJackGame, player); | ||
} | ||
} | ||
Comment on lines
+74
to
+78
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. depth 2를 피하기위함이라면, dpeth2 까지는 괜찮아보여요. |
||
|
||
private void handOutCardToEachPlayer(final BlackJackGame blackJackGame, final Player player) { | ||
boolean command = true; | ||
while (player.isUnderThanBoundary(PLAYER_BUST_BOUNDARY) && command) { | ||
final String playerAnswer = inputGameCommandToGetOneMoreCard(player); | ||
command = isCardHandedOutByCommand(blackJackGame, player, playerAnswer); | ||
} | ||
} | ||
|
||
private String inputGameCommandToGetOneMoreCard(final Player player) { | ||
Optional<String> gameCommand; | ||
do { | ||
gameCommand = checkGameCommand(player); | ||
} while (gameCommand.isEmpty()); | ||
return gameCommand.get(); | ||
} | ||
|
||
private Optional<String> checkGameCommand(final Player player) { | ||
try { | ||
final String gameCommand = InputView.readGameCommandToGetOneMoreCard(player.getName()); | ||
validateCorrectCommand(gameCommand); | ||
return Optional.of(gameCommand); | ||
} catch (final IllegalArgumentException e) { | ||
System.out.println(e.getMessage()); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private boolean isCardHandedOutByCommand(final BlackJackGame blackJackGame, final Player player, | ||
final String playerAnswer) { | ||
if (playerAnswer.equals(YES_COMMAND)) { | ||
blackJackGame.handOutCardTo(shufflingMachine, player); | ||
OutputView.printParticipantCards(player.getName(), player.getCards()); | ||
return true; | ||
} | ||
OutputView.printParticipantCards(player.getName(), player.getCards()); | ||
return false; | ||
} | ||
|
||
private void handOutCardToDealer(final BlackJackGame blackJackGame, final Dealer dealer) { | ||
while (dealer.isUnderThanBoundary(DEALER_DRAWING_BOUNDARY)) { | ||
blackJackGame.handOutCardTo(shufflingMachine, dealer); | ||
OutputView.printDealerReceiveOneMoreCard(); | ||
} | ||
} | ||
|
||
private void validateCorrectCommand(final String gameCommand) { | ||
if (!(YES_COMMAND.equals(gameCommand) || NO_COMMAND.equals(gameCommand))) { | ||
throw new IllegalArgumentException("y 또는 n만 입력 가능합니다."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package blackjack.domain.card; | ||
|
||
import java.util.Objects; | ||
|
||
public class Card { | ||
|
||
private final Rank rank; | ||
private final Suit suit; | ||
|
||
public Card(final Rank rank, final Suit suit) { | ||
this.rank = rank; | ||
this.suit = suit; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Card card = (Card) o; | ||
return rank == card.rank && suit == card.suit; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(rank, suit); | ||
} | ||
|
||
public Rank getRank() { | ||
return this.rank; | ||
} | ||
|
||
public Suit getSuit() { | ||
return this.suit; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package blackjack.domain.card; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Deck { | ||
|
||
private static final Map<Integer, Card> deck = new HashMap<>(); | ||
|
||
static { | ||
makeDeck(); | ||
} | ||
|
||
private Deck() { | ||
} | ||
|
||
private static void makeDeck() { | ||
int count = 0; | ||
for (final Rank rank : Rank.values()) { | ||
count = addCardsToDeck(rank, count); | ||
} | ||
} | ||
|
||
private static int addCardsToDeck(final Rank rank, int count) { | ||
for (final Suit suit : Suit.values()) { | ||
deck.put(count, new Card(rank, suit)); | ||
count++; | ||
} | ||
return count; | ||
} | ||
|
||
public static Card from(final int index) { | ||
return deck.get(index); | ||
} | ||
|
||
public static List<Integer> getKeys() { | ||
return new ArrayList<>(deck.keySet()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package blackjack.domain.card; | ||
|
||
public enum Rank { | ||
ACE(1, "A"), DEUCE(2, "2"), THREE(3, "3"), | ||
FOUR(4, "4"), FIVE(5, "5"), SIX(6, "6"), | ||
SEVEN(7, "7"), EIGHT(8, "8"), NINE(9, "9"), | ||
TEN(10, "10"), JACK(10, "J"), QUEEN(10, "Q"), | ||
KING(10, "K"); | ||
|
||
private final int value; | ||
private final String rankFormat; | ||
|
||
Rank(final int value, final String rankFormat) { | ||
this.value = value; | ||
this.rankFormat = rankFormat; | ||
} | ||
|
||
public int getValue() { | ||
return this.value; | ||
} | ||
|
||
public String getRankFormat() { | ||
return this.rankFormat; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package blackjack.domain.card; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class ShufflingMachine { | ||
|
||
private final List<Integer> keys; | ||
|
||
public ShufflingMachine() { | ||
this.keys = shuffleDeck(); | ||
} | ||
|
||
private List<Integer> shuffleDeck() { | ||
final List<Integer> keys = Deck.getKeys(); | ||
Collections.shuffle(keys); | ||
return keys; | ||
} | ||
|
||
public Integer draw() { | ||
return keys.remove(0); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package blackjack.domain.card; | ||
|
||
public enum Suit { | ||
HEART("하트"), | ||
DIAMOND("다이아몬드"), | ||
SPADE("스페이드"), | ||
CLOVER("클로버"); | ||
|
||
private final String value; | ||
|
||
Suit(final String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return this.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.
BlackJackGame에서 dealer, players를 빼내서 프로세스를 진행하는것보다, BlackJackGame에서 관리할 순 없는걸까요?!