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

[Java-3주차] 유호연 블랙잭 미션 제출합니다. #765

Open
wants to merge 22 commits into
base: main
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ out/

### VS Code ###
.vscode/
/target/
2 changes: 2 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/main/
/test/
9 changes: 6 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ repositories {

dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation platform('org.assertj:assertj-bom:3.25.1')
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation('org.assertj:assertj-core')
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testImplementation 'org.junit.platform:junit-platform-launcher:1.9.3'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
}


java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
Expand All @@ -24,3 +26,4 @@ java {
test {
useJUnitPlatform()
}

2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
43 changes: 43 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>blackjack</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.jupiter.version>5.8.2</junit.jupiter.version>
</properties>

<dependencies>
<!-- JUnit Jupiter API -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>

<!-- JUnit Jupiter Engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Maven Surefire Plugin for running tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
109 changes: 109 additions & 0 deletions src/main/java/BlackJack/BlackjackGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package BlackJack;

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

public class BlackjackGame {

public static int findBlackJack(Dealer dealer, Money m) {
Calculate c;
c = new Calculate(dealer.getCards());
int dealerSum = c.sum();

c = new Calculate(m.getPlayer().getCards());
int playerSum = c.sum();

if(dealerSum == 21 && playerSum == 21) {
m.setResult(m.getMoney());
return 0;
}
if(dealerSum != 21 && playerSum == 21) {
m.setResult((int)(m.getMoney()*1.5));
return 1;
}
return 0;

}

public static void lessTwentyone(String answer,Calculate calculate,Deal deal, Player player, ResultView result) {
if(calculate.sum()<21&&answer.equals("y")) {
deal.dealOutCard(player);
}
}

public static void addCards(Calculate calculate,Deal deal, Player player, InputView input, ResultView result) {
String answer;
do {
answer= input.getPlayerChoice(player.getName());
lessTwentyone(answer,calculate, deal, player, result);
result.printOwnCard(player);
System.out.println();
}while(answer.equals("y")&&calculate.sum()<21);

}

public static void main(String[] args) {

InputView input = new InputView();
String playerNamesInput = input.getPlayerNames();
String[] playerNames = playerNamesInput.split(",");

List<Player> players = new ArrayList<>();
for(String name : playerNames) {
players.add(new Player(name));
}

List<Money> dividends = new ArrayList<>();
for(Player player : players) {
int n=input.getMoney(player);
dividends.add(new Money(player,n));
}

Dealer dealer = new Dealer();
Deal deal = new Deal(players, dealer);

ResultView result = new ResultView();

result.printFirst(playerNamesInput);
deal.dealOutCard(dealer);
deal.dealOutCard(dealer);
for(Player player : players) {
deal.dealOutCard(player);
deal.dealOutCard(player);
}


result.printPlayerCard(dealer, players);
System.out.println();

int bj = 0;
for(Money m : dividends) {
bj += findBlackJack(dealer, m);
}
if(bj!=0) {
result.printResult(dealer, players);
result.finalMoney(dividends, dealer);
return;
}

for(Player player : players) {
Calculate calculate = new Calculate(player.getCards());
System.out.println();
addCards(calculate, deal, player, input, result);

}

System.out.println();
result.addDealer(dealer, deal);
System.out.println();

result.printResult(dealer, players);

/* 1단계
result.finalWinOrLoss(dealer, players);
*/

result.finalMoney(dividends, dealer);
}

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

public class Calculate {

private String[] shape= {"하트", "다이아몬드", "클로버", "스페이드"};
private String[] rank = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
private Card cards;

Calculate(Card cards){
this.cards = cards;
}

public int change(String c) {
char[] StringNum = c.toCharArray();
char num = StringNum[StringNum.length-1];

if(num>=50&&num<=57) {
return num-48;
}

if(num == 'A') {
return 1;
}

return 10;
}

public int sum() {
int sum = 0;
for(String c : cards.getCard()) {
sum+=change(c);
}

return sum;

}


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

import java.util.ArrayList;

public class Card {


private ArrayList<String> cards;

public Card() {
this.cards = new ArrayList<>();
}

public ArrayList<String> getCard() {
return cards;
}

public void addCard(String c) {
cards.add(c);
}


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

import java.util.List;
import java.util.Random;

public class Deal {

private List<Player> players;
private Dealer dealer;
private String[] shape= {"하트", "다이아몬드", "클로버", "스페이드"};
private String[] rank = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
private Card Allcards;
private Card main = new Card();

public Deal(List<Player> players, Dealer dealer) {
this.players = players;
this.dealer = dealer;
}

public String PickCards(Player player) {
Random random = new Random();
int PickShape;
PickShape = random.nextInt(shape.length);

int PickRank;
PickRank = random.nextInt(rank.length);

//테스트를 위한 코드
//PickShape = 0;
//PickRank = 0;

StringBuilder c = new StringBuilder();
c.append(shape[PickShape]);
c.append(rank[PickRank]);

String card = c.toString();



return card;
}

public boolean checkSameCard(String card, Card cards) {
return cards.getCard().contains(card);
}

public String dealOutCard(Player player) {
String card = PickCards(player);
while(checkSameCard(card, player.getCards())||checkSameCard(card, main)) {
card = PickCards(player);
}
player.setCards(card);
main.addCard(card);

return card;
}

}


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


public class Dealer extends Player{

public Dealer(){
super("딜러");
}

public boolean over(Calculate calculate, Deal deal) {
if(calculate.sum()<17) {
deal.dealOutCard(this);
return true;
}
return false;
}
}
49 changes: 49 additions & 0 deletions src/main/java/BlackJack/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package BlackJack;

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

public class InputView {

static Scanner sc = new Scanner(System.in);

public static String getPlayerNames() {
System.out.println("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)");
return sc.next();
}

public String getPlayerChoice(String playerName) {
String choice;
while (true) {
System.out.println(playerName + "는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)");
choice = sc.next();
if (choice.equalsIgnoreCase("y") || choice.equalsIgnoreCase("n")) {
break;
}
if (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n")) {
System.out.println("잘못된 입력입니다. 예는 y, 아니오는 n 중 하나를 입력하세요.");
}
}
return choice;
}

public int getMoney(Player player) {
int money = 0;
while (true) {
System.out.println(player.getName() + "의 배팅 금액은?");
try {
money = sc.nextInt();
if (money > 0) {
break;
} else {
System.out.println("배팅 금액은 0보다 커야 합니다. 다시 입력하세요.");
}
} catch (InputMismatchException e) {
System.out.println("잘못된 입력입니다. 숫자를 입력하세요.");
sc.next();
}
}
return money;
}
}
Loading