-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: Ojimin
Are you sure you want to change the base?
Changes from all commits
5ff21c2
cf5eb96
1169f19
b091a8a
921e07d
0e9293d
91b80d8
eebd8bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} | ||
} |
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; | ||
} | ||
} |
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; | ||
} | ||
|
||
public Type getType() { | ||
return type; | ||
} | ||
|
||
public Number getNumber() { | ||
return number; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return number.toString()+type.toString(); | ||
} | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. card가 있다면 합은 언제나 결정할 수 있습니다!! |
||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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; | ||
} | ||
} |
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)]; | ||
} | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Player와 Dealer가 생성과 동시에 카드 두 장을 가지게 만든 이유가 있을까요?? |
||
|
||
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; | ||
} | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. toString은 입,출력 문구를 지정하기에는 적절치 못합니다! |
||
} |
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)]; | ||
} | ||
} |
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(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
카드에 Random을 넣지 않고
잘 섞인 고유한 카드들을 가진 Deck 같은 객체를 만들어서 카드들을 반환해주는 것이 어떨까요?