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

[블랙잭 게임 미션] 김대욱 미션 제출합니다. #2

Open
wants to merge 23 commits into
base: MarriedSenior
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cb1ce76
docs: 기능 요구 사항
MarriedSenior Jan 24, 2024
6005b68
docs: 추상화로 파일 먼저 만들기
MarriedSenior Jan 24, 2024
6410966
feat: Inputview 클래스 생성
MarriedSenior Jan 24, 2024
a1d3103
누락 방지
MarriedSenior Jan 24, 2024
dab31ea
docs: 추상화 클래스 만들기
MarriedSenior Jan 27, 2024
3a7d2e5
feat: Card.java 구현
MarriedSenior Jan 27, 2024
e2d205d
CardGenerator.java 구현
MarriedSenior Jan 27, 2024
6b61c4f
feat: CardDeck.java
MarriedSenior Jan 27, 2024
9275a17
feat: Player.java 구현
MarriedSenior Jan 27, 2024
d8ee674
refactor: Player.java 수정
MarriedSenior Jan 27, 2024
603bc1c
refactor: Dealer.java 수정
MarriedSenior Jan 27, 2024
c791a65
refactor: Card.java 수정
MarriedSenior Jan 27, 2024
e056d31
refactor: ScaoreCounter 수정
MarriedSenior Jan 27, 2024
323a922
feat: BlackJackGame.java 구현중
MarriedSenior Jan 27, 2024
61d4d4f
refactor: BlackJackGame 구현중.2
MarriedSenior Jan 27, 2024
7f72de0
refactor: Player.java 수정
MarriedSenior Jan 28, 2024
dab21cf
refactor: BlackJackGame.java 수정
MarriedSenior Jan 28, 2024
1c05a50
feat: BlackJackController.java 구현중
MarriedSenior Jan 28, 2024
4d5ea76
refactor: BlackJackGame.java 개행 추가
MarriedSenior Jan 30, 2024
2a9723b
refactor: BlackJackGame.java 수정
MarriedSenior Jan 30, 2024
2cf308c
refactor: CardDeck.java와CardGenerator 통합
MarriedSenior Jan 30, 2024
dc7e71f
refactor: Player.java 생성자 수정
MarriedSenior Jan 30, 2024
cb90ca1
refactor: Player.java 생성자 수정2
MarriedSenior Jan 30, 2024
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
76 changes: 0 additions & 76 deletions README.md

This file was deleted.

26 changes: 26 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 기능 목록 - 블랙잭 게임

## 입력
- 게임에 참가할 플레이어의 이름을 입력받는다 (쉼표 기준으로 분리)
- 플레이어가 카드를 더 받을지 받지 않을지 y 혹은 n으로 입력받는다.

## 입력시 예외처리
- 플레이어의 이름을 입력받을 때 타입이 맞지 않으면 예외처리
Copy link
Member

Choose a reason for hiding this comment

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

타입이 맞지 않는다라는 것은 구현 세부적인 내용이네요! 다른 말로는 개발자가 아니라면 '타입이 뭐지?!' 라는 생각이 들 수 있습니다.

기능 목록은 정말로 광화문에 있는 사람 아무나 데려왔을 때에도 이해하는게 가능할 정도로 쉽게 작성하도록 연습하면 좋습니다!

'프로그램을 위한 기능 명세서'를 작성한다기보다, '서비스에 대한 기능 명세서'를 작성한다는 느낌으로 해보시면 좋을 것 같아요 😄

- 플레이어가 카드를 더 받을지 유무를 입력받을 때 y 혹은 n이 아니면 예외처리

