Skip to content
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
33 changes: 33 additions & 0 deletions src/main/java/blackjack/BlackJackApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package blackjack;

import blackjack.domain.person.Dealer;
import blackjack.domain.person.Participants;
import blackjack.domain.person.Player;
import blackjack.view.InputView;

import java.util.List;

public class BlackJackApplication {
private static final InputView inputView = new InputView();

public static void main(String[] args) throws Exception {
// player name과 배팅 금액 설정 후 객체 생성
List<Player> players = inputView.makePlayers();
Participants participants = new Participants(players);

// 모두에게 카드 2장 할당
participants.initCards();

// player에게 카드 추가로 뽑을지 여부 선택하도록 함
participants.getMoreCard(inputView);

// 모두의 카드 패, 총점 출력
System.out.println(participants.getResultCards());

// 승패 여부 가린 후 최종 수익 계산
participants.match();

// 최종 수익 출력
System.out.println(participants.getIncomes());
}
}
77 changes: 77 additions & 0 deletions src/main/java/blackjack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 블랙잭
## 게임 규칙
- 딜러와 플레이어 중 카드의 합이 21 또는 21에 가장 가까운 숫자를 가지는 쪽이 배팅 금액을 획득
- 딜러와 플레이어는 1:1로 경기 진행
- 게임 시작 시 모두에게 2장의 카드를 지급
- 플레이어
- 2장의 카드 합이 21 미만일 경우 추가로 1장씩 뽑을 수 있음
- 카드 추가로 뽑아 21 초과 시 배팅금액 모두 상실
- 딜러
- 2장의 카드 합이 16 이하이면 카드 1장 추가
- 2장의 카드 합이 17 이상이면 동작 없음
- 처음 두 장의 카드 합이 21일 경우 블랙잭이 되면 베팅 금액의 1.5 배를 딜러에게 받는다.
- 딜러와 플레이어가 모두 두 카드 합이 21인 경우 플레이어는 베팅한 금액을 돌려받는다.
- 딜러가 21 초과 시 남은 플레이어는 무조건 승리해 배팅 금액 획득

## 설계
### CardFactory
상태
- cardPool을 가진다.

행동
- cardPool을 생성하여 보관한다.
- cardPool에서 한 개의 card를 꺼내주고, 중복 방지를 위해 제거한다.

### Card
상태
- 모양과 숫자를 가진다.

행동
- K,Q,J라면 10을, 2~9 사이라면 해당 값을, A라면 1을 반환한다.
- +) A일 경우에 값을 11로 갖는 경우 체크는 Cards에서 대신한다.

### Cards
상태
- 카드 목록을 가진다.
- 새로운 카드를 가져올 수 있는 cardFactory를 가진다.
- 카드 전체의 값을 저장한다.

행동
- 카드 목록에 카드를 추가할 수 있다.
- 카드 전체의 값을 반환할 수 있다.

### Person
상태
- Cards 객체 보유하여 카드 목록과 카드의 총점 정보를 가진다.
- 게임의 승패여부를 가짐

행동
- 카드 총점을 반환할 수 있다.
- 새 카드를 뽑을 수 있는지 판별한다.

### Dealer
상태
- 플레이어별 게임 결과에 따른 총 수익 금액을 가진다.

행동
- 16이하라면 새 카드를 뽑는다.
- 수익 금액을 증감한다.
- 21 초과 시 무조건 Lose

### Player
상태
- 배팅 금액을 가진다.
- 수익 금액을 가진다.

행동
- 딜러와 매칭하여 승패를 가린 후 Status 변경
- Status에 따라 수익 금액을 증감한다.

### Participants
상태
- 참가자 목록(플레이어들 + 딜러)을 가진다.

행동
- 모든 플레이어와 딜러의 승패를 가린다.
- 딜러와 플레이어의 수익 금액을 최종 결정한다.

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

public class Card {
private final String name;
private final Character number;

public Card(String name, Character number) {
this.name = name;
this.number = number;
}

public int getNumber() throws Exception {
if(number>= '2' && number <= '9') {
return number-'0';
}
if(number == 'K' || number == 'Q' || number == 'J') {
return 10;
}
if(number == 'A') {
return 1; //if card's total value is less than 11, then add 10 additionally.
}
throw new Exception("can't return value of number");
}

@Override
public String toString() {
return number + name;
}
}
33 changes: 33 additions & 0 deletions src/main/java/blackjack/domain/card/CardFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package blackjack.domain.card;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class CardFactory {
private final List<Card> cardPool;

public CardFactory() {
this.cardPool = initCardPool();
}

protected List<Card> initCardPool() {
List<Card> cardList = new ArrayList<>();
List<String> figures = Arrays.asList("스페이드", "클로버", "하트", "다이아몬드");
List<Character> numbers = Arrays.asList('2','3','4','5','6','7','8','9','K','Q','J','A');
for (String figure:figures) {
for (Character number:numbers) {
cardList.add(new Card(figure, number));
}
}
return cardList;
}

public Card selectCard(){
int index = new Random().nextInt(this.cardPool.size());
Card card = cardPool.get(index);
cardPool.remove(index);
return card;
}
}
36 changes: 36 additions & 0 deletions src/main/java/blackjack/domain/card/Cards.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package blackjack.domain.card;

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

