-
Notifications
You must be signed in to change notification settings - Fork 0
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단계] 아토(이혜린) 미션 제출합니다. #17
base: main
Are you sure you want to change the base?
Changes from all commits
143ebe7
21dae3a
1786e9f
76ef94b
3cd8593
f61ae03
be2c348
cc38fcc
959c905
4b04093
a10af0c
48dc12d
c305c41
7ceb8ba
9608caf
4ec8b0e
25353e0
9e6d1f3
9c16047
25088ec
de8d257
a1666f1
fc78bad
a86d395
e836cbe
3cbf5c0
efad4ef
6e0a373
86c80ac
3e0e9ee
46cc05c
4f27bbd
c245b03
70db6d4
cace848
a957752
d4da751
d888faa
5729a1d
1ca43df
ce53b0d
8969a9a
1a0d96e
4d35231
23b3015
0155159
1800705
76bed3c
33d8237
1f5d1ff
eb15bf0
cf27355
543e4a1
8c510c9
429555c
7622c5f
560a034
948d569
b3cc95c
b9c0665
5c8c80d
61950f8
a1b1c15
3e1c8f8
c99ed8e
c551c57
28dd3a1
dc16898
a8150b9
f5dc59d
c5476b0
c2ada09
d595119
feec13d
6a713c2
fb27032
2ec2baa
13c85a2
f057371
e02326f
706741c
fd097e3
e9f554d
8aaf97b
dd795c3
22b97bb
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,19 @@ | ||
package blackjack; | ||
|
||
import blackjack.game.BlackJackGame; | ||
import blackjack.view.InputView; | ||
import blackjack.view.OutputView; | ||
|
||
public class BlackJackMain { | ||
|
||
public static void main(String[] args) { | ||
InputView inputView = new InputView(); | ||
OutputView outputView = new OutputView(); | ||
BlackJackGame blackJackGame = new BlackJackGame(inputView, outputView); | ||
try { | ||
blackJackGame.play(); | ||
} catch (Exception e) { | ||
outputView.printExceptionMessage(e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package blackjack.card; | ||
|
||
import blackjack.player.Score; | ||
import java.util.Objects; | ||
|
||
public class Card { | ||
|
||
private final Shape shape; | ||
private final Rank rank; | ||
|
||
public Card(Shape shape, Rank rank) { | ||
this.shape = shape; | ||
this.rank = rank; | ||
} | ||
|
||
public boolean isAce() { | ||
return rank == Rank.ACE; | ||
} | ||
|
||
public Score getScore() { | ||
return rank.getScore(); | ||
} | ||
|
||
public Shape getShape() { | ||
return shape; | ||
} | ||
|
||
public Rank getRank() { | ||
return rank; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Card card = (Card) o; | ||
return shape == card.shape && rank == card.rank; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(shape, rank); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package blackjack.card; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
|
||
public class Deck { | ||
|
||
private final Queue<Card> cards; | ||
|
||
Deck(List<Card> cards) { | ||
this.cards = new LinkedList<>(cards); | ||
} | ||
|
||
public static Deck createShuffledFullDeck() { | ||
List<Card> cards = new LinkedList<>(); | ||
for (Shape shape : Shape.values()) { | ||
cards.addAll(createNumberCardsOf(shape)); | ||
} | ||
Collections.shuffle(cards); | ||
return new Deck(cards); | ||
} | ||
|
||
static List<Card> createNumberCardsOf(Shape shape) { | ||
List<Card> cards = new ArrayList<>(); | ||
for (Rank rank : Rank.values()) { | ||
cards.add(new Card(shape, rank)); | ||
} | ||
return cards; | ||
} | ||
|
||
public Card draw() { | ||
if (cards.isEmpty()) { | ||
throw new IllegalStateException("[ERROR] 덱이 비어있습니다."); | ||
} | ||
return cards.poll(); | ||
} | ||
|
||
int size() { | ||
return cards.size(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package blackjack.card; | ||
|
||
import blackjack.player.Score; | ||
|
||
public enum Rank { | ||
|
||
ACE(1), | ||
TWO(2), | ||
THREE(3), | ||
FOUR(4), | ||
FIVE(5), | ||
SIX(6), | ||
SEVEN(7), | ||
EIGHT(8), | ||
NINE(9), | ||
TEN(10), | ||
JACK(10), | ||
QUEEN(10), | ||
KING(10); | ||
|
||
private final Score score; | ||
|
||
Rank(int score) { | ||
this.score = new Score(score); | ||
} | ||
|
||
Score getScore() { | ||
return this.score; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package blackjack.card; | ||
|
||
public enum Shape { | ||
|
||
HEART, | ||
SPADE, | ||
CLOVER, | ||
DIAMOND | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package blackjack.game; | ||
|
||
import blackjack.card.Deck; | ||
import blackjack.player.Dealer; | ||
import blackjack.player.Player; | ||
import blackjack.player.Players; | ||
import blackjack.view.InputView; | ||
import blackjack.view.OutputView; | ||
import java.util.List; | ||
|
||
public class BlackJackGame { | ||
|
||
private final InputView inputView; | ||
private final OutputView outputView; | ||
|
||
public BlackJackGame(InputView inputView, OutputView outputView) { | ||
this.inputView = inputView; | ||
this.outputView = outputView; | ||
} | ||
|
||
public void play() { | ||
Deck deck = Deck.createShuffledFullDeck(); | ||
Dealer dealer = new Dealer(); | ||
Players players = createPlayers(); | ||
PlayerBettingMoney playerBettingMoney = decideBettingMoney(players); | ||
initializeGame(deck, dealer, players); | ||
proceedPlayersTurn(deck, players); | ||
proceedDealerTurn(deck, dealer); | ||
|
||
showCardsWithScore(dealer, players); | ||
showMatchResult(dealer, players, playerBettingMoney); | ||
} | ||
|
||
private Players createPlayers() { | ||
outputView.printNamesRequest(); | ||
List<String> names = inputView.readNames(); | ||
Players players = new Players(names); | ||
outputView.printNewLine(); | ||
return players; | ||
} | ||
|
||
private PlayerBettingMoney decideBettingMoney(Players players) { | ||
PlayerBettingMoney playerBettingMoney = new PlayerBettingMoney(); | ||
for (Player player : players.getPlayers()) { | ||
outputView.printBettingRequestMessage(player.getName()); | ||
Money money = new Money(inputView.readBattingAmount()); | ||
playerBettingMoney.addBetting(player, money); | ||
outputView.printNewLine(); | ||
} | ||
return playerBettingMoney; | ||
} | ||
|
||
private void initializeGame(Deck deck, Dealer dealer, Players players) { | ||
players.doInitialDraw(deck); | ||
dealer.doInitialDraw(deck); | ||
outputView.printInitializeBlackJack(players.getNames()); | ||
showInitialCard(dealer, players); | ||
} | ||
|
||
private void showInitialCard(Dealer dealer, Players players) { | ||
outputView.printDealerFirstCard(dealer.getFirstCard()); | ||
|
||
for (Player player : players.getPlayers()) { | ||
outputView.printPlayerCards(player.getName(), player.getCards()); | ||
} | ||
outputView.printNewLine(); | ||
} | ||
|
||
private void proceedPlayersTurn(Deck deck, Players players) { | ||
for (Player player : players.getPlayers()) { | ||
proceedPlayerTurn(deck, player); | ||
} | ||
outputView.printNewLine(); | ||
} | ||
|
||
private void proceedPlayerTurn(Deck deck, Player player) { | ||
Command command = askPlayerToDrawMore(player); | ||
if (command.isNo()) { | ||
return; | ||
} | ||
player.drawCard(deck); | ||
outputView.printPlayerCards(player.getName(), player.getCards()); | ||
|
||
if (player.hasDrawableScore()) { | ||
proceedPlayerTurn(deck, player); | ||
} | ||
} | ||
|
||
private Command askPlayerToDrawMore(Player player) { | ||
outputView.printDrawMoreCardRequest(player.getName()); | ||
String input = inputView.readCommand(); | ||
return Command.from(input); | ||
} | ||
|
||
private void proceedDealerTurn(Deck deck, Dealer dealer) { | ||
while (dealer.hasDrawableScore()) { | ||
dealer.drawCard(deck); | ||
outputView.printDealerDrawCard(); | ||
outputView.printNewLine(); | ||
} | ||
} | ||
|
||
private void showCardsWithScore(Dealer dealer, Players players) { | ||
outputView.printDealerCardsWithScore(dealer.getCards(), dealer.getScore()); | ||
for (Player player : players.getPlayers()) { | ||
outputView.printPlayerCardsWithScore(player.getName(), player.getCards(), player.getScore()); | ||
} | ||
outputView.printNewLine(); | ||
} | ||
|
||
private void showMatchResult(Dealer dealer, Players players, PlayerBettingMoney bettingResults) { | ||
MatchResults matchResults = calculateMatchResults(dealer, players, bettingResults); | ||
outputView.printResultStart(); | ||
showDealerResult(matchResults); | ||
showPlayersResult(players, matchResults); | ||
} | ||
|
||
private MatchResults calculateMatchResults(Dealer dealer, Players players, PlayerBettingMoney bettingResults) { | ||
MatchResults matchResults = new MatchResults(dealer.getHand()); | ||
for (Player player : players.getPlayers()) { | ||
matchResults.addResult(player, bettingResults.getBettingAmountOf(player)); | ||
} | ||
return matchResults; | ||
} | ||
|
||
private void showDealerResult(MatchResults matchResults) { | ||
outputView.printDealerResult(matchResults.getDealerResult()); | ||
} | ||
|
||
private void showPlayersResult(Players players, MatchResults matchResults) { | ||
for (Player player : players.getPlayers()) { | ||
int playerResult = matchResults.getResultOf(player); | ||
outputView.printPlayerResult(player.getName(), playerResult); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package blackjack.game; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
public enum Command { | ||
YES("y"), | ||
NO("n"); | ||
Comment on lines
+6
to
+8
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. 만약 카드를 더 받을지 말지 결정하는 명령어를 "y"에서 다른 문자열 요구사항으로 변경된다면 현재 사용자 입력을 받는 view 패키지가 따로 존재하고 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. 저도 약간 고민했던 부분인데... |
||
|
||
private final String value; | ||
|
||
Command(String value) { | ||
this.value = value; | ||
} | ||
|
||
public static Command from(String value) { | ||
return Arrays.stream(values()) | ||
.filter(command -> command.value.equals(value)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("[ERROR] 존재하지 않는 명령어입니다.")); | ||
} | ||
|
||
public boolean isNo() { | ||
return Objects.equals(value, Command.NO.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.
이번 미션 진행하면서 이 컨트롤러가 많이 커져서 불편하지 않았나요? ㅎㅎㅎ
저도 너무 커져서 Controller있고 아예 다른 Game이라는 객체를 만들어 요 객체 필드에 Dealer와 Players를 가지고 있게 구현하는 방식으로 리팩터링 해보았어요
initializeGame(deck, dealer, players);
proceedPlayersTurn(deck, players);
proceedDealerTurn(deck, dealer);
showCardsWithScore(dealer, players);
이 메서드들 모두 deck, dealer, players로 동일한데 매번 파라미터로 넘겨주는 형태가 뭔가 조금 이상한 거 같더라구요
그래서 저는 deck을 dealer 필드로 넣고, dealer와 players를 Game 이라는 필드에 넣어 컨트롤러의 무게를 조금 줄이는 방법으로 해보았는데, 나쁘진 않은 거 같더라구요!