## 블랙잭 게임 구현
- 카드의 숫자 계산은 카드 숫자를 기본으로 ace는 1 또는 11로 계산한다.
- king, queen, jack은 각각 10으로 계산한다.
- 게임을 시작하면 플레이어는 두장의 카드를 지급 받으며, 두 장의 카드 숫자가 21을 초과하지 않을 경우에는 카드를 더 뽑을 수 있음.
- 딜러는 처음에 받은 2장의 합계가 16이하면 반드시 1장의 카드를 추가로 뽑음.
- 게임의 승패는 딜러보다 높으면서 카드 숫자의 총합이 21을 넘지 않음으로 구분.

## 출력
- 게임에 참여할 사람의 입력하라는 문구 출력
- 플레이 시작시 2장을 나누었다는 문구 출력
- 플레이 시작시 딜러와 플레이어들이 받은 카드들을 출력
- 각 플레이어가 카드를 더 받을지를 입력하라는 문구 출력
- 딜러와 플레이어들이 카드를 받을 때 마다 각자의 카드 소유 현황을 출력.
- 딜러가 16이하라 카드를 한장 더 받는 경우 그 문구를 출력.
- 게임의 결과 딜러와 플레이어들의 카드 소유 현황과 점수의 합을 출력.
- 최종 승패를 출력.
Comment on lines +11 to +26
Copy link
Member

Choose a reason for hiding this comment

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

기능목록 저번 미션과 비교해서 정말 많이 좋아졌습니다!

이제 구조 자체는 손볼 곳이 없다는 생각이 듭니다! 💯

9 changes: 9 additions & 0 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import controller.BlackJackController;
import view.input.InputView;
import view.output.ResultView;

public class Application {
public static void main(String[] args) {
new BlackJackController(new InputView(), new ResultView()).start();
}
}
36 changes: 36 additions & 0 deletions src/main/java/controller/BlackJackController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package controller;

import domain.BlackJackGame;
import view.input.InputView;
import view.output.ResultView;

import java.util.List;

public class BlackJackController {

private final InputView inputView;
private final ResultView resultView;
private BlackJackGame blackJackGame;

public BlackJackController(InputView inputView, ResultView resultView){
this.inputView = inputView;
this.resultView = resultView;
}

public void start(){

}

public void makeBlackJackGame(List<String> playerNames){
this.blackJackGame = new BlackJackGame(playerNames);
}
public void startBlackJackGame(){
blackJackGame.startPhase();
blackJackGame.playPhase(inputView.readGetCardMoreOrNot());
blackJackGame.dealerPhase();
}

public void showResultBlackJackGame(){

}
}
67 changes: 67 additions & 0 deletions src/main/java/domain/BlackJackGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package domain;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class BlackJackGame {
private Dealer dealer;
private List<Player> players;
private CardDeck cardDeck;
private List<Player> winners;

public BlackJackGame(List<String> splitPlayerNames) {
players = splitPlayerNames.stream()
.map(playerNames -> new Player(playerNames))
.collect(Collectors.toList());
dealer = new Dealer();
cardDeck = new CardDeck();
winners = new ArrayList<>();
cardDeck.generateCards();
}

public void startPhase() {
dealer.receiveCard(cardDeck.draw());
dealer.receiveCard(cardDeck.draw());
for (Player player : players) {
player.receiveCard(cardDeck.draw());
player.receiveCard(cardDeck.draw());
}
}
public void playPhase(String getCardMoreOrNot){
for (Player player : players){
eachPlayerPhase(player, getCardMoreOrNot);
}
}
public void eachPlayerPhase(Player player, String getCardMoreOrNot) {
String answer = "y";
if(player.getScore() > 17) return;
while(answer.equals("y") || player.getScore() <= 21){
answer = getCardMoreOrNot;
if(answer.equals("n")){
break;
}
player.receiveCard(cardDeck.draw());
}
}

public void dealerPhase(){
if (dealer.getScore() <= 16)
dealer.receiveCard(cardDeck.draw());
}

public List<Player> getWinner(){
gattherWinner(players);
return winners;
}
Comment on lines +53 to +56
Copy link
Member

Choose a reason for hiding this comment

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

딜러의 승패도 출력되어야 하니, 추후 getDealerStatus 등과 같은 메소드가 필요하겠네요!

public void gattherWinner(List<Player> players){
for(Player player : players){
if(player.getScore() >dealer.getScore()){
player.isWinner();
this.winners.add(player);
}
}
}


}
45 changes: 45 additions & 0 deletions src/main/java/domain/Card.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package domain;