public class Cards {
private final List<Card> cardList;
private int total;

public Cards() {
this.cardList = new ArrayList<>();
this.total = 0;
}

public String getAllCards() {
return cardList.toString();
}

public void getMoreCard(CardFactory cardFactory) throws Exception {
Card card = cardFactory.selectCard();
cardList.add(card);
Integer value = card.getNumber();
if(total < 11 && value==1){
total += 10; //handle if a number of card is 'A'
}
total += card.getNumber();
}

public int getTotal(){
return total;
}

public int getSize() {
return this.cardList.size();
}
}
31 changes: 31 additions & 0 deletions src/main/java/blackjack/domain/person/Dealer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package blackjack.domain.person;

public class Dealer extends Person{

public Dealer() {
super();
}

@Override
public boolean needMoreCard() {
return this.getTotal() <= 16;
}

@Override
public String getAllCards() {
return "딜러 카드: "+cards.getAllCards();
}

@Override
public String getIncome() {
return "딜러: " + this.income +"\n";
}

public boolean exceedMAX() {
if(this.getTotal() > MAX_NUM){
this.changeStatus(Status.LOSE);
return true;
}
return false;
}
}
77 changes: 77 additions & 0 deletions src/main/java/blackjack/domain/person/Participants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package blackjack.domain.person;

import blackjack.domain.card.CardFactory;
import blackjack.view.InputView;

import java.util.List;

public class Participants {
private final CardFactory cardFactory;
private final Dealer dealer;
private final List<Player> players;

public Participants(List<Player> players) {
this.cardFactory = new CardFactory();
this.dealer = new Dealer();
this.players = players;
}

public void initCards() throws Exception {
dealer.initCards(cardFactory);
for(Player player : players){
player.initCards(cardFactory);
}
}

public void getMoreCard(InputView inputView) throws Exception {
//player 카드 추가 할/말
for(Player player : players){
while(player.needMoreCard()) {
if(inputView.wantMoreCard(player)) {
player.getMoreCard(cardFactory);
System.out.println(player.getAllCards());
continue;
}
break;
}
}

// dealer 카드 한장 추가 할/말
if(dealer.needMoreCard()){
dealer.getMoreCard(cardFactory);
System.out.println("딜러는 16이하라 한장의 카드를 더 받았습니다.");
}
}

public void match() {
// 딜러가 21 초과시 모든 플레이어 승리
if (dealer.getTotal()>21){
for(Player player : players) {
player.win(dealer);
dealer.changeStatus(Status.LOSE);
}
}

for(Player player : players) {
player.match(dealer);
}
}

public String getResultCards() {
String ret = "";
ret += dealer.getAllCards() + " - 결과: " +dealer.getTotal() + "\n";
for(Player player : players) {
ret += player.getAllCards() + " - 결과: " +player.getTotal() + "\n";
}
return ret;
}

public String getIncomes() {
String ret = "";
ret += dealer.getIncome();
for(Player player : players) {
ret += player.getIncome();
}
return ret;
}
}
47 changes: 47 additions & 0 deletions src/main/java/blackjack/domain/person/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package blackjack.domain.person;

import blackjack.domain.card.CardFactory;
import blackjack.domain.card.Cards;

public abstract class Person {
protected static final int MAX_NUM = 21;
protected final Cards cards;
protected double income;
private Status status;

public Person() {
this.cards = new Cards();
this.income = 0;
this.status = Status.PROCESSING;
}

protected int getTotal() {
return cards.getTotal();
}

public void changeStatus(Status status) {
this.status = status;
}

public Status getStatus() {
return status;
}
public void initCards(CardFactory cardFactory) throws Exception {
getMoreCard(cardFactory);
getMoreCard(cardFactory);
}

public void getMoreCard(CardFactory cardFactory) throws Exception {
cards.getMoreCard(cardFactory);
}

public abstract boolean needMoreCard();

public abstract String getAllCards();

public abstract String getIncome();

public void addIncome(double income){
this.income += income;
}
}
Loading