Skip to content
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

[블랙잭 게임 미션-1단계] 오지민 제출합니다 #6

Open
wants to merge 8 commits into
base: Ojimin
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,21 @@ jason: 패
- 기능을 구현하기 전에 java-baseball/docs/README.md 파일에 구현할 기능 목록을 정리해 추가한다.
- Git의 커밋 단위는 앞 단계에서 README.md 파일에 정리한 기능 목록 단위로 추가한다.
- 커밋 메시지 [컨벤션 가이드](https://gist.github.com/stephenparish/9941e89d80e2bc58a153)를 참고해 커밋 메시지를 작성한다.

## 구현할 기능 목록
-[x] 게임에 참여할 사람의 이름 입력받기
-[ ] 딜러와 플레이어들에게 카드 나누기
-[ ] 딜러는 카드 1장씩, 플레이어는 2장씩 나누기
-[x] 한장의 카드를 더 부여받을지 구현
-[ ] 딜러카드 숫자 계산 시 에이스 카드는 11로 계산
-[x] 결과 계산 및 출력
-[x] 최종 승패 계산 및 출력
-[x] 딜러와 플레이어와 숫자 비교하여 딜러 승패 계산
-[x] 플레이어 숫자를 딜러와 다른 플레이어와 비교하여 계산
-[ ] 에러 처리
-[ ] 사용자가 y, n 이외에 다른 입력을 했을시
-[ ] 에이스카드 1 or 11 처리
-[ ] 카드 중복처리
-[ ] 결과 숫자가 21을 넘을경우 패 처리
-[ ] mvc 패턴으로 변경
-[ ] 기능별 test 코드 추가
Empty file removed src/main/java/.gitkeep
Empty file.
17 changes: 17 additions & 0 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import controller.BlackJackGame;
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) {
BlackJackGame.start();
}
}
127 changes: 127 additions & 0 deletions src/main/java/controller/BlackJackGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +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() {
List<Player> 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<Player> inputPlayer() {
OutputView.printInputPlayerNameMessage();
String playerNames = InputView.inputPlayerName();
List<String> 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;
}
}
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
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
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
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
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
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
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();
}
}
Loading