π λΈλμ 1λ¨κ³
κΈ°λ₯ μꡬμ¬ν
- λΈλμ κ²μμ λ³νν νλ‘κ·Έλ¨μ ꡬννλ€. λΈλμ κ²μμ λλ¬μ νλ μ΄μ΄ μ€ μΉ΄λμ ν©μ΄ 21 λλ 21μ κ°μ₯ κ°κΉμ΄ μ«μλ₯Ό κ°μ§λ μͺ½μ΄ μ΄κΈ°λ κ²μμ΄λ€.
- νλ μ΄μ΄λ κ²μμ μμν λ λ°°ν κΈμ‘μ μ ν΄μΌ νλ€. μΉ΄λμ μ«μ κ³μ°μ μΉ΄λ μ«μλ₯Ό κΈ°λ³ΈμΌλ‘ νλ©°, μμΈλ‘ Aceλ 1 λλ 11λ‘ κ³μ°ν μ μμΌλ©°, King, Queen, Jackμ κ°κ° 10μΌλ‘ κ³μ°νλ€.
- κ²μμ μμνλ©΄ νλ μ΄μ΄λ λ μ₯μ μΉ΄λλ₯Ό μ§κΈ λ°μΌλ©°, λ μ₯μ μΉ΄λ μ«μλ₯Ό ν©μ³ 21μ μ΄κ³Όνμ§ μμΌλ©΄μ 21μ κ°κΉκ² λ§λ€λ©΄ μ΄κΈ΄λ€. 21μ λμ§ μμ κ²½μ° μνλ€λ©΄ μΌλ§λ μ§ μΉ΄λλ₯Ό κ³μ λ½μ μ μλ€. λ¨, μΉ΄λλ₯Ό μΆκ°λ‘ λ½μ 21μ μ΄κ³Όν κ²½μ° λ°°ν κΈμ‘μ λͺ¨λ μκ² λλ€.
- λλ¬λ μ²μμ λ°μ 2μ₯μ ν©κ³κ° 16μ΄νμ΄λ©΄ λ°λμ 1μ₯μ μΉ΄λλ₯Ό μΆκ°λ‘ λ°μμΌ νκ³ , 17μ μ΄μμ΄λ©΄ μΆκ°λ‘ λ°μ μ μλ€.
- κ²μμ μλ£ν ν κ° νλ μ΄μ΄λ³λ‘ μΉν¨λ₯Ό μΆλ ₯νλ€.
ꡬνν κΈ°λ₯ λͺ©λ‘
- μ°Έκ°μ μ΄λ¦ λ°μ λλ¬μ μ°Έκ°μ κ°μ²΄ μμ± - μ°Έκ°μμ λλ¬κ° κ°μ§ μΉ΄λ κ°μ²΄ μμ± - λ λ°μ μ§ λ§μ§ μ λ ₯μ λ°λΌ μΆκ°μ μΈ μΉ΄λ κ°μ²΄ μμ± - λλ¬μ μΆκ°μ μΈ μΉ΄λ κ°μ²΄ μμ± - λλ¬μ λΉκ΅νμ¬ μΉν¨ ꡬνκΈ° - κ²μ κ²°κ³Ό μΆλ ₯
μΆλ ₯
κ²μμ μ°Έμ¬ν μ¬λμ μ΄λ¦μ μ
λ ₯νμΈμ.(μΌν κΈ°μ€μΌλ‘ λΆλ¦¬)
halim,jinhee
λλ¬μ halim, jinheeμκ² 2μ₯μ μΉ΄λλ₯Ό λλμμ΅λλ€.
λλ¬ μΉ΄λ: 3λ€μ΄μλͺ¬λ, 9ν΄λ‘λ²
halim μΉ΄λ: 2ννΈ, 8μ€νμ΄λ
jinhee μΉ΄λ: 7ν΄λ‘λ², Kμ€νμ΄λ
halimλ νμ₯μ μΉ΄λλ₯Ό λ λ°κ² μ΅λκΉ?(μλ y, μλμ€λ n)
y
halim μΉ΄λ: 2ννΈ, 8μ€νμ΄λ, Aν΄λ‘λ²
halimλ νμ₯μ μΉ΄λλ₯Ό λ λ°κ² μ΅λκΉ?(μλ y, μλμ€λ n)
n
jinheeμ νμ₯μ μΉ΄λλ₯Ό λ λ°κ² μ΅λκΉ?(μλ y, μλμ€λ n)
n
jinhee μΉ΄λ: 7ν΄λ‘λ², Kμ€νμ΄λ
λλ¬λ 16μ΄νλΌ νμ₯μ μΉ΄λλ₯Ό λ λ°μμ΅λλ€.
λλ¬ μΉ΄λ: 3λ€μ΄μλͺ¬λ, 9ν΄λ‘λ², 8λ€μ΄μλͺ¬λ - κ²°κ³Ό: 20
halim μΉ΄λ: 2ννΈ, 8μ€νμ΄λ, Aν΄λ‘λ² - κ²°κ³Ό: 21
jinhee μΉ΄λ: 7ν΄λ‘λ², Kμ€νμ΄λ - κ²°κ³Ό: 17
## μ΅μ’
μΉν¨
λλ¬: 1μΉ 1ν¨
halim: μΉ
jinhee: ν¨
###
- μΈμ€ν΄μ€ λ³μ private μ μΈ
- card number, shapes Enum κ΄λ¦¬ -> Enum λ³μλͺ νκΈ x, μμ΄ μ¬μ©
- controllerλΌλ¦¬ νΈμΆ x
main controller
public class Controller {
public static final int DEALER_ONE_MORE_CARD_STANDARD_NUMBER = 16;
public static final String DEALER = "λλ¬";
public static final String NO = "n";
public void play() {
List<String> names = inputParticipantsNames();
List<Player> players = Participants.create(names);
Participants participants = new Participants(players);
Player dealer = new Player(DEALER, Cards.getInitialCards());
printPlayersInitialCards(dealer, participants);
GameResult gameResult = getGameResult(getFinalParticipants(participants), getFinalDealer(dealer));
printGameResult(gameResult);
}
private List<String> inputParticipantsNames() {
String names = InputView.inputPlayerNames();
String[] participantsNames = Convert.splitNames(names);
System.out.println("");
return new ArrayList<>(Arrays.asList(participantsNames));
}
private void printPlayersInitialCards(Player dealer, Participants participants) {
printPlayersInitialMessage(participants);
System.out.println("");
printPlayerCards(dealer);
for (Player user : participants.getParticipants())
printPlayerCards(user);
System.out.println("");
}
// μ΅μ’
μ°Έμ¬μ μΉ΄λ
private Participants getFinalParticipants(Participants participants) {
List<Player> players = new ArrayList<>();
for (Player player : participants.getParticipants()) {
Player finalUserCard = checkOneMoreCard(player);
printPlayerCards(finalUserCard);
players.add(finalUserCard);
}
System.out.println("");
return new Participants(players);
}
// μ΅μ’
λλ¬ μΉ΄λ
private Player getFinalDealer(Player dealer) {
Cards cards = dealer.getCards();
if (Cards.getSum(cards) <= DEALER_ONE_MORE_CARD_STANDARD_NUMBER) {
Card randomCard = Card.getRandomCard();
cards.addCard(randomCard);
printDealerOneMoreCardMessage();
}
return dealer;
}
private Player checkOneMoreCard(Player participant) {
Cards cards = participant.getCards();
while (!isNoAnswer(participant)) {
Card card = Card.getRandomCard();
cards.addCard(card);
printPlayerCards(participant);
}
return participant;
}
private boolean isNoAnswer(Player participant) {
String name = participant.getName();
String answer = InputView.inputYesOrNoOneMoreCard(name);
return answer.equals(NO);
}
private void printDealerOneMoreCardMessage() {
OutputView.printDealerOneMoreCardMessage();
System.out.println("");
}
private void printPlayerCards(Player participant) {
OutputView.printPlayerCards(participant);
System.out.println("");
}
private void printPlayersInitialMessage(Participants participants) {
String names = Convert.getNamesWithComma(participants.getParticipants());
OutputView.printPlayersInitialMessage(names);
System.out.println("");
}
private void printPlayersCardsResult(Player dealer, Participants participants) {
printPlayerCardsResult(dealer);
for (Player participant : participants.getParticipants())
printPlayerCardsResult(participant);
System.out.println("");
}
private void printPlayerCardsResult(Player player) {
Cards cards = player.getCards();
int playersResult = Cards.getSum(cards);
OutputView.printPlayerCardsSumResult(player, playersResult);
}
private GameResult getGameResult(Participants participants, Player dealer) {
printPlayersCardsResult(dealer, participants);
Map<String, String> participantsResult = GameResultCreator.getParticipantsResult(dealer, participants);
Map<String, Integer> dealerResult = GameResultCreator.getDealerResult(participantsResult);
return new GameResult(participantsResult, dealerResult);
}
private void printGameResult(GameResult gameResult) {
OutputView.printDealerGameResult(gameResult.getDealerResult());
OutputView.printParticipantsGameResult(gameResult.getParticipantsResult());
}
}
model.Card
public class Card {
private static final int MAX_ACE_NUMBER = 11;
private static final int STANDARD_NUMBER = 21;
private static final int NUMBERS_COUNT = 12;
private static final int SHAPES_COUNT = 4;
private static final char ACE = 'A';
private static final char JACK = 'J';
private static final char QUEEN = 'Q';
private static final char KING = 'K';
private final CardNumbers number;
private final CardShapes shape;
public Card(CardNumbers number, CardShapes shape) {
this.number = number;
this.shape = shape;
}
public static Card getRandomCard() {
Random random = new Random();
int randomNumberIdx = random.nextInt(NUMBERS_COUNT);
int randomShapeIdx = random.nextInt(SHAPES_COUNT);
CardNumbers number = CardNumbers.getCardNumber(randomNumberIdx);
CardShapes shape = CardShapes.getCardShape(randomShapeIdx);
return new Card(number, shape);
}
public static boolean isMinAceNumber(int cardsNumberSum) {
return cardsNumberSum + MAX_ACE_NUMBER > STANDARD_NUMBER;
}
public static boolean isSpecialCard(char number) {
return isJackCard(number) || isQueenCard(number) || isKingCard(number);
}
public static boolean isAceCard(char number) {
return number == ACE;
}
public String getCard() {
return number.getNumber() + shape.getShape();
}
private static boolean isJackCard(char number) {
return number == JACK;
}
private static boolean isQueenCard(char number) {
return number == QUEEN;
}
private static boolean isKingCard(char number) {
return number == KING;
}
}
model.CardNumbers
Enum
public enum CardNumbers {
TWO(0, '2'),
THREE(1, '3'),
FOUR(2, '4'),
FIVE(3, '5'),
SIX(4, '6'),
SEVEN(5, '7'),
EIGHT(6, '8'),
NINE(7, '9'),
ACE(8, 'A'),
JACK(9, 'J'),
KING(10, 'K'),
QUEEN(11, 'Q');
private final int idx;
private final char number;
CardNumbers(int cardIdx, char cardNumber) {
this.idx = cardIdx;
this.number = cardNumber;
}
public static CardNumbers getCardNumber(int cardIdx) {
return Arrays.stream(CardNumbers.values())
.filter(num -> num.idx == cardIdx)
.findAny()
.orElse(null);
}
public char getNumber() {
return this.number;
}
}
model.Cards
public class Cards {
private static final int MIN_ACE_NUMBER = 1;
private static final int MAX_ACE_NUMBER = 11;
private static final int TEN = 10;
private static final int INITIAL_CARD_COUNT = 2;
private final List<Card> cards;
public Cards(List<Card> cards) {
this.cards = cards;
}
public List<Card> getCards() {
return this.cards;
}
public void addCard(Card card) {
cards.add(card);
}
public static Cards getInitialCards() {
List<Card> cards = new ArrayList<>();
for (int i = 0; i < INITIAL_CARD_COUNT; i++)
cards.add(Card.getRandomCard());
return new Cards(cards);
}
public static int getSum(Cards cards) {
int sum = 0;
for (Card card : cards.getCards()) {
char number = card.getCard().charAt(0);
if (Card.isMinAceNumber(sum) && Card.isAceCard(number)) {
sum += MIN_ACE_NUMBER;
continue;
}
if (!Card.isMinAceNumber(sum) && Card.isAceCard(number)) {
sum += MAX_ACE_NUMBER;
continue;
}
if (Card.isSpecialCard(number)) {
sum += TEN;
continue;
}
sum += Character.getNumericValue(number);
}
return sum;
}
}
model.CardShapes
Enum
public enum CardShapes {
CLOVER(0, "ν΄λ‘λ²"),
HEART(1, "ννΈ"),
DIAMOND(2, "λ€μ΄μλͺ¬λ"),
SPADE(3, "μ€νμ΄λ");
private final int idx;
private final String shape;
CardShapes(int idx, String shape) {
this.shape = shape;
this.idx = idx;
}
public static CardShapes getCardShape(int cardIdx) {
return Arrays.stream(CardShapes.values())
.filter(shape -> shape.idx == cardIdx)
.findAny()
.orElse(null);
}
public String getShape() {
return this.shape;
}
}
model.GameResultCreator
public class GameResultCreator {
public static Map<String, Integer> getDealerResult(Map<String, String> participantsResult) {
Map<String, Integer> dealerResult = new HashMap<String, Integer>() {
{
put(ResultTypes.WIN.getResultType(), 0);
put(ResultTypes.LOSE.getResultType(), 0);
put(ResultTypes.TIE.getResultType(), 0);
}
};
List<String> result = new ArrayList<>();
for (String userName : participantsResult.keySet())
result.add(participantsResult.get(userName));
for (String str : result)
dealerResult.put(str, dealerResult.getOrDefault(str, 0) + 1);
return dealerResult;
}
public static Map<String, String> getParticipantsResult(Player dealer, Participants participants) {
Map<String, String> participantsResult = new HashMap<>();
for (Player user : participants.getParticipants())
Validator.checkParticipantResult(participantsResult, dealer, user);
return participantsResult;
}
}
model.ResultTypes
Enum
public enum ResultTypes {
WIN("μΉ"),
LOSE("ν¨"),
TIE("무");
private final String type;
ResultTypes(String type) {
this.type = type;
}
public String getResultType() {
return type;
}
}
'π» > Java CleanCode Study' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
πͺμ¬λ€λ¦¬ κ²μ # 2022.04.18~2022.05.02 (0) | 2022.06.12 |
---|---|
π°λ‘λ κ²μ (MVC ν¨ν΄) #2022.02.21~2022.03.05 (0) | 2022.06.12 |
Could not find method testCompile() for arguments [{group=junit, name=junit, version=4.12}] (0) | 2022.04.22 |
[ERROR] - Could not find or load main class worker.org.gradle.process.internal.worker.GradleWorkerMain (0) | 2022.02.26 |
[JAVA] test code μμ±λ² (0) | 2022.02.26 |