Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ jason: 패
- 기능을 구현하기 전에 java-baseball/docs/README.md 파일에 구현할 기능 목록을 정리해 추가한다.
- Git의 커밋 단위는 앞 단계에서 README.md 파일에 정리한 기능 목록 단위로 추가한다.
- 커밋 메시지 [컨벤션 가이드](https://gist.github.com/stephenparish/9941e89d80e2bc58a153)를 참고해 커밋 메시지를 작성한다.

## 구현할 기능 목록
- 게임에 참여할 사람의 이름 입력받기
- 딜러와 플레이어들에게 카드 나누기
- 한장의 카드를 더 부여받을지 구현
- 딜러 처리
- 결과 계산 및 출력
- 최종 승패 계산 및 출력
- 에러 처리
- 사용자가 y, n 이외에 다른 입력을 했을시
- mvc 패턴으로 변경
- 기능별 test 코드 추가
Empty file removed src/main/java/.gitkeep
Empty file.
57 changes: 57 additions & 0 deletions src/main/java/Application.java
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(","));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

","로 나누는 것은 입력과 관련된 것이니 ","을 inputView에서 관리하는 것이 어떨까요??

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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);

//최종 승패


}
}
31 changes: 31 additions & 0 deletions src/main/java/model/Card.java
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

카드에 Random을 넣지 않고
잘 섞인 고유한 카드들을 가진 Deck 같은 객체를 만들어서 카드들을 반환해주는 것이 어떨까요?

}

public Type getType() {
return type;
}

public Number getNumber() {
return number;
}

@Override
public String toString() {
return number.toString()+type.toString();
}
}
44 changes: 44 additions & 0 deletions src/main/java/model/CardList.java
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

card가 있다면 합은 언제나 결정할 수 있습니다!!
그래서 sum은 cardList로 부터 추론할 수 있는 값이기에 불필요한 필드입니다 ㅎㅎ
오히려 sum을 두는 것이 실제 card 합과 sum의 합이 다른 정합성의 문제가 발생할 수 있는 것이죠!


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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);
}
}
25 changes: 25 additions & 0 deletions src/main/java/model/Dealer.java
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;
}
}
33 changes: 33 additions & 0 deletions src/main/java/model/Number.java
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)];
}
}
34 changes: 34 additions & 0 deletions src/main/java/model/Player.java
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Player와 Dealer가 생성과 동시에 카드 두 장을 가지게 만든 이유가 있을까요??
giveCard(List cards) 와 같은 메서드를 둘 수도 있었을 것 같아요!!


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;
}
}
23 changes: 23 additions & 0 deletions src/main/java/model/PlayerList.java
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toString은 입,출력 문구를 지정하기에는 적절치 못합니다!
도메인에 출력의 책임이 추가되어 SRP를 위반하기 때문이죠!! 참고 글

}
13 changes: 13 additions & 0 deletions src/main/java/model/Type.java
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)];
}
}
16 changes: 16 additions & 0 deletions src/main/java/view/InputView.java
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();
}
}
48 changes: 48 additions & 0 deletions src/main/java/view/OutputView.java
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("");
}

}
Empty file removed src/test/java/.gitkeep
Empty file.