From 5ff21c24ecc6a70a8811d2d5d373f2f31c3efab0 Mon Sep 17 00:00:00 2001 From: Ojimin Date: Sat, 27 Jan 2024 23:54:15 +0900 Subject: [PATCH 1/8] =?UTF-8?q?chore:=20readme=EC=97=90=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=ED=95=A0=20=EA=B8=B0=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 003db037..88ff05e1 100644 --- a/README.md +++ b/README.md @@ -74,3 +74,13 @@ jason: 패 - 기능을 구현하기 전에 java-baseball/docs/README.md 파일에 구현할 기능 목록을 정리해 추가한다. - Git의 커밋 단위는 앞 단계에서 README.md 파일에 정리한 기능 목록 단위로 추가한다. - 커밋 메시지 [컨벤션 가이드](https://gist.github.com/stephenparish/9941e89d80e2bc58a153)를 참고해 커밋 메시지를 작성한다. + +## 구현할 기능 목록 +- 게임에 참여할 사람의 이름 입력받기 +- 딜러와 플레이어들에게 카드 나누기 +- 한장의 카드를 더 부여받을지 구현 + - 딜러 처리 +- 결과 계산 및 출력 +- 최종 승패 계산 및 출력 +- mvc 패턴으로 변경 +- 기능별 test 코드 추가 \ No newline at end of file From cf5eb96793caa8eb3b07560df93ec988bf6e539d Mon Sep 17 00:00:00 2001 From: Ojimin Date: Sun, 28 Jan 2024 15:14:03 +0900 Subject: [PATCH 2/8] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=EC=97=90=20?= =?UTF-8?q?=EC=B0=B8=EC=97=AC=ED=95=A0=20=EC=82=AC=EB=9E=8C=EC=9D=98=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EC=9E=85=EB=A0=A5=EB=B0=9B=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/.gitkeep | 0 src/main/java/Application.java | 19 +++++++++++++++++++ src/main/java/model/Player.java | 13 +++++++++++++ src/main/java/model/PlayerList.java | 15 +++++++++++++++ src/main/java/view/InputView.java | 11 +++++++++++ src/main/java/view/OutputView.java | 9 +++++++++ src/test/java/.gitkeep | 0 8 files changed, 68 insertions(+) delete mode 100644 src/main/java/.gitkeep create mode 100644 src/main/java/Application.java create mode 100644 src/main/java/model/Player.java create mode 100644 src/main/java/model/PlayerList.java create mode 100644 src/main/java/view/InputView.java create mode 100644 src/main/java/view/OutputView.java delete mode 100644 src/test/java/.gitkeep diff --git a/README.md b/README.md index 88ff05e1..bcb00009 100644 --- a/README.md +++ b/README.md @@ -82,5 +82,6 @@ jason: 패 - 딜러 처리 - 결과 계산 및 출력 - 최종 승패 계산 및 출력 +- 에러 처리 - mvc 패턴으로 변경 - 기능별 test 코드 추가 \ No newline at end of file diff --git a/src/main/java/.gitkeep b/src/main/java/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 00000000..b10a68b1 --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,19 @@ +import model.Player; +import view.InputView; +import view.OutputView; + +import java.util.Arrays; +import java.util.List; + +public class Application { + public static void main(String[] args) { + OutputView.printInputPlayerNameMessage(); + String playerNames = InputView.inputPlayerName(); + List playerNameList = Arrays.asList(playerNames.split(",")); + for (String playerName : playerNameList) { + Player player = new Player(playerName); + } + + + } +} diff --git a/src/main/java/model/Player.java b/src/main/java/model/Player.java new file mode 100644 index 00000000..c9ee8e03 --- /dev/null +++ b/src/main/java/model/Player.java @@ -0,0 +1,13 @@ +package model; + +public class Player { + private String name; + + public Player(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/src/main/java/model/PlayerList.java b/src/main/java/model/PlayerList.java new file mode 100644 index 00000000..c12f1a4f --- /dev/null +++ b/src/main/java/model/PlayerList.java @@ -0,0 +1,15 @@ +package model; + +import java.util.List; + +public class PlayerList { + private List playerList; + + public PlayerList(List playerList) { + this.playerList = playerList; + } + + public List getPlayerList() { + return playerList; + } +} diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 00000000..da816435 --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,11 @@ +package view; + +import java.util.Scanner; + +public class InputView { + + public static String inputPlayerName() { + Scanner scanner = new Scanner(System.in); + return scanner.nextLine(); + } +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java new file mode 100644 index 00000000..6adc0c91 --- /dev/null +++ b/src/main/java/view/OutputView.java @@ -0,0 +1,9 @@ +package view; + +public class OutputView { + + public static void printInputPlayerNameMessage() { + System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"); + } + +} diff --git a/src/test/java/.gitkeep b/src/test/java/.gitkeep deleted file mode 100644 index e69de29b..00000000 From 1169f19983070010dba4e595fe7f79f4eaf139c3 Mon Sep 17 00:00:00 2001 From: Ojimin Date: Mon, 29 Jan 2024 11:57:19 +0900 Subject: [PATCH 3/8] =?UTF-8?q?feat:=20dealer=EC=99=80=20player=EC=97=90?= =?UTF-8?q?=EA=B2=8C=20=EC=B9=B4=EB=93=9C=EB=82=98=EB=88=84=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 22 ++++++++++++++++--- src/main/java/model/Card.java | 31 +++++++++++++++++++++++++++ src/main/java/model/Dealer.java | 31 +++++++++++++++++++++++++++ src/main/java/model/Number.java | 33 +++++++++++++++++++++++++++++ src/main/java/model/Player.java | 27 +++++++++++++++++++++++ src/main/java/model/PlayerList.java | 8 +++++++ src/main/java/model/Type.java | 13 ++++++++++++ src/main/java/view/OutputView.java | 15 +++++++++++++ 8 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 src/main/java/model/Card.java create mode 100644 src/main/java/model/Dealer.java create mode 100644 src/main/java/model/Number.java create mode 100644 src/main/java/model/Type.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java index b10a68b1..55fbf5b5 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -1,18 +1,34 @@ +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 playerNameList = Arrays.asList(playerNames.split(",")); - for (String playerName : playerNameList) { - Player player = new Player(playerName); - } + List 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)카드 뽑기 + + //게임 종료 - 결과 출력, 기준 : + + //최종 승패 + } diff --git a/src/main/java/model/Card.java b/src/main/java/model/Card.java new file mode 100644 index 00000000..a08516f9 --- /dev/null +++ b/src/main/java/model/Card.java @@ -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; + } + + public Type getType() { + return type; + } + + public Number getNumber() { + return number; + } + + @Override + public String toString() { + return number.toString()+type.toString(); + } +} diff --git a/src/main/java/model/Dealer.java b/src/main/java/model/Dealer.java new file mode 100644 index 00000000..420747d6 --- /dev/null +++ b/src/main/java/model/Dealer.java @@ -0,0 +1,31 @@ +package model; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class Dealer { + private List cardList; + + public Dealer() { + this.cardList = initCardList(); + } + + /* + 코드 중복 - 수정 + */ + public List initCardList() { + List cardList = new ArrayList<>(); + cardList.add(new Card()); + cardList.add(new Card()); + return cardList; + } + + @Override + public String toString() { + String cards = cardList.stream() + .map(Card::toString) + .collect(Collectors.joining(", ")); + return "딜러: " + cards; + } +} diff --git a/src/main/java/model/Number.java b/src/main/java/model/Number.java new file mode 100644 index 00000000..a497a818 --- /dev/null +++ b/src/main/java/model/Number.java @@ -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)]; + } +} diff --git a/src/main/java/model/Player.java b/src/main/java/model/Player.java index c9ee8e03..92288044 100644 --- a/src/main/java/model/Player.java +++ b/src/main/java/model/Player.java @@ -1,13 +1,40 @@ package model; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + public class Player { private String name; + private List cardList; public Player(String name) { this.name = name; + this.cardList = initCardList(); + } + + /* + 코드 중복- 수정 필요 + */ + public List initCardList() { + List cardList = new ArrayList<>(); + cardList.add(new Card()); + cardList.add(new Card()); + return cardList; } public String getName() { return name; } + + /* + playerlist에 있는 메서드와 구분 필요 + */ + @Override + public String toString() { + String cards = cardList.stream() + .map(Card::toString) + .collect(Collectors.joining(", ")); + return name + "카드:" + cards; + } } diff --git a/src/main/java/model/PlayerList.java b/src/main/java/model/PlayerList.java index c12f1a4f..056d50f1 100644 --- a/src/main/java/model/PlayerList.java +++ b/src/main/java/model/PlayerList.java @@ -1,6 +1,7 @@ package model; import java.util.List; +import java.util.stream.Collectors; public class PlayerList { private List playerList; @@ -12,4 +13,11 @@ public PlayerList(List playerList) { public List getPlayerList() { return playerList; } + + @Override + public String toString() { + return playerList.stream() + .map(Player::getName) + .collect(Collectors.joining(", ")); + } } diff --git a/src/main/java/model/Type.java b/src/main/java/model/Type.java new file mode 100644 index 00000000..ea106e3c --- /dev/null +++ b/src/main/java/model/Type.java @@ -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)]; + } +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 6adc0c91..fe5ae819 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -1,9 +1,24 @@ 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); + } + } + } From b091a8a292b908eea7f4a9ebd89177579e068813 Mon Sep 17 00:00:00 2001 From: Ojimin Date: Mon, 29 Jan 2024 13:11:41 +0900 Subject: [PATCH 4/8] =?UTF-8?q?feat:=20=EB=94=9C=EB=9F=AC=EC=B9=B4?= =?UTF-8?q?=EB=93=9C=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 14 +++++++++-- src/main/java/model/CardList.java | 40 ++++++++++++++++++++++++++++++ src/main/java/model/Dealer.java | 20 ++++++--------- src/main/java/model/Player.java | 18 +++----------- src/main/java/view/OutputView.java | 8 ++++++ 5 files changed, 71 insertions(+), 29 deletions(-) create mode 100644 src/main/java/model/CardList.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java index 55fbf5b5..cb8597c4 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -23,11 +23,21 @@ public static void main(String[] args) { //1)초반 카드 나누기 - 카드 숫자: 하트, 스페이드, 다이아몬드,클로버 => 1~9, A, King, Queen, Jack OutputView.printCardAssignment(playerList); OutputView.printDealerAndPlayerCard(dealer, playerList); - //2)카드 뽑기 + //2)카드 뽑기 - player, 딜러 순 + //player가 n 을 입력하면 현재 player의 카드를 보여주고 다음턴 + + //딜러는 알아서 계산 후 출력 + if (dealer.getCardList().getSum() > 16) { + OutputView.printNotDrawDealerCardMessage(); + } + if (dealer.getCardList().getSum() <=16) { + OutputView.printDrawDealerCardMessage(); + dealer.getCardList().addCard(); + } //게임 종료 - 결과 출력, 기준 : - //최종 승패 + //최종 승패 - 딜러는 플레이어와 대결 느낌? diff --git a/src/main/java/model/CardList.java b/src/main/java/model/CardList.java new file mode 100644 index 00000000..725fc287 --- /dev/null +++ b/src/main/java/model/CardList.java @@ -0,0 +1,40 @@ +package model; + +import java.util.ArrayList; +import java.util.List; + +public class CardList { + private List cardList; + private int sum = 0; + + public CardList() { + this.cardList = initCardList(); + this.sum = calculateSum(this.cardList); + } + + public List initCardList() { + List cardList = new ArrayList<>(); + cardList.add(new Card()); + cardList.add(new Card()); + return cardList; + } + + public int calculateSum(List cardList) { + return cardList.stream() + .mapToInt(card -> card.getNumber().getRank()) + .sum(); + } + + public List getCardList() { + return cardList; + } + + public int getSum() { + return sum; + } + + public void addCard() { + cardList.add(new Card()); + calculateSum(cardList); + } +} diff --git a/src/main/java/model/Dealer.java b/src/main/java/model/Dealer.java index 420747d6..560c4623 100644 --- a/src/main/java/model/Dealer.java +++ b/src/main/java/model/Dealer.java @@ -5,27 +5,21 @@ import java.util.stream.Collectors; public class Dealer { - private List cardList; + private CardList cardList; public Dealer() { - this.cardList = initCardList(); - } - - /* - 코드 중복 - 수정 - */ - public List initCardList() { - List cardList = new ArrayList<>(); - cardList.add(new Card()); - cardList.add(new Card()); - return cardList; + this.cardList = new CardList(); } @Override public String toString() { - String cards = cardList.stream() + String cards = cardList.getCardList().stream() .map(Card::toString) .collect(Collectors.joining(", ")); return "딜러: " + cards; } + + public CardList getCardList() { + return cardList; + } } diff --git a/src/main/java/model/Player.java b/src/main/java/model/Player.java index 92288044..9dd6b7d4 100644 --- a/src/main/java/model/Player.java +++ b/src/main/java/model/Player.java @@ -6,21 +6,11 @@ public class Player { private String name; - private List cardList; + private CardList cardList; public Player(String name) { this.name = name; - this.cardList = initCardList(); - } - - /* - 코드 중복- 수정 필요 - */ - public List initCardList() { - List cardList = new ArrayList<>(); - cardList.add(new Card()); - cardList.add(new Card()); - return cardList; + this.cardList = new CardList(); } public String getName() { @@ -32,9 +22,9 @@ public String getName() { */ @Override public String toString() { - String cards = cardList.stream() + String cards = cardList.getCardList().stream() .map(Card::toString) .collect(Collectors.joining(", ")); - return name + "카드:" + cards; + return name + "카드: " + cards; } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index fe5ae819..bc75d550 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -19,6 +19,14 @@ public static void printDealerAndPlayerCard(Dealer dealer, PlayerList playerList for (Player player : playerList.getPlayerList()) { System.out.println(player); } + System.out.println(""); + } + + public static void printDrawDealerCardMessage() { + System.out.println("딜러는 16이하라 한장의 카드를 더 받았습니다."); + } + public static void printNotDrawDealerCardMessage() { + System.out.println("딜러는 16초과라 카드를 뽑지 않습니다."); } } From 921e07dc154f8c7983f1558f9159b66944451f74 Mon Sep 17 00:00:00 2001 From: Ojimin Date: Mon, 29 Jan 2024 14:54:37 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20player=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EB=BD=91=EA=B8=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/Application.java | 25 ++++++++++++++++++------- src/main/java/model/Player.java | 8 ++++++-- src/main/java/view/InputView.java | 5 +++++ src/main/java/view/OutputView.java | 12 ++++++++++-- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bcb00009..ba58cf92 100644 --- a/README.md +++ b/README.md @@ -83,5 +83,6 @@ jason: 패 - 결과 계산 및 출력 - 최종 승패 계산 및 출력 - 에러 처리 + - 사용자가 y, n 이외에 다른 입력을 했을시 - mvc 패턴으로 변경 - 기능별 test 코드 추가 \ No newline at end of file diff --git a/src/main/java/Application.java b/src/main/java/Application.java index cb8597c4..a247dbbd 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -15,8 +15,8 @@ public static void main(String[] args) { String playerNames = InputView.inputPlayerName(); List playerNameList = Arrays.asList(playerNames.split(",")); List players = playerNameList.stream() - .map(Player::new) - .collect(Collectors.toList()); + .map(Player::new) + .collect(Collectors.toList()); PlayerList playerList = new PlayerList(players); //게임 진행 Dealer dealer = new Dealer(); @@ -25,20 +25,31 @@ public static void main(String[] args) { OutputView.printDealerAndPlayerCard(dealer, playerList); //2)카드 뽑기 - player, 딜러 순 //player가 n 을 입력하면 현재 player의 카드를 보여주고 다음턴 - + for (Player player : playerList.getPlayerList()) { + while (true) { + 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) { + if (dealer.getCardList().getSum() <= 16) { OutputView.printDrawDealerCardMessage(); dealer.getCardList().addCard(); } - //게임 종료 - 결과 출력, 기준 : - - //최종 승패 - 딜러는 플레이어와 대결 느낌? + //게임 종료 - 결과 출력, 기준 : 한 게임 끝나면 + //최종 승패 } diff --git a/src/main/java/model/Player.java b/src/main/java/model/Player.java index 9dd6b7d4..89d26116 100644 --- a/src/main/java/model/Player.java +++ b/src/main/java/model/Player.java @@ -17,9 +17,13 @@ public String getName() { return name; } + public CardList getCardList() { + return cardList; + } + /* - playerlist에 있는 메서드와 구분 필요 - */ + playerlist에 있는 메서드와 구분 필요 + */ @Override public String toString() { String cards = cardList.getCardList().stream() diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java index da816435..9e782b1f 100644 --- a/src/main/java/view/InputView.java +++ b/src/main/java/view/InputView.java @@ -8,4 +8,9 @@ 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(); + } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index bc75d550..4a4a75d0 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -22,11 +22,19 @@ public static void printDealerAndPlayerCard(Dealer dealer, PlayerList playerList 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("딜러는 16이하라 한장의 카드를 더 받았습니다."); + System.out.println("\n딜러는 16이하라 한장의 카드를 더 받았습니다."); } public static void printNotDrawDealerCardMessage() { - System.out.println("딜러는 16초과라 카드를 뽑지 않습니다."); + System.out.println("\n딜러는 16초과라 카드를 뽑지 않습니다."); } } From 0e9293da35f7e5c9ca4a38e434c25c71a0ded2d4 Mon Sep 17 00:00:00 2001 From: Ojimin Date: Mon, 29 Jan 2024 15:08:32 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat:=20=EA=B2=B0=EA=B3=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 1 + src/main/java/model/CardList.java | 6 +++++- src/main/java/view/OutputView.java | 12 ++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/Application.java b/src/main/java/Application.java index a247dbbd..3f8a21f0 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -48,6 +48,7 @@ public static void main(String[] args) { } //게임 종료 - 결과 출력, 기준 : 한 게임 끝나면 + OutputView.printResultSum(dealer, playerList); //최종 승패 diff --git a/src/main/java/model/CardList.java b/src/main/java/model/CardList.java index 725fc287..30e4f96b 100644 --- a/src/main/java/model/CardList.java +++ b/src/main/java/model/CardList.java @@ -35,6 +35,10 @@ public int getSum() { public void addCard() { cardList.add(new Card()); - calculateSum(cardList); + updateSum(); + } + + public void updateSum() { + this.sum = calculateSum(this.cardList); } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 4a4a75d0..03b78e18 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -31,10 +31,18 @@ public static void printPlayerCard(Player player) { } public static void printDrawDealerCardMessage() { - System.out.println("\n딜러는 16이하라 한장의 카드를 더 받았습니다."); + System.out.println("\n딜러는 16이하라 한장의 카드를 더 받았습니다.\n"); } public static void printNotDrawDealerCardMessage() { - System.out.println("\n딜러는 16초과라 카드를 뽑지 않습니다."); + 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(""); } } From 91b80d8efa82ee12574f1ae03659dd7acc6ad31b Mon Sep 17 00:00:00 2001 From: Ojimin Date: Mon, 5 Feb 2024 17:10:00 +0900 Subject: [PATCH 7/8] =?UTF-8?q?feat:=20readme=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?=EB=B0=8F=20=EC=B5=9C=EC=A2=85=EC=8A=B9=ED=8C=A8=20=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 26 +++++++++------- src/main/java/Application.java | 33 +++++++++++++++++++-- src/main/java/controller/BlackJackGame.java | 11 +++++++ src/main/java/view/OutputView.java | 22 ++++++++++++++ 4 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 src/main/java/controller/BlackJackGame.java diff --git a/README.md b/README.md index ba58cf92..ebea473d 100644 --- a/README.md +++ b/README.md @@ -76,13 +76,19 @@ jason: 패 - 커밋 메시지 [컨벤션 가이드](https://gist.github.com/stephenparish/9941e89d80e2bc58a153)를 참고해 커밋 메시지를 작성한다. ## 구현할 기능 목록 -- 게임에 참여할 사람의 이름 입력받기 -- 딜러와 플레이어들에게 카드 나누기 -- 한장의 카드를 더 부여받을지 구현 - - 딜러 처리 -- 결과 계산 및 출력 -- 최종 승패 계산 및 출력 -- 에러 처리 - - 사용자가 y, n 이외에 다른 입력을 했을시 -- mvc 패턴으로 변경 -- 기능별 test 코드 추가 \ No newline at end of file +-[x] 게임에 참여할 사람의 이름 입력받기 +-[ ] 딜러와 플레이어들에게 카드 나누기 + -[ ] 딜러는 카드 1장씩, 플레이어는 2장씩 나누기 +-[x] 한장의 카드를 더 부여받을지 구현 + -[ ] 딜러카드 숫자 계산 시 에이스 카드는 11로 계산 +-[x] 결과 계산 및 출력 +-[x] 최종 승패 계산 및 출력 + -[x] 딜러와 플레이어와 숫자 비교하여 딜러 승패 계산 + -[x] 플레이어 숫자를 딜러와 다른 플레이어와 비교하여 계산 +-[ ] 에러 처리 + -[ ] 사용자가 y, n 이외에 다른 입력을 했을시 + -[ ] 에이스카드 1 or 11 처리 + -[ ] 카드 중복처리 + -[ ] 결과 숫자가 21을 넘을경우 패 처리 +-[ ] mvc 패턴으로 변경 +-[ ] 기능별 test 코드 추가 \ No newline at end of file diff --git a/src/main/java/Application.java b/src/main/java/Application.java index 3f8a21f0..c318ab7f 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -50,8 +50,37 @@ public static void main(String[] args) { //게임 종료 - 결과 출력, 기준 : 한 게임 끝나면 OutputView.printResultSum(dealer, playerList); - //최종 승패 - + //최종 승패 - dealer는 각 player별로 비교하여 승패 기록, 각 player은 딜러와 다른 player와 비교, 그리고 player가 1등이면 승 아니면 패 + //딜러 승패 계산 + int dealerWin = 0; + int dealerLose = 0; + for (Player player : playerList.getPlayerList()) { + if (dealer.getCardList().getSum() > player.getCardList().getSum()) { + dealerWin += 1; + } + if (dealer.getCardList().getSum() < player.getCardList().getSum()) { + dealerLose += 1; + } + } + OutputView.printFinalWin(dealerWin, dealerLose); + //플레이어 승패 계산 + for (Player player: playerList.getPlayerList()) { + boolean isWin = false; + int playerSum = player.getCardList().getSum(); + for (Player otherPlayer : playerList.getPlayerList()) { + if (playerSum < otherPlayer.getCardList().getSum() || playerSum < dealer.getCardList().getSum()) { + isWin = false; + OutputView.printFinalPlayerResult(player, isWin); + break; + } + if (player.getCardList().getSum() > otherPlayer.getCardList().getSum()) { + isWin = true; + } + } + if (isWin && playerSum > dealer.getCardList().getSum()) { + OutputView.printFinalPlayerResult(player,isWin); + } + } } } diff --git a/src/main/java/controller/BlackJackGame.java b/src/main/java/controller/BlackJackGame.java new file mode 100644 index 00000000..97bef56b --- /dev/null +++ b/src/main/java/controller/BlackJackGame.java @@ -0,0 +1,11 @@ +package controller; + +public class BlackJackGame { + + public static void start(){ + + + + + } +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index 03b78e18..b3862f0a 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -45,4 +45,26 @@ public static void printResultSum(Dealer dealer, PlayerList playerList) { System.out.println(""); } + public static void printFinalWin(int dealerWin, int dealerLose){ + System.out.println("## 최종 승패"); + if (dealerWin == 0) { + System.out.println("딜러: " + dealerLose + "패"); + } + if (dealerLose == 0) { + System.out.println("딜러: " + dealerWin + "승"); + } + if (dealerWin != 0 && dealerLose != 0) { + System.out.println("딜러: " + dealerWin + "승 " + dealerLose + "패"); + } + } + + public static void printFinalPlayerResult(Player player, boolean isWin) { + if(isWin) { + System.out.println(player.getName() + ": " + "승"); + } + if(!isWin) { + System.out.println(player.getName() + ": " + "패"); + } + } + } From eebd8bc7e88f3f45b59f08dd27243d348255f15c Mon Sep 17 00:00:00 2001 From: Ojimin Date: Wed, 7 Feb 2024 11:46:37 +0900 Subject: [PATCH 8/8] =?UTF-8?q?feat:=20controller=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 73 +----------- src/main/java/controller/BlackJackGame.java | 118 +++++++++++++++++++- src/main/java/view/OutputView.java | 9 +- 3 files changed, 124 insertions(+), 76 deletions(-) diff --git a/src/main/java/Application.java b/src/main/java/Application.java index c318ab7f..db3d9e25 100644 --- a/src/main/java/Application.java +++ b/src/main/java/Application.java @@ -1,3 +1,4 @@ +import controller.BlackJackGame; import model.Dealer; import model.Player; import model.PlayerList; @@ -11,76 +12,6 @@ public class Application { public static void main(String[] args) { - OutputView.printInputPlayerNameMessage(); - String playerNames = InputView.inputPlayerName(); - List playerNameList = Arrays.asList(playerNames.split(",")); - List 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) { - 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); - - //최종 승패 - dealer는 각 player별로 비교하여 승패 기록, 각 player은 딜러와 다른 player와 비교, 그리고 player가 1등이면 승 아니면 패 - //딜러 승패 계산 - int dealerWin = 0; - int dealerLose = 0; - for (Player player : playerList.getPlayerList()) { - if (dealer.getCardList().getSum() > player.getCardList().getSum()) { - dealerWin += 1; - } - if (dealer.getCardList().getSum() < player.getCardList().getSum()) { - dealerLose += 1; - } - } - OutputView.printFinalWin(dealerWin, dealerLose); - - //플레이어 승패 계산 - for (Player player: playerList.getPlayerList()) { - boolean isWin = false; - int playerSum = player.getCardList().getSum(); - for (Player otherPlayer : playerList.getPlayerList()) { - if (playerSum < otherPlayer.getCardList().getSum() || playerSum < dealer.getCardList().getSum()) { - isWin = false; - OutputView.printFinalPlayerResult(player, isWin); - break; - } - if (player.getCardList().getSum() > otherPlayer.getCardList().getSum()) { - isWin = true; - } - } - if (isWin && playerSum > dealer.getCardList().getSum()) { - OutputView.printFinalPlayerResult(player,isWin); - } - } + BlackJackGame.start(); } } diff --git a/src/main/java/controller/BlackJackGame.java b/src/main/java/controller/BlackJackGame.java index 97bef56b..612e5fc9 100644 --- a/src/main/java/controller/BlackJackGame.java +++ b/src/main/java/controller/BlackJackGame.java @@ -1,11 +1,127 @@ package controller; +import model.Dealer; +import model.Player; +import model.PlayerList; +import view.InputView; +import view.OutputView; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + public class BlackJackGame { - public static void start(){ + public static void start() { + List players = inputPlayer(); + PlayerList playerList = new PlayerList(players); + Dealer dealer = new Dealer(); + assignCard(dealer, playerList); + drawCard(playerList, dealer); + OutputView.printResultSum(dealer, playerList); + getFinalWinLose(dealer, playerList); + } + + private static List inputPlayer() { + OutputView.printInputPlayerNameMessage(); + String playerNames = InputView.inputPlayerName(); + List playerNameList = Arrays.asList(playerNames.split(",")); + return playerNameList.stream() + .map(Player::new) + .collect(Collectors.toList()); + } + + private static void assignCard(Dealer dealer, PlayerList playerList) { + OutputView.printCardAssignment(playerList); + OutputView.printDealerAndPlayerCard(dealer, playerList); + } + + private static void drawCard(PlayerList playerList, Dealer dealer) { + for (Player player : playerList.getPlayerList()) { + drawPlayerCard(player); + } + drawDealerCard(dealer); + } + + private static void drawPlayerCard(Player player) { + while (true) { + OutputView.printDrawPlayerCardMessage(player); + String playerInput = InputView.inputPlayerMoreCard(); + if (playerInput.equals("y")) { + player.getCardList().addCard(); + } + OutputView.printPlayerCard(player); + if (playerInput.equals("n")) { + break; + } + } + } + + private static void drawDealerCard(Dealer dealer) { + if (dealer.getCardList().getSum() > 16) { + OutputView.printNotDrawDealerCardMessage(); + } + if (dealer.getCardList().getSum() <= 16) { + OutputView.printDrawDealerCardMessage(); + dealer.getCardList().addCard(); + } + } + + private static void getFinalWinLose(Dealer dealer, PlayerList playerList) { + calculateDealerWinLose(dealer, playerList); + calculatePlayerWinLose(dealer, playerList); + } + private static void calculateDealerWinLose(Dealer dealer, PlayerList playerList) { + int dealerWin = 0; + int dealerLose = 0; + int dealerSum = dealer.getCardList().getSum(); + for (Player player : playerList.getPlayerList()) { + int playerSum = player.getCardList().getSum(); + dealerWin = getDealerWin(dealerSum, playerSum, dealerWin); + dealerLose = getDealerLose(dealerSum, playerSum, dealerLose); + } + OutputView.printFinalWin(dealerWin, dealerLose); + } + private static int getDealerWin(int dealerSum, int playerSum, int dealerWin) { + if (dealerSum > playerSum) { + dealerWin += 1; + } + return dealerWin; + } + private static int getDealerLose(int dealerSum, int playerSum, int dealerLose) { + if (dealerSum < playerSum) { + dealerLose += 1; + } + return dealerLose; + } + + private static void calculatePlayerWinLose(Dealer dealer, PlayerList playerList) { + int dealerSum = dealer.getCardList().getSum(); + for (Player player : playerList.getPlayerList()) { + boolean isWin = false; + int playerSum = player.getCardList().getSum(); + isWin(playerList, player, dealerSum, isWin); + if (isWin && playerSum > dealerSum) { + OutputView.printFinalPlayerResult(player, isWin); + } + } + } + private static boolean isWin(PlayerList playerList, Player player, int dealerSum, boolean isWin) { + int playerSum = player.getCardList().getSum(); + for (Player otherPlayer : playerList.getPlayerList()) { + if (playerSum < otherPlayer.getCardList().getSum() || playerSum < dealerSum) { + isWin = false; + OutputView.printFinalPlayerResult(player, isWin); + break; + } + if (playerSum > otherPlayer.getCardList().getSum()) { + isWin = true; + } + } + return isWin; } } diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java index b3862f0a..73450715 100644 --- a/src/main/java/view/OutputView.java +++ b/src/main/java/view/OutputView.java @@ -10,7 +10,7 @@ public static void printInputPlayerNameMessage() { System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)"); } - public static void printCardAssignment(PlayerList playerList){ + public static void printCardAssignment(PlayerList playerList) { System.out.println("딜러와 " + playerList.toString() + "에게 2장을 나누었습니다."); } @@ -33,6 +33,7 @@ public static void printPlayerCard(Player player) { public static void printDrawDealerCardMessage() { System.out.println("\n딜러는 16이하라 한장의 카드를 더 받았습니다.\n"); } + public static void printNotDrawDealerCardMessage() { System.out.println("\n딜러는 16초과라 카드를 뽑지 않습니다.\n"); } @@ -45,7 +46,7 @@ public static void printResultSum(Dealer dealer, PlayerList playerList) { System.out.println(""); } - public static void printFinalWin(int dealerWin, int dealerLose){ + public static void printFinalWin(int dealerWin, int dealerLose) { System.out.println("## 최종 승패"); if (dealerWin == 0) { System.out.println("딜러: " + dealerLose + "패"); @@ -59,10 +60,10 @@ public static void printFinalWin(int dealerWin, int dealerLose){ } public static void printFinalPlayerResult(Player player, boolean isWin) { - if(isWin) { + if (isWin) { System.out.println(player.getName() + ": " + "승"); } - if(!isWin) { + if (!isWin) { System.out.println(player.getName() + ": " + "패"); } }