public class Card {
private String pattern;
private String mark;
private int number;

Card(String pattern, String mark, int number) {
this.pattern = pattern;
this.mark = mark;
this.number = number;
}

Card(String pattern, int index) {
this.pattern = pattern;
this.mark = indexToMark(index);
this.number = indexToNumber(index);
}

private String indexToMark(int index) {
if (number == 1) {
return "A";
}
if (number == 11) {
return "J";
}
if (number == 12) {
return "Q";
}
if (number == 13) {
return "K";
}
return String.valueOf(index);
}

private int indexToNumber(int index) {
if (index > 10) return 10;
return number;
}

public int getNumber(){
return number;
}

}
35 changes: 35 additions & 0 deletions src/main/java/domain/CardDeck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package domain;

import java.util.ArrayList;
import java.util.List;

public class CardDeck {
private List<Card> deck;
private static final String[] PATTERNS = {"스페이드", "하트", "클로버", "다이아몬드"};
private static final int MAX_INDEX = 13;

public CardDeck() {
deck = new ArrayList<>();
}

public Card draw() {
Card selectedCard = getRandomCard();
deck.remove(selectedCard);
return selectedCard;
}

public Card getRandomCard(){
int selectedCardIndex = (int) (Math.random() * deck.size());
return deck.get(selectedCardIndex);
}
public void generateCards() {
for (String pattern : PATTERNS) {
for (int i = 1; i <= MAX_INDEX; i++) {
Card card = new Card(pattern, i);
deck.add(card);
}
}
}


}
11 changes: 11 additions & 0 deletions src/main/java/domain/Dealer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package domain;

public class Dealer extends Player {
Dealer(){
super("Dealer");
}

public Card getStartCard(){
return this.getPlayerCardList().get(0);
}
}
43 changes: 43 additions & 0 deletions src/main/java/domain/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package domain;

import java.util.ArrayList;
import java.util.List;

public class Player {
private final String playerName;
private final List<Card> playerCardList;
private int score;
private boolean winOrNot;

public Player(String playerName){
this.playerName = playerName;
this.playerCardList = new ArrayList<>();
this.score = 0;
this. winOrNot = false;
}

public void receiveCard(Card receivedCard){
playerCardList.add(receivedCard);
}

public String getPlayerName(){
return this.playerName;
}

public List<Card> getPlayerCardList(){
return this.playerCardList;
}

public int getScore() {
this.calculateScore();
return score;
}
public void calculateScore(){
for (Card card : playerCardList){
score += card.getNumber();
}
}
public void isWinner(){
this.winOrNot = true;
}
}
31 changes: 31 additions & 0 deletions src/main/java/view/input/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package view.input;

import java.util.Scanner;
import java.util.List;

public class InputView {
private final String PLAYER_NAMES_INPUT_MESSAGE = "게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)";
private final String PLAYER_GET_CARD_INPUT_MESSAGE = "%s는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)";

private final Scanner scanner;
public InputView() {
this.scanner = new Scanner(System.in);
}

public List<String> readPlayerName(){
System.out.println(PLAYER_NAMES_INPUT_MESSAGE);

final String delimiter = ",";
String playerNames = scanner.nextLine();

return List.of(playerNames.split(delimiter));
}

public String readGetCardMoreOrNot(String player){
System.out.printf(PLAYER_GET_CARD_INPUT_MESSAGE, player);

String yesOrNo = scanner.nextLine();

return yesOrNo;
}
}
4 changes: 4 additions & 0 deletions src/main/java/view/output/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package view.output;

public class ResultView {
}