-
Notifications
You must be signed in to change notification settings - Fork 7
[블랙잭 게임 미션-1단계] 오지민 제출합니다 #6
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
base: Ojimin
Are you sure you want to change the base?
Changes from 6 commits
5ff21c2
cf5eb96
1169f19
b091a8a
921e07d
0e9293d
91b80d8
eebd8bc
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,57 @@ | ||
| import model.Dealer; | ||
| import model.Player; | ||
| import model.PlayerList; | ||
| import view.InputView; | ||
| import view.OutputView; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| OutputView.printInputPlayerNameMessage(); | ||
| String playerNames = InputView.inputPlayerName(); | ||
| List<String> playerNameList = Arrays.asList(playerNames.split(",")); | ||
| List<Player> players = playerNameList.stream() | ||
| .map(Player::new) | ||
| .collect(Collectors.toList()); | ||
| PlayerList playerList = new PlayerList(players); | ||
| //게임 진행 | ||
| Dealer dealer = new Dealer(); | ||
| //1)초반 카드 나누기 - 카드 숫자: 하트, 스페이드, 다이아몬드,클로버 => 1~9, A, King, Queen, Jack | ||
| OutputView.printCardAssignment(playerList); | ||
| OutputView.printDealerAndPlayerCard(dealer, playerList); | ||
| //2)카드 뽑기 - player, 딜러 순 | ||
| //player가 n 을 입력하면 현재 player의 카드를 보여주고 다음턴 | ||
| for (Player player : playerList.getPlayerList()) { | ||
| while (true) { | ||
|
Contributor
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. 만약 유저의 카드 합이 21이 넘어버린다면 더 이상 카드를 받을 수 없습니다! |
||
| OutputView.printDrawPlayerCardMessage(player); | ||
| String playerInput = InputView.inputPlayerMoreCard(); | ||
| if (playerInput.equals("y")) { | ||
| player.getCardList().addCard(); | ||
| } | ||
| OutputView.printPlayerCard(player); | ||
| if (playerInput.equals("n")) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| //딜러는 알아서 계산 후 출력 | ||
| if (dealer.getCardList().getSum() > 16) { | ||
| OutputView.printNotDrawDealerCardMessage(); | ||
| } | ||
| if (dealer.getCardList().getSum() <= 16) { | ||
| OutputView.printDrawDealerCardMessage(); | ||
| dealer.getCardList().addCard(); | ||
| } | ||
|
|
||
| //게임 종료 - 결과 출력, 기준 : 한 게임 끝나면 | ||
| OutputView.printResultSum(dealer, playerList); | ||
|
|
||
| //최종 승패 | ||
|
|
||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
||
| public class Card { | ||
| private Type type; | ||
| private Number number; | ||
|
|
||
| public Card() { | ||
| Type randomType = Type.getRandomType(); | ||
| Number randomNumber = Number.getRandomNumber(); | ||
| //객체 간 중복체크 필요 | ||
| this.type = randomType; | ||
| this.number = randomNumber; | ||
|
Comment on lines
+12
to
+16
Contributor
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. 카드에 Random을 넣지 않고 |
||
| } | ||
|
|
||
| public Type getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public Number getNumber() { | ||
| return number; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return number.toString()+type.toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class CardList { | ||
| private List<Card> cardList; | ||
| private int sum = 0; | ||
|
Comment on lines
+7
to
+8
Contributor
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. card가 있다면 합은 언제나 결정할 수 있습니다!! |
||
|
|
||
| public CardList() { | ||
| this.cardList = initCardList(); | ||
| this.sum = calculateSum(this.cardList); | ||
| } | ||
|
|
||
| public List<Card> initCardList() { | ||
| List<Card> cardList = new ArrayList<>(); | ||
| cardList.add(new Card()); | ||
| cardList.add(new Card()); | ||
| return cardList; | ||
| } | ||
|
|
||
| public int calculateSum(List<Card> cardList) { | ||
|
Contributor
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. CardList가 스스로 점수를 계산하는 것 너무 좋습니다 👍👍👍 |
||
| return cardList.stream() | ||
| .mapToInt(card -> card.getNumber().getRank()) | ||
| .sum(); | ||
| } | ||
|
Comment on lines
+22
to
+26
Contributor
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. calculateSum이 있다면 getSum은 필요 없지 않을까요?? |
||
|
|
||
| public List<Card> getCardList() { | ||
| return cardList; | ||
| } | ||
|
|
||
| public int getSum() { | ||
| return sum; | ||
| } | ||
|
|
||
| public void addCard() { | ||
| cardList.add(new Card()); | ||
| updateSum(); | ||
| } | ||
|
|
||
| public void updateSum() { | ||
| this.sum = calculateSum(this.cardList); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Dealer { | ||
| private CardList cardList; | ||
|
|
||
| public Dealer() { | ||
| this.cardList = new CardList(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| String cards = cardList.getCardList().stream() | ||
| .map(Card::toString) | ||
| .collect(Collectors.joining(", ")); | ||
| return "딜러: " + cards; | ||
| } | ||
|
|
||
| public CardList getCardList() { | ||
| return cardList; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package model; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public enum Number { | ||
| A(1), | ||
| TWO(2), | ||
| THREE(3), | ||
| FOUR(4), | ||
| FIVE(5), | ||
| SIX(6), | ||
| SEVEN(7), | ||
| EIGHT(8), | ||
| NINE(9), | ||
| TEN(10), | ||
| K(10), | ||
| Q(10), | ||
| J(10); | ||
| private int rank; | ||
|
|
||
| Number(int rank) { | ||
| this.rank = rank; | ||
| } | ||
|
|
||
| public int getRank() { | ||
| return rank; | ||
| } | ||
|
|
||
| public static Number getRandomNumber() { | ||
| Random random = new Random(); | ||
| return values()[random.nextInt(values().length)]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package model; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Player { | ||
| private String name; | ||
| private CardList cardList; | ||
|
|
||
| public Player(String name) { | ||
| this.name = name; | ||
| this.cardList = new CardList(); | ||
| } | ||
|
Comment on lines
+11
to
+14
Contributor
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. Player와 Dealer가 생성과 동시에 카드 두 장을 가지게 만든 이유가 있을까요?? |
||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public CardList getCardList() { | ||
| return cardList; | ||
| } | ||
|
|
||
| /* | ||
| playerlist에 있는 메서드와 구분 필요 | ||
| */ | ||
| @Override | ||
| public String toString() { | ||
| String cards = cardList.getCardList().stream() | ||
| .map(Card::toString) | ||
| .collect(Collectors.joining(", ")); | ||
| return name + "카드: " + cards; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package model; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class PlayerList { | ||
| private List<Player> playerList; | ||
|
|
||
| public PlayerList(List<Player> playerList) { | ||
| this.playerList = playerList; | ||
| } | ||
|
|
||
| public List<Player> getPlayerList() { | ||
| return playerList; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return playerList.stream() | ||
| .map(Player::getName) | ||
| .collect(Collectors.joining(", ")); | ||
| } | ||
|
Comment on lines
+17
to
+22
Contributor
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. toString은 입,출력 문구를 지정하기에는 적절치 못합니다! |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package model; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public enum Type { | ||
|
|
||
| 하트, 스페이드, 다이아몬드, 클로버; | ||
|
|
||
| public static Type getRandomType() { | ||
| Random random = new Random(); | ||
| return values()[random.nextInt(values().length)]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package view; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
|
|
||
| public static String inputPlayerName() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| return scanner.nextLine(); | ||
| } | ||
|
|
||
| public static String inputPlayerMoreCard() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| return scanner.nextLine(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package view; | ||
|
|
||
| import model.Dealer; | ||
| import model.Player; | ||
| import model.PlayerList; | ||
|
|
||
| public class OutputView { | ||
|
|
||
| public static void printInputPlayerNameMessage() { | ||
| System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"); | ||
| } | ||
|
|
||
| public static void printCardAssignment(PlayerList playerList){ | ||
| System.out.println("딜러와 " + playerList.toString() + "에게 2장을 나누었습니다."); | ||
| } | ||
|
|
||
| public static void printDealerAndPlayerCard(Dealer dealer, PlayerList playerList) { | ||
| System.out.println(dealer); | ||
| for (Player player : playerList.getPlayerList()) { | ||
| System.out.println(player); | ||
| } | ||
| System.out.println(""); | ||
| } | ||
|
|
||
| public static void printDrawPlayerCardMessage(Player player) { | ||
| System.out.println(player.getName() + "는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"); | ||
| } | ||
|
|
||
| public static void printPlayerCard(Player player) { | ||
| System.out.println(player); | ||
| } | ||
|
|
||
| public static void printDrawDealerCardMessage() { | ||
| System.out.println("\n딜러는 16이하라 한장의 카드를 더 받았습니다.\n"); | ||
| } | ||
| public static void printNotDrawDealerCardMessage() { | ||
| System.out.println("\n딜러는 16초과라 카드를 뽑지 않습니다.\n"); | ||
| } | ||
|
|
||
| public static void printResultSum(Dealer dealer, PlayerList playerList) { | ||
| System.out.println(dealer + " - 결과: " + dealer.getCardList().getSum()); | ||
| for (Player player : playerList.getPlayerList()) { | ||
| System.out.println(player + " - 결과: " + player.getCardList().getSum()); | ||
| } | ||
| System.out.println(""); | ||
| } | ||
|
|
||
| } |
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.
","로 나누는 것은 입력과 관련된 것이니 ","을 inputView에서 관리하는 것이 어떨까요??