From cf6e7a7dbdcc985db8a874bb2ba9842a876cbf28 Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Sat, 27 Jan 2024 22:47:56 +0900 Subject: [PATCH 01/11] =?UTF-8?q?1=EB=B2=88=EC=A7=B8=20=EB=A6=AC=ED=80=98?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/.DS_Store | Bin 0 -> 6148 bytes src/main/java/blackjack/Application.java | 60 +++++++++++ src/main/java/blackjack/Between16And21.java | 39 +++++++ src/main/java/blackjack/CardDictionary.java | 40 +++++++ .../java/blackjack/CardGameSimulator.java | 98 ++++++++++++++++++ src/main/java/blackjack/DecideResult.java | 21 ++++ 6 files changed, 258 insertions(+) create mode 100644 src/main/java/blackjack/.DS_Store create mode 100644 src/main/java/blackjack/Application.java create mode 100644 src/main/java/blackjack/Between16And21.java create mode 100644 src/main/java/blackjack/CardDictionary.java create mode 100644 src/main/java/blackjack/CardGameSimulator.java create mode 100644 src/main/java/blackjack/DecideResult.java diff --git a/src/main/java/blackjack/.DS_Store b/src/main/java/blackjack/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0> Cards = CardDictionary.createCardDictionary(); + + List participants = getInput("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)", s -> Arrays.asList(s.split(","))); + ParticipantSize(participants); //예외 처리 + + Collections.shuffle(participants); + + Map participantStates = CardGameSimulator.simulateCardGame(participants, Cards); + + for (String participant : participants) { + for (Map.Entry entry: participantStates.entrySet()) { + Between16And21.SumBetween16And21(participant, entry, Cards); + } + } + String winner = DecideWinner.determineWinner(participantStates); + System.out.println("## 최종 승패"); + System.out.println("딜러: "); + for (String participant : participants) { + System.out.println(participant + ": " + ); + } + + } + + private static T getInput(String prompt, Function parser) { + T participants; + + Scanner scanner = new Scanner(System.in); + System.out.println(prompt); + String userinput = scanner.nextLine(); + participants = parser.apply(userinput); + return participants; + } + + private static void ParticipantSize(List participants) { + if (participants.size() > 25) { + throw new IllegalArgumentException("가능한 참가 인원을 초과하였습니다."); + } + } + + + + + +} + diff --git a/src/main/java/blackjack/Between16And21.java b/src/main/java/blackjack/Between16And21.java new file mode 100644 index 000000000..e65838a7a --- /dev/null +++ b/src/main/java/blackjack/Between16And21.java @@ -0,0 +1,39 @@ +package blackjack; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +public class Between16And21 { + public static void SumBetween16And21(String participant, Map.Entry participantEntry, Map> Cards) { + while(true) { + CardGameSimulator.ParticipantState participantState = participantEntry.getValue(); + int sum = participantState.getSum(Cards); + + if (sum < 16 || sum >=21) { + break; + } + + if (sum > 16 && sum < 21) { + System.out.println(participant+ "는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"); + Scanner scanner = new Scanner(System.in); + String answer = scanner.nextLine(); + + if("y".equalsIgnoreCase(answer)) { + Map.Entry extraCard = CardGameSimulator.getRandomCard(Cards); + int extraCardValue = extraCard.getValue(); + String extraCardName = extraCard.getKey(); + participantState.updateSumAndNameList(extraCardValue, extraCardName); + + System.out.println(participant + "카드: " + participantState.printlist()); + //16과 21인지 판단하고 만약 중간이라면 다시 SumBetween16and21 실행 아니면 continue + } else if ("n".equalsIgnoreCase(answer)) { + System.out.println(participant + "카드: " + participantState.printlist()); + break; + + } + } + } + } +} diff --git a/src/main/java/blackjack/CardDictionary.java b/src/main/java/blackjack/CardDictionary.java new file mode 100644 index 000000000..f59eabcb4 --- /dev/null +++ b/src/main/java/blackjack/CardDictionary.java @@ -0,0 +1,40 @@ +package blackjack; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.ArrayList; + +public class CardDictionary { + public static Map> createCardDictionary() { + Map> Cards = new HashMap<>(); + + for (int i = 2; i <= 10; i++) { + for (String suit : new String[]{"하트", "스페이드", "클로버", "다이아몬드"}) { + String cardName = i + suit; + List values = new ArrayList<>(); + values.add(i); + Cards.put(cardName, values); + } + } + + for (String suit : new String[]{"하트", "스페이드", "클로버", "다이아몬드"}) { + for (String face : new String[]{"J", "Q", "K"}) { + String cardName = face + suit; + List values = new ArrayList<>(); + values.add(10); + Cards.put(cardName, values); + } + } + + for (String suit : new String[]{"하트", "스페이드", "클로버", "다이아몬드"}) { + String cardName = suit + "Ace"; + List values = new ArrayList<>(); + values.add(1); + values.add(11); + Cards.put(cardName, values); + } + return Cards; //HashMap [[key: 카드 이름, value = 카드 숫자]] + } +} +// Making functional interface!!!!!!!!! \ No newline at end of file diff --git a/src/main/java/blackjack/CardGameSimulator.java b/src/main/java/blackjack/CardGameSimulator.java new file mode 100644 index 000000000..b9528e40f --- /dev/null +++ b/src/main/java/blackjack/CardGameSimulator.java @@ -0,0 +1,98 @@ +package blackjack; + +import java.util.*; + + +public class CardGameSimulator { + + public static class ParticipantState { + private int sum; + private List cardlist; + + public ParticipantState() { + this.sum = 0; + this.cardlist = new ArrayList<>(); + } + + public int printsum() { + return this.sum; + } + + public List printlist() { + return this.cardlist; + } + + public int getSum(Map> Cards) { + + CardOperation sumOperation = shuffledCard -> { + int value = shuffledCard.getValue(); + this.sum += value; + }; + + performCardOperation(Cards, sumOperation); + + return this.sum; + } + + public void updateSumAndNameList(int value, String cardNames) { + this.sum += value; + this.cardlist.add(cardNames); + } + } + + public static Map simulateCardGame(List participants, Map> Cards) { + Map participantStates = new HashMap<>(); + + for(String participant : participants) { + participantStates.put(participant, new ParticipantState()); + } + + for (String participant : participants) { + ParticipantState participantState = participantStates.get(participant); + performCardOperation(Cards, shuffledCard -> { + int value = shuffledCard.getValue(); + String cardName = shuffledCard.getKey(); + participantState.updateSumAndNameList(value, cardName); + }); + } + return participantStates; + + } + + interface CardOperation { + void operate(Map.Entry shuffledCard); + } + + private static void performCardOperation(Map> Cards, CardOperation operation) { + for(int i = 0; i < 2; i++) { + Map.Entry shuffledCard = getRandomCard(Cards); + operation.operate(shuffledCard); + } + } + + public static Map.Entry getRandomCard(Map> Cards) { + String randomCardName = getRandomCardName(Cards.keySet()); + + List cardValues = Cards.get(randomCardName); + + int cardvalue = cardValues.get(0); + + Cards.remove(randomCardName); + + return new AbstractMap.SimpleEntry<>(randomCardName, cardvalue); + } + + public static T getRandomCardName(Set keySet) { + int randomIndex = new Random().nextInt(keySet.size()); + Iterator iterator = keySet.iterator(); + for(int i = 0; i< randomIndex; i++) { + iterator.next(); + + } + return iterator.next(); + } + } + + + + diff --git a/src/main/java/blackjack/DecideResult.java b/src/main/java/blackjack/DecideResult.java new file mode 100644 index 000000000..1ed9e00fe --- /dev/null +++ b/src/main/java/blackjack/DecideResult.java @@ -0,0 +1,21 @@ +package blackjack; + +import java.util.Map; +public class DecideResult { + public static String determineWinner(Map participantStates) { + String winner = null; + int HighestSum = Integer.MIN_VALUE; + + for (Map.Entry entry : participantStates.entrySet()) { + CardGameSimulator.ParticipantState participantState = entry.getValue(); + int sum = participantState.printsum(); + + if(sum > HighestSum) { + HighestSum = sum; + winner = entry.getKey(); + } + } + + return winner; + } +} From d27f20b59d7ab334981b68db549ec408eac2f31e Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Sun, 28 Jan 2024 09:56:12 +0900 Subject: [PATCH 02/11] =?UTF-8?q?Application.java=20=EC=9E=84=EC=8B=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index ea3c0a433..6d8593e35 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -27,11 +27,11 @@ public static void main(String[] args) { Between16And21.SumBetween16And21(participant, entry, Cards); } } - String winner = DecideWinner.determineWinner(participantStates); + String winner = DecideResult.determineWinner(participantStates); System.out.println("## 최종 승패"); System.out.println("딜러: "); for (String participant : participants) { - System.out.println(participant + ": " + ); + System.out.println(participant + ": " ); } } From 640c3ca08e9fdf6bd5fba3767be5caa1369d7b70 Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Sun, 28 Jan 2024 10:47:31 +0900 Subject: [PATCH 03/11] =?UTF-8?q?=EB=94=9C=EB=9F=AC=EB=A5=BC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=ED=95=98=EA=B3=A0=20=EB=94=9C=EB=9F=AC=EC=97=90?= =?UTF-8?q?=EA=B2=8C=20=ED=95=84=EC=9A=94=ED=95=9C=20=EB=A1=9C=EC=A7=81(De?= =?UTF-8?q?alerBetween16And21,=20Application.java=EC=97=90=EC=84=9C?= =?UTF-8?q?=EC=9D=98=20for=EB=AC=B8)=EC=9D=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 20 ++++++++++++++------ src/main/java/blackjack/Between16And21.java | 19 +++++++++++++++++++ src/main/java/blackjack/DecideResult.java | 19 +++++++------------ 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index 6d8593e35..c7bb9397a 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -15,25 +15,33 @@ public class Application { public static void main(String[] args) { Map> Cards = CardDictionary.createCardDictionary(); + List Dealer = Arrays.asList("딜러"); List participants = getInput("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)", s -> Arrays.asList(s.split(","))); ParticipantSize(participants); //예외 처리 Collections.shuffle(participants); Map participantStates = CardGameSimulator.simulateCardGame(participants, Cards); + Map dealerState = CardGameSimulator.simulateCardGame(Dealer, Cards); for (String participant : participants) { - for (Map.Entry entry: participantStates.entrySet()) { + for (Map.Entry entry : participantStates.entrySet()) { Between16And21.SumBetween16And21(participant, entry, Cards); } } - String winner = DecideResult.determineWinner(participantStates); - System.out.println("## 최종 승패"); - System.out.println("딜러: "); - for (String participant : participants) { - System.out.println(participant + ": " ); + + + for (Map.Entry entry : dealerState.entrySet()) { + Between16And21.DealerBetween16And21(Dealer.get(0), entry, Cards); } + + for (String participant : participants) { + for (Map.Entry entry : participantStates.entrySet()) { + DecideResult.CompareWithDealer(); + } + + } } private static T getInput(String prompt, Function parser) { diff --git a/src/main/java/blackjack/Between16And21.java b/src/main/java/blackjack/Between16And21.java index e65838a7a..9fdef0a6d 100644 --- a/src/main/java/blackjack/Between16And21.java +++ b/src/main/java/blackjack/Between16And21.java @@ -36,4 +36,23 @@ public static void SumBetween16And21(String participant, Map.Entry DealerEntry, Map> Cards) { + CardGameSimulator.ParticipantState DealerState = DealerEntry.getValue(); + int sum = DealerState.getSum(Cards); + + while(true) { + if (sum > 21 || (16 < sum && sum <= 21)) { + break; + } + + if (sum < 16) { + Map.Entry extraCard = CardGameSimulator.getRandomCard(Cards); + int extraCardValue = extraCard.getValue(); + String extraCardName = extraCard.getKey(); + DealerState.updateSumAndNameList(extraCardValue, extraCardName); //DealerEntry + break; + } + } + } } diff --git a/src/main/java/blackjack/DecideResult.java b/src/main/java/blackjack/DecideResult.java index 1ed9e00fe..3e792b5d2 100644 --- a/src/main/java/blackjack/DecideResult.java +++ b/src/main/java/blackjack/DecideResult.java @@ -1,21 +1,16 @@ package blackjack; import java.util.Map; + public class DecideResult { - public static String determineWinner(Map participantStates) { - String winner = null; - int HighestSum = Integer.MIN_VALUE; + public static int DealerScore() { - for (Map.Entry entry : participantStates.entrySet()) { - CardGameSimulator.ParticipantState participantState = entry.getValue(); - int sum = participantState.printsum(); + } - if(sum > HighestSum) { - HighestSum = sum; - winner = entry.getKey(); - } - } + public static void CompareWithDealer() { - return winner; } + +// Calculate "딜러" ParticipantState.printsum() +// Compare each participant's sum value with dealer } From ca685b3b668e8744c1cc2da43d661dddc146d47f Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Tue, 30 Jan 2024 22:50:42 +0900 Subject: [PATCH 04/11] =?UTF-8?q?DecideResult.java=EC=97=90=20CompareWithD?= =?UTF-8?q?ealer=20=EC=B6=94=EA=B0=80,=20participantState=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EC=97=90=EC=84=9C=20=EC=8A=B9=ED=8C=A8=20?= =?UTF-8?q?=EA=B0=92=EC=9D=84=20=EA=B0=80=EC=A0=B8=EC=98=A8=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 10 ++--- src/main/java/blackjack/Between16And21.java | 8 ++-- .../java/blackjack/CardGameSimulator.java | 37 +++++++++++++++-- src/main/java/blackjack/DecideResult.java | 40 ++++++++++++++++--- 4 files changed, 77 insertions(+), 18 deletions(-) diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index c7bb9397a..02df29eeb 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -23,6 +23,7 @@ public static void main(String[] args) { Map participantStates = CardGameSimulator.simulateCardGame(participants, Cards); Map dealerState = CardGameSimulator.simulateCardGame(Dealer, Cards); + int DealerSum = 0; for (String participant : participants) { for (Map.Entry entry : participantStates.entrySet()) { @@ -33,15 +34,10 @@ public static void main(String[] args) { for (Map.Entry entry : dealerState.entrySet()) { Between16And21.DealerBetween16And21(Dealer.get(0), entry, Cards); + DealerSum = DecideResult.DealerScore(entry); } - - for (String participant : participants) { - for (Map.Entry entry : participantStates.entrySet()) { - DecideResult.CompareWithDealer(); - } - - } + DecideResult.CompareWithDealer(participants, DealerSum, participantStates); } private static T getInput(String prompt, Function parser) { diff --git a/src/main/java/blackjack/Between16And21.java b/src/main/java/blackjack/Between16And21.java index 9fdef0a6d..60771210d 100644 --- a/src/main/java/blackjack/Between16And21.java +++ b/src/main/java/blackjack/Between16And21.java @@ -10,13 +10,14 @@ public static void SumBetween16And21(String participant, Map.Entry=21) { break; } if (sum > 16 && sum < 21) { - System.out.println(participant+ "는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"); + System.out.println(name + "는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"); Scanner scanner = new Scanner(System.in); String answer = scanner.nextLine(); @@ -26,10 +27,10 @@ public static void SumBetween16And21(String participant, Map.Entry + System.out.println(Dealer + "카드: " + DealerState.printcardlist()); break; } } diff --git a/src/main/java/blackjack/CardGameSimulator.java b/src/main/java/blackjack/CardGameSimulator.java index b9528e40f..3a4db47ff 100644 --- a/src/main/java/blackjack/CardGameSimulator.java +++ b/src/main/java/blackjack/CardGameSimulator.java @@ -8,20 +8,31 @@ public class CardGameSimulator { public static class ParticipantState { private int sum; private List cardlist; + private String name; + private List WinOrLoss = new ArrayList<>(); public ParticipantState() { this.sum = 0; this.cardlist = new ArrayList<>(); + this.name = ""; } public int printsum() { return this.sum; } - public List printlist() { + public List printcardlist() { return this.cardlist; } + public String printname() { + return this.name; + } + + public String printwinorloss() { + return this.WinOrLoss.get(0); + } + public int getSum(Map> Cards) { CardOperation sumOperation = shuffledCard -> { @@ -34,6 +45,22 @@ public int getSum(Map> Cards) { return this.sum; } + public String getName(String participant) { + this.name = participant; + return this.name; + } + + + public void WinOrLoss(int winorloss) { + + if (winorloss ==0) { + this.WinOrLoss.add("승"); + } + if (winorloss ==1) { + this.WinOrLoss.add("패"); + } + } + public void updateSumAndNameList(int value, String cardNames) { this.sum += value; this.cardlist.add(cardNames); @@ -88,10 +115,14 @@ public static T getRandomCardName(Set keySet) { for(int i = 0; i< randomIndex; i++) { iterator.next(); - } - return iterator.next(); } + return iterator.next(); } +} + + + + diff --git a/src/main/java/blackjack/DecideResult.java b/src/main/java/blackjack/DecideResult.java index 3e792b5d2..0bfc6d86e 100644 --- a/src/main/java/blackjack/DecideResult.java +++ b/src/main/java/blackjack/DecideResult.java @@ -1,16 +1,46 @@ package blackjack; +import java.util.List; import java.util.Map; public class DecideResult { - public static int DealerScore() { - + public static int DealerScore(Map.Entry dealerEntry) { + int sum = dealerEntry.getValue().printsum(); + return sum; } - public static void CompareWithDealer() { + public static void CompareWithDealer(List participants, int DealerSum, Map participantStates) { + + int win = 0; + int loss = 0; + + for (Map.Entry entry : participantStates.entrySet()) { + int participantSum = entry.getValue().printsum(); + if (participantSum <= 21) { + if (DealerSum > participantSum) { + win += 1; + entry.getValue().WinOrLoss(1); + } + if (DealerSum < participantSum) { + loss += 1; + entry.getValue().WinOrLoss(0); + } + } else { + win +=1; + } + } + + System.out.println("## 최종 승패"); + System.out.println("딜러: " + win + "승 " + loss + '패'); + + for (Map.Entry entry : participantStates.entrySet()) { + String name = entry.getValue().printname(); + System.out.println(name + ": " + entry.getValue().printwinorloss()); + + } - } // Calculate "딜러" ParticipantState.printsum() // Compare each participant's sum value with dealer -} + } +} \ No newline at end of file From bfc893ac3d56fe84dae4c5fa4d5d551ce9979427 Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Tue, 30 Jan 2024 23:11:34 +0900 Subject: [PATCH 05/11] =?UTF-8?q?InputView.java=20=EC=B6=94=EA=B0=80,=20su?= =?UTF-8?q?mbetween16and21.java=20=EB=A3=A8=ED=94=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 10 +--------- src/main/java/blackjack/Between16And21.java | 9 +++++++-- src/main/java/blackjack/InputView.java | 17 +++++++++++++++++ src/main/java/blackjack/OutView.java | 4 ++++ 4 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 src/main/java/blackjack/InputView.java create mode 100644 src/main/java/blackjack/OutView.java diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index 02df29eeb..f4012f048 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -16,7 +16,7 @@ public static void main(String[] args) { Map> Cards = CardDictionary.createCardDictionary(); List Dealer = Arrays.asList("딜러"); - List participants = getInput("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)", s -> Arrays.asList(s.split(","))); + List participants = InputView.getInput("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)", s -> Arrays.asList(s.split(","))); ParticipantSize(participants); //예외 처리 Collections.shuffle(participants); @@ -40,15 +40,7 @@ public static void main(String[] args) { DecideResult.CompareWithDealer(participants, DealerSum, participantStates); } - private static T getInput(String prompt, Function parser) { - T participants; - Scanner scanner = new Scanner(System.in); - System.out.println(prompt); - String userinput = scanner.nextLine(); - participants = parser.apply(userinput); - return participants; - } private static void ParticipantSize(List participants) { if (participants.size() > 25) { diff --git a/src/main/java/blackjack/Between16And21.java b/src/main/java/blackjack/Between16And21.java index 60771210d..aa1274b61 100644 --- a/src/main/java/blackjack/Between16And21.java +++ b/src/main/java/blackjack/Between16And21.java @@ -18,8 +18,7 @@ public static void SumBetween16And21(String participant, Map.Entry 16 && sum < 21) { System.out.println(name + "는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"); - Scanner scanner = new Scanner(System.in); - String answer = scanner.nextLine(); + String answer = InputView.getInput("",String::valueOf); if("y".equalsIgnoreCase(answer)) { Map.Entry extraCard = CardGameSimulator.getRandomCard(Cards); @@ -28,6 +27,12 @@ public static void SumBetween16And21(String participant, Map.Entry 16 && participantState.printsum() <= 21) { + continue; + } else { + break; + } //16과 21인지 판단하고 만약 중간이라면 다시 SumBetween16and21 실행 아니면 continue } else if ("n".equalsIgnoreCase(answer)) { System.out.println(name + "카드: " + participantState.printcardlist()); diff --git a/src/main/java/blackjack/InputView.java b/src/main/java/blackjack/InputView.java new file mode 100644 index 000000000..c2ea3ac74 --- /dev/null +++ b/src/main/java/blackjack/InputView.java @@ -0,0 +1,17 @@ +package blackjack; + +import java.util.Scanner; +import java.util.function.Function; + +public class InputView { + public static T getInput(String prompt, Function parser) { + T result; + + Scanner scanner = new Scanner(System.in); + System.out.println(prompt); + + String userinput = scanner.nextLine(); + result = parser.apply(userinput); + return result; + } +} diff --git a/src/main/java/blackjack/OutView.java b/src/main/java/blackjack/OutView.java new file mode 100644 index 000000000..aa4852cab --- /dev/null +++ b/src/main/java/blackjack/OutView.java @@ -0,0 +1,4 @@ +package blackjack; + +public class OutView { +} From fd63b3351336acbcaae31232737ef4da888fa01b Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Tue, 30 Jan 2024 23:32:35 +0900 Subject: [PATCH 06/11] =?UTF-8?q?GameSimulator=20->=20BlackJackGame,=20Par?= =?UTF-8?q?ticipant=20=EA=B0=9D=EC=B2=B4=20=ED=8C=8C=EC=9D=BC=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1,=20OperationWithCard.java=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 8 +- src/main/java/blackjack/Between16And21.java | 12 +- src/main/java/blackjack/BlackJackGame.java | 33 +++++ .../java/blackjack/CardGameSimulator.java | 129 ------------------ src/main/java/blackjack/DecideResult.java | 12 +- .../java/blackjack/OperationWithCard.java | 38 ++++++ src/main/java/blackjack/Participant.java | 69 ++++++++++ 7 files changed, 154 insertions(+), 147 deletions(-) create mode 100644 src/main/java/blackjack/BlackJackGame.java delete mode 100644 src/main/java/blackjack/CardGameSimulator.java create mode 100644 src/main/java/blackjack/OperationWithCard.java create mode 100644 src/main/java/blackjack/Participant.java diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index f4012f048..ce0e0a95c 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -21,18 +21,18 @@ public static void main(String[] args) { Collections.shuffle(participants); - Map participantStates = CardGameSimulator.simulateCardGame(participants, Cards); - Map dealerState = CardGameSimulator.simulateCardGame(Dealer, Cards); + Map participantStates = BlackJackGame.simulateCardGame(participants, Cards); + Map dealerState = BlackJackGame.simulateCardGame(Dealer, Cards); int DealerSum = 0; for (String participant : participants) { - for (Map.Entry entry : participantStates.entrySet()) { + for (Map.Entry entry : participantStates.entrySet()) { Between16And21.SumBetween16And21(participant, entry, Cards); } } - for (Map.Entry entry : dealerState.entrySet()) { + for (Map.Entry entry : dealerState.entrySet()) { Between16And21.DealerBetween16And21(Dealer.get(0), entry, Cards); DealerSum = DecideResult.DealerScore(entry); } diff --git a/src/main/java/blackjack/Between16And21.java b/src/main/java/blackjack/Between16And21.java index aa1274b61..f1a0895b2 100644 --- a/src/main/java/blackjack/Between16And21.java +++ b/src/main/java/blackjack/Between16And21.java @@ -6,9 +6,9 @@ import java.util.Scanner; public class Between16And21 { - public static void SumBetween16And21(String participant, Map.Entry participantEntry, Map> Cards) { + public static void SumBetween16And21(String participant, Map.Entry participantEntry, Map> Cards) { while(true) { - CardGameSimulator.ParticipantState participantState = participantEntry.getValue(); + Participant.ParticipantState participantState = participantEntry.getValue(); int sum = participantState.getSum(Cards); String name = participantState.getName(participant); @@ -21,7 +21,7 @@ public static void SumBetween16And21(String participant, Map.Entry extraCard = CardGameSimulator.getRandomCard(Cards); + Map.Entry extraCard = OperationWithCard.getRandomCard(Cards); int extraCardValue = extraCard.getValue(); String extraCardName = extraCard.getKey(); participantState.updateSumAndNameList(extraCardValue, extraCardName); @@ -43,8 +43,8 @@ public static void SumBetween16And21(String participant, Map.Entry DealerEntry, Map> Cards) { - CardGameSimulator.ParticipantState DealerState = DealerEntry.getValue(); + public static void DealerBetween16And21(String Dealer, Map.Entry DealerEntry, Map> Cards) { + Participant.ParticipantState DealerState = DealerEntry.getValue(); int sum = DealerState.getSum(Cards); while(true) { @@ -53,7 +53,7 @@ public static void DealerBetween16And21(String Dealer, Map.Entry extraCard = CardGameSimulator.getRandomCard(Cards); + Map.Entry extraCard = OperationWithCard.getRandomCard(Cards); int extraCardValue = extraCard.getValue(); String extraCardName = extraCard.getKey(); DealerState.updateSumAndNameList(extraCardValue, extraCardName); //DealerEntry diff --git a/src/main/java/blackjack/BlackJackGame.java b/src/main/java/blackjack/BlackJackGame.java new file mode 100644 index 000000000..192966ac8 --- /dev/null +++ b/src/main/java/blackjack/BlackJackGame.java @@ -0,0 +1,33 @@ +package blackjack; + +import java.util.*; + + +public class BlackJackGame { + public static Map simulateCardGame(List participants, Map> Cards) { + Map participantStates = new HashMap<>(); + + for (String participant : participants) { + participantStates.put(participant, new Participant.ParticipantState()); + } + + for (String participant : participants) { + Participant.ParticipantState participantState = participantStates.get(participant); + OperationWithCard.performCardOperation(Cards, shuffledCard -> { + int value = shuffledCard.getValue(); + String cardName = shuffledCard.getKey(); + participantState.updateSumAndNameList(value, cardName); + }); + } + return participantStates; + + } + +} + + + + + + + diff --git a/src/main/java/blackjack/CardGameSimulator.java b/src/main/java/blackjack/CardGameSimulator.java deleted file mode 100644 index 3a4db47ff..000000000 --- a/src/main/java/blackjack/CardGameSimulator.java +++ /dev/null @@ -1,129 +0,0 @@ -package blackjack; - -import java.util.*; - - -public class CardGameSimulator { - - public static class ParticipantState { - private int sum; - private List cardlist; - private String name; - private List WinOrLoss = new ArrayList<>(); - - public ParticipantState() { - this.sum = 0; - this.cardlist = new ArrayList<>(); - this.name = ""; - } - - public int printsum() { - return this.sum; - } - - public List printcardlist() { - return this.cardlist; - } - - public String printname() { - return this.name; - } - - public String printwinorloss() { - return this.WinOrLoss.get(0); - } - - public int getSum(Map> Cards) { - - CardOperation sumOperation = shuffledCard -> { - int value = shuffledCard.getValue(); - this.sum += value; - }; - - performCardOperation(Cards, sumOperation); - - return this.sum; - } - - public String getName(String participant) { - this.name = participant; - return this.name; - } - - - public void WinOrLoss(int winorloss) { - - if (winorloss ==0) { - this.WinOrLoss.add("승"); - } - if (winorloss ==1) { - this.WinOrLoss.add("패"); - } - } - - public void updateSumAndNameList(int value, String cardNames) { - this.sum += value; - this.cardlist.add(cardNames); - } - } - - public static Map simulateCardGame(List participants, Map> Cards) { - Map participantStates = new HashMap<>(); - - for(String participant : participants) { - participantStates.put(participant, new ParticipantState()); - } - - for (String participant : participants) { - ParticipantState participantState = participantStates.get(participant); - performCardOperation(Cards, shuffledCard -> { - int value = shuffledCard.getValue(); - String cardName = shuffledCard.getKey(); - participantState.updateSumAndNameList(value, cardName); - }); - } - return participantStates; - - } - - interface CardOperation { - void operate(Map.Entry shuffledCard); - } - - private static void performCardOperation(Map> Cards, CardOperation operation) { - for(int i = 0; i < 2; i++) { - Map.Entry shuffledCard = getRandomCard(Cards); - operation.operate(shuffledCard); - } - } - - public static Map.Entry getRandomCard(Map> Cards) { - String randomCardName = getRandomCardName(Cards.keySet()); - - List cardValues = Cards.get(randomCardName); - - int cardvalue = cardValues.get(0); - - Cards.remove(randomCardName); - - return new AbstractMap.SimpleEntry<>(randomCardName, cardvalue); - } - - public static T getRandomCardName(Set keySet) { - int randomIndex = new Random().nextInt(keySet.size()); - Iterator iterator = keySet.iterator(); - for(int i = 0; i< randomIndex; i++) { - iterator.next(); - - } - return iterator.next(); - } -} - - - - - - - - diff --git a/src/main/java/blackjack/DecideResult.java b/src/main/java/blackjack/DecideResult.java index 0bfc6d86e..c79807bc4 100644 --- a/src/main/java/blackjack/DecideResult.java +++ b/src/main/java/blackjack/DecideResult.java @@ -4,17 +4,17 @@ import java.util.Map; public class DecideResult { - public static int DealerScore(Map.Entry dealerEntry) { + public static int DealerScore(Map.Entry dealerEntry) { int sum = dealerEntry.getValue().printsum(); return sum; } - public static void CompareWithDealer(List participants, int DealerSum, Map participantStates) { + public static void CompareWithDealer(List participants, int DealerSum, Map participantStates) { int win = 0; int loss = 0; - for (Map.Entry entry : participantStates.entrySet()) { + for (Map.Entry entry : participantStates.entrySet()) { int participantSum = entry.getValue().printsum(); if (participantSum <= 21) { if (DealerSum > participantSum) { @@ -33,14 +33,10 @@ public static void CompareWithDealer(List participants, int DealerSum, M System.out.println("## 최종 승패"); System.out.println("딜러: " + win + "승 " + loss + '패'); - for (Map.Entry entry : participantStates.entrySet()) { + for (Map.Entry entry : participantStates.entrySet()) { String name = entry.getValue().printname(); System.out.println(name + ": " + entry.getValue().printwinorloss()); } - - -// Calculate "딜러" ParticipantState.printsum() -// Compare each participant's sum value with dealer } } \ No newline at end of file diff --git a/src/main/java/blackjack/OperationWithCard.java b/src/main/java/blackjack/OperationWithCard.java new file mode 100644 index 000000000..bc2dc3f14 --- /dev/null +++ b/src/main/java/blackjack/OperationWithCard.java @@ -0,0 +1,38 @@ +package blackjack; + +import java.util.*; + +public class OperationWithCard { + interface CardOperation { + void operate(Map.Entry shuffledCard); + } + + public static void performCardOperation(Map> Cards, CardOperation operation) { + for (int i = 0; i < 2; i++) { + Map.Entry shuffledCard = getRandomCard(Cards); + operation.operate(shuffledCard); + } + } + + public static Map.Entry getRandomCard(Map> Cards) { + String randomCardName = getRandomCardName(Cards.keySet()); + + List cardValues = Cards.get(randomCardName); + + int cardvalue = cardValues.get(0); + + Cards.remove(randomCardName); + + return new AbstractMap.SimpleEntry<>(randomCardName, cardvalue); + } + + public static T getRandomCardName(Set keySet) { + int randomIndex = new Random().nextInt(keySet.size()); + Iterator iterator = keySet.iterator(); + for (int i = 0; i < randomIndex; i++) { + iterator.next(); + + } + return iterator.next(); + } +} diff --git a/src/main/java/blackjack/Participant.java b/src/main/java/blackjack/Participant.java new file mode 100644 index 000000000..877fd385d --- /dev/null +++ b/src/main/java/blackjack/Participant.java @@ -0,0 +1,69 @@ +package blackjack; + +import java.util.*; + +public class Participant { + public static class ParticipantState { + private int sum; + private List cardlist; + private String name; + private List WinOrLoss = new ArrayList<>(); + + public ParticipantState() { + this.sum = 0; + this.cardlist = new ArrayList<>(); + this.name = ""; + } + + public int printsum() { + return this.sum; + } + + public List printcardlist() { + return this.cardlist; + } + + public String printname() { + return this.name; + } + + public String printwinorloss() { + return this.WinOrLoss.get(0); + } + + public int getSum(Map> Cards) { + + OperationWithCard.CardOperation sumOperation = shuffledCard -> { + int value = shuffledCard.getValue(); + this.sum += value; + }; + + OperationWithCard.performCardOperation(Cards, sumOperation); + + return this.sum; + } + + public String getName(String participant) { + this.name = participant; + return this.name; + } + + + public void WinOrLoss(int winorloss) { + + if (winorloss == 0) { + this.WinOrLoss.add("승"); + } + if (winorloss == 1) { + this.WinOrLoss.add("패"); + } + } + + public void updateSumAndNameList(int value, String cardNames) { + this.sum += value; + this.cardlist.add(cardNames); + } + } + + +} From 3046e9e43bf102dc53a465a5e968718f03cc64fc Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Wed, 31 Jan 2024 00:09:18 +0900 Subject: [PATCH 07/11] =?UTF-8?q?OutputView.java=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/.DS_Store | Bin 6148 -> 6148 bytes src/main/java/blackjack/Application.java | 16 +++--------- src/main/java/blackjack/Between16And21.java | 24 +++++++++--------- src/main/java/blackjack/DecideResult.java | 14 +++------- src/main/java/blackjack/OutView.java | 4 --- .../java/blackjack/{ => view}/InputView.java | 2 +- src/main/java/blackjack/view/OutView.java | 18 +++++++++++++ 7 files changed, 38 insertions(+), 40 deletions(-) delete mode 100644 src/main/java/blackjack/OutView.java rename src/main/java/blackjack/{ => view}/InputView.java (94%) create mode 100644 src/main/java/blackjack/view/OutView.java diff --git a/src/main/java/blackjack/.DS_Store b/src/main/java/blackjack/.DS_Store index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..93142988b6527640a2905a094ba89471e2559357 100644 GIT binary patch delta 209 zcmZoMXfc=|&e%RNQEZ}~q96+c0|O%ig8)NLx?yl~es00UMD2QzAPYkoLncEiLpe+W zC entry : participantStates.entrySet()) { + entry.getValue().getName(participant); Between16And21.SumBetween16And21(participant, entry, Cards); } } - for (Map.Entry entry : dealerState.entrySet()) { Between16And21.DealerBetween16And21(Dealer.get(0), entry, Cards); DealerSum = DecideResult.DealerScore(entry); @@ -40,17 +37,10 @@ public static void main(String[] args) { DecideResult.CompareWithDealer(participants, DealerSum, participantStates); } - - private static void ParticipantSize(List participants) { if (participants.size() > 25) { throw new IllegalArgumentException("가능한 참가 인원을 초과하였습니다."); } } - - - - - } diff --git a/src/main/java/blackjack/Between16And21.java b/src/main/java/blackjack/Between16And21.java index f1a0895b2..3c3ecd77c 100644 --- a/src/main/java/blackjack/Between16And21.java +++ b/src/main/java/blackjack/Between16And21.java @@ -1,16 +1,16 @@ package blackjack; +import blackjack.view.InputView; + import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Scanner; public class Between16And21 { public static void SumBetween16And21(String participant, Map.Entry participantEntry, Map> Cards) { while(true) { - Participant.ParticipantState participantState = participantEntry.getValue(); - int sum = participantState.getSum(Cards); - String name = participantState.getName(participant); + int sum = participantEntry.getValue().getSum(Cards); + String name = participantEntry.getValue().printname(); if (sum < 16 || sum >=21) { break; @@ -20,22 +20,23 @@ public static void SumBetween16And21(String participant, Map.Entry extraCard = OperationWithCard.getRandomCard(Cards); int extraCardValue = extraCard.getValue(); String extraCardName = extraCard.getKey(); - participantState.updateSumAndNameList(extraCardValue, extraCardName); + participantEntry.getValue().updateSumAndNameList(extraCardValue, extraCardName); - System.out.println(name + "카드: " + participantState.printcardlist()); + System.out.println(name + "카드: " + participantEntry.getValue().printcardlist()); - if (participantState.printsum() > 16 && participantState.printsum() <= 21) { + if (participantEntry.getValue().printsum() > 16 && participantEntry.getValue().printsum() <= 21) { continue; } else { break; } //16과 21인지 판단하고 만약 중간이라면 다시 SumBetween16and21 실행 아니면 continue } else if ("n".equalsIgnoreCase(answer)) { - System.out.println(name + "카드: " + participantState.printcardlist()); + System.out.println(name + "카드: " + participantEntry.getValue().printcardlist()); break; } @@ -44,8 +45,7 @@ public static void SumBetween16And21(String participant, Map.Entry DealerEntry, Map> Cards) { - Participant.ParticipantState DealerState = DealerEntry.getValue(); - int sum = DealerState.getSum(Cards); + int sum = DealerEntry.getValue().getSum(Cards); while(true) { if (sum > 21 || (16 < sum && sum <= 21)) { @@ -56,8 +56,8 @@ public static void DealerBetween16And21(String Dealer, Map.Entry extraCard = OperationWithCard.getRandomCard(Cards); int extraCardValue = extraCard.getValue(); String extraCardName = extraCard.getKey(); - DealerState.updateSumAndNameList(extraCardValue, extraCardName); //DealerEntry - System.out.println(Dealer + "카드: " + DealerState.printcardlist()); + DealerEntry.getValue().updateSumAndNameList(extraCardValue, extraCardName); //DealerEntry + System.out.println(Dealer + "카드: " + DealerEntry.getValue().printcardlist()); break; } } diff --git a/src/main/java/blackjack/DecideResult.java b/src/main/java/blackjack/DecideResult.java index c79807bc4..7b66d260b 100644 --- a/src/main/java/blackjack/DecideResult.java +++ b/src/main/java/blackjack/DecideResult.java @@ -1,5 +1,7 @@ package blackjack; +import blackjack.view.OutView; + import java.util.List; import java.util.Map; @@ -26,17 +28,9 @@ public static void CompareWithDealer(List participants, int DealerSum, M entry.getValue().WinOrLoss(0); } } else { - win +=1; + win += 1; } } - - System.out.println("## 최종 승패"); - System.out.println("딜러: " + win + "승 " + loss + '패'); - - for (Map.Entry entry : participantStates.entrySet()) { - String name = entry.getValue().printname(); - System.out.println(name + ": " + entry.getValue().printwinorloss()); - - } + OutView.printFinalResults(win, loss, participantStates); } } \ No newline at end of file diff --git a/src/main/java/blackjack/OutView.java b/src/main/java/blackjack/OutView.java deleted file mode 100644 index aa4852cab..000000000 --- a/src/main/java/blackjack/OutView.java +++ /dev/null @@ -1,4 +0,0 @@ -package blackjack; - -public class OutView { -} diff --git a/src/main/java/blackjack/InputView.java b/src/main/java/blackjack/view/InputView.java similarity index 94% rename from src/main/java/blackjack/InputView.java rename to src/main/java/blackjack/view/InputView.java index c2ea3ac74..cf38063e6 100644 --- a/src/main/java/blackjack/InputView.java +++ b/src/main/java/blackjack/view/InputView.java @@ -1,4 +1,4 @@ -package blackjack; +package blackjack.view; import java.util.Scanner; import java.util.function.Function; diff --git a/src/main/java/blackjack/view/OutView.java b/src/main/java/blackjack/view/OutView.java new file mode 100644 index 000000000..7fe9a340b --- /dev/null +++ b/src/main/java/blackjack/view/OutView.java @@ -0,0 +1,18 @@ +package blackjack.view; + +import blackjack.Participant; + +import java.util.Map; + +public class OutView { + public static void printFinalResults(int win, int loss, Map participantStates) { + System.out.println("## 최종 승패"); + System.out.println("딜러: " + win + "승 " + loss + '패'); + + for (Map.Entry entry : participantStates.entrySet()) { + String name = entry.getValue().printname(); + System.out.println(name + ": " + entry.getValue().printwinorloss()); + + } + } +} From ea86e8d2ca9795f3eff9ed5ead5d699f5fb4f1e7 Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Wed, 31 Jan 2024 00:14:46 +0900 Subject: [PATCH 08/11] =?UTF-8?q?=EB=94=9C=EB=9F=AC=EA=B2=B0=EA=B3=BC?= =?UTF-8?q?=EA=B0=92=20=EC=B6=9C=EB=A0=A5=20=ED=95=A8=EC=88=98=EC=A4=91?= =?UTF-8?q?=EB=B3=B5=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 2 +- src/main/java/blackjack/DecideResult.java | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index 070fb030c..96cb51075 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -31,7 +31,7 @@ public static void main(String[] args) { for (Map.Entry entry : dealerState.entrySet()) { Between16And21.DealerBetween16And21(Dealer.get(0), entry, Cards); - DealerSum = DecideResult.DealerScore(entry); + DealerSum = entry.getValue().printsum(); } DecideResult.CompareWithDealer(participants, DealerSum, participantStates); diff --git a/src/main/java/blackjack/DecideResult.java b/src/main/java/blackjack/DecideResult.java index 7b66d260b..eced6e35b 100644 --- a/src/main/java/blackjack/DecideResult.java +++ b/src/main/java/blackjack/DecideResult.java @@ -6,10 +6,6 @@ import java.util.Map; public class DecideResult { - public static int DealerScore(Map.Entry dealerEntry) { - int sum = dealerEntry.getValue().printsum(); - return sum; - } public static void CompareWithDealer(List participants, int DealerSum, Map participantStates) { From dc886dfdf5ffd00a409414a982e484bd84672057 Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Wed, 31 Jan 2024 01:05:00 +0900 Subject: [PATCH 09/11] =?UTF-8?q?canReceiveCard.java=EC=83=9D=EC=84=B1?= =?UTF-8?q?=ED=9B=84=20=EA=B0=9D=EC=B2=B4=EC=99=80=20=EA=B3=B5=ED=86=B5?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/Application.java | 4 +- src/main/java/blackjack/Between16And21.java | 65 ---------------- .../java/blackjack/CardGameParticipant.java | 10 +++ src/main/java/blackjack/DecideResult.java | 1 - src/main/java/blackjack/canReceiveCard.java | 74 +++++++++++++++++++ 5 files changed, 86 insertions(+), 68 deletions(-) delete mode 100644 src/main/java/blackjack/Between16And21.java create mode 100644 src/main/java/blackjack/CardGameParticipant.java create mode 100644 src/main/java/blackjack/canReceiveCard.java diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index 96cb51075..cdbc47b31 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -25,12 +25,12 @@ public static void main(String[] args) { for (String participant : participants) { for (Map.Entry entry : participantStates.entrySet()) { entry.getValue().getName(participant); - Between16And21.SumBetween16And21(participant, entry, Cards); + canReceiveCard.ParticipantBetween16And21(participant, entry, Cards); } } for (Map.Entry entry : dealerState.entrySet()) { - Between16And21.DealerBetween16And21(Dealer.get(0), entry, Cards); + canReceiveCard.DealerBetween16And21(Dealer.get(0), entry, Cards); DealerSum = entry.getValue().printsum(); } diff --git a/src/main/java/blackjack/Between16And21.java b/src/main/java/blackjack/Between16And21.java deleted file mode 100644 index 3c3ecd77c..000000000 --- a/src/main/java/blackjack/Between16And21.java +++ /dev/null @@ -1,65 +0,0 @@ -package blackjack; - -import blackjack.view.InputView; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class Between16And21 { - public static void SumBetween16And21(String participant, Map.Entry participantEntry, Map> Cards) { - while(true) { - int sum = participantEntry.getValue().getSum(Cards); - String name = participantEntry.getValue().printname(); - - if (sum < 16 || sum >=21) { - break; - } - - if (sum > 16 && sum < 21) { - System.out.println(name + "는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"); - String answer = InputView.getInput("",String::valueOf); - - - if("y".equalsIgnoreCase(answer)) { - Map.Entry extraCard = OperationWithCard.getRandomCard(Cards); - int extraCardValue = extraCard.getValue(); - String extraCardName = extraCard.getKey(); - participantEntry.getValue().updateSumAndNameList(extraCardValue, extraCardName); - - System.out.println(name + "카드: " + participantEntry.getValue().printcardlist()); - - if (participantEntry.getValue().printsum() > 16 && participantEntry.getValue().printsum() <= 21) { - continue; - } else { - break; - } - //16과 21인지 판단하고 만약 중간이라면 다시 SumBetween16and21 실행 아니면 continue - } else if ("n".equalsIgnoreCase(answer)) { - System.out.println(name + "카드: " + participantEntry.getValue().printcardlist()); - break; - - } - } - } - } - - public static void DealerBetween16And21(String Dealer, Map.Entry DealerEntry, Map> Cards) { - int sum = DealerEntry.getValue().getSum(Cards); - - while(true) { - if (sum > 21 || (16 < sum && sum <= 21)) { - break; - } - - if (sum < 16) { - Map.Entry extraCard = OperationWithCard.getRandomCard(Cards); - int extraCardValue = extraCard.getValue(); - String extraCardName = extraCard.getKey(); - DealerEntry.getValue().updateSumAndNameList(extraCardValue, extraCardName); //DealerEntry - System.out.println(Dealer + "카드: " + DealerEntry.getValue().printcardlist()); - break; - } - } - } -} diff --git a/src/main/java/blackjack/CardGameParticipant.java b/src/main/java/blackjack/CardGameParticipant.java new file mode 100644 index 000000000..d44fa54f4 --- /dev/null +++ b/src/main/java/blackjack/CardGameParticipant.java @@ -0,0 +1,10 @@ +package blackjack; + +import java.util.List; +import java.util.Map; + +public interface CardGameParticipant { + void playTurn(String participant, Map.Entry participantEntry, Map> Cards); + + boolean shouldContinue(); +} diff --git a/src/main/java/blackjack/DecideResult.java b/src/main/java/blackjack/DecideResult.java index eced6e35b..abbdfa3d2 100644 --- a/src/main/java/blackjack/DecideResult.java +++ b/src/main/java/blackjack/DecideResult.java @@ -6,7 +6,6 @@ import java.util.Map; public class DecideResult { - public static void CompareWithDealer(List participants, int DealerSum, Map participantStates) { int win = 0; diff --git a/src/main/java/blackjack/canReceiveCard.java b/src/main/java/blackjack/canReceiveCard.java new file mode 100644 index 000000000..bfde9f8a5 --- /dev/null +++ b/src/main/java/blackjack/canReceiveCard.java @@ -0,0 +1,74 @@ +package blackjack; + +import blackjack.view.InputView; +import java.util.List; +import java.util.Map; + +public class canReceiveCard { + public static void ParticipantBetween16And21(String participant, Map.Entry participantEntry, Map> Cards) { + playTurn(new Participant(), participant, participantEntry, Cards); + } + + public static void DealerBetween16And21(String Dealer, Map.Entry DealerEntry, Map> Cards) { + playTurn(new Dealer(), Dealer, DealerEntry, Cards); + } + + public static void playTurn(CardGameParticipant participant, String name, Map.Entry entry, Map> Cards) { + while (true) { + int sum = entry.getValue().getSum(Cards); + + if (sum < 16 || sum >= 21) { + break; + } + + if (sum > 16 && sum < 21) { + System.out.println(name + "는 한장의 카드를 더 받겠습니까?(예는 y, 아니오는 n)"); + String answer = InputView.getInput("", String::valueOf); + + + if ("y".equalsIgnoreCase(answer)) { + Map.Entry extraCard = OperationWithCard.getRandomCard(Cards); + int extraCardValue = extraCard.getValue(); + String extraCardName = extraCard.getKey(); + entry.getValue().updateSumAndNameList(extraCardValue, extraCardName); + + System.out.println(name + "카드: " + entry.getValue().printcardlist()); + + if (participant.shouldContinue()) { + continue; + } else { + break; + } + //16과 21인지 판단하고 만약 중간이라면 다시 SumBetween16and21 실행 아니면 continue + } else if ("n".equalsIgnoreCase(answer)) { + System.out.println(name + "카드: " + entry.getValue().printcardlist()); + break; + + } + } + } + } + + static class Participant implements CardGameParticipant { + public void playTurn(String participant, Map.Entry participantEntry, Map> Cards) { + + } + + @Override + public boolean shouldContinue() { + return true; + } + } + + static class Dealer implements CardGameParticipant { + @Override + public void playTurn(String participant, Map.Entry participantEntry, Map> Cards) { + + } + + @Override + public boolean shouldContinue() { + return false; + } + } +} From c57f595bce9c16b84682f6d9d054ff14c8009366 Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Wed, 31 Jan 2024 01:06:30 +0900 Subject: [PATCH 10/11] add @Override --- src/main/java/blackjack/canReceiveCard.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/blackjack/canReceiveCard.java b/src/main/java/blackjack/canReceiveCard.java index bfde9f8a5..1986c62f6 100644 --- a/src/main/java/blackjack/canReceiveCard.java +++ b/src/main/java/blackjack/canReceiveCard.java @@ -50,6 +50,7 @@ public static void playTurn(CardGameParticipant participant, String name, Map.En } static class Participant implements CardGameParticipant { + @Override public void playTurn(String participant, Map.Entry participantEntry, Map> Cards) { } From dbb7168b9c28d8e5143fda9f35e5447e527bdec9 Mon Sep 17 00:00:00 2001 From: Revivekirin Date: Wed, 31 Jan 2024 01:29:32 +0900 Subject: [PATCH 11/11] =?UTF-8?q?rule=20=ED=8C=8C=EC=9D=BC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/blackjack/.DS_Store | Bin 6148 -> 6148 bytes src/main/java/blackjack/Application.java | 7 ++++ src/main/java/blackjack/BlackJackGame.java | 33 ----------------- src/main/java/blackjack/CardDictionary.java | 1 - src/main/java/blackjack/Participant.java | 1 + .../java/blackjack/rule/BlackJackGame.java | 34 ++++++++++++++++++ .../{ => rule}/CardGameParticipant.java | 3 +- .../blackjack/{ => rule}/DecideResult.java | 3 +- .../{ => rule}/OperationWithCard.java | 4 +-- .../blackjack/{ => rule}/canReceiveCard.java | 4 +-- src/main/java/blackjack/view/OutView.java | 2 +- 11 files changed, 51 insertions(+), 41 deletions(-) delete mode 100644 src/main/java/blackjack/BlackJackGame.java create mode 100644 src/main/java/blackjack/rule/BlackJackGame.java rename src/main/java/blackjack/{ => rule}/CardGameParticipant.java (83%) rename src/main/java/blackjack/{ => rule}/DecideResult.java (94%) rename src/main/java/blackjack/{ => rule}/OperationWithCard.java (94%) rename src/main/java/blackjack/{ => rule}/canReceiveCard.java (95%) diff --git a/src/main/java/blackjack/.DS_Store b/src/main/java/blackjack/.DS_Store index 93142988b6527640a2905a094ba89471e2559357..dab9e6658cb91f416d097d9be9385e0c36735cc8 100644 GIT binary patch delta 190 zcmZoMXfc=|#>B`mF;Q%yo+6MAu%0Zy*ig^Gz`(%55X4Z*ki(FglWrKCoS$0&6oUb{ zTyDOLOHxjL5>SG}-ppXCJ*F;Q%yo+2a5!~knXmd!1U&1{nen0{<#=iui6%5G+4`p!I=U&N4+ OVX_U6^yV0m70dwVhY!L4 diff --git a/src/main/java/blackjack/Application.java b/src/main/java/blackjack/Application.java index cdbc47b31..923b1866f 100644 --- a/src/main/java/blackjack/Application.java +++ b/src/main/java/blackjack/Application.java @@ -1,6 +1,7 @@ package blackjack; import blackjack.view.InputView; +import blackjack.rule.*; import java.util.Arrays; import java.util.List; @@ -10,18 +11,22 @@ public class Application { public static void main(String[] args) { + // initializing card_dictionary Map> Cards = CardDictionary.createCardDictionary(); + // initializing participant, dealer List Dealer = Arrays.asList("딜러"); List participants = InputView.getInput("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)", s -> Arrays.asList(s.split(","))); ParticipantSize(participants); //예외 처리 Collections.shuffle(participants); + // adding two card Map participantStates = BlackJackGame.simulateCardGame(participants, Cards); Map dealerState = BlackJackGame.simulateCardGame(Dealer, Cards); int DealerSum = 0; + // evaluate between 16 and 21 (participant) for (String participant : participants) { for (Map.Entry entry : participantStates.entrySet()) { entry.getValue().getName(participant); @@ -29,11 +34,13 @@ public static void main(String[] args) { } } + // evaluate between 16 and 21 (dealer) for (Map.Entry entry : dealerState.entrySet()) { canReceiveCard.DealerBetween16And21(Dealer.get(0), entry, Cards); DealerSum = entry.getValue().printsum(); } + // Comparing DecideResult.CompareWithDealer(participants, DealerSum, participantStates); } diff --git a/src/main/java/blackjack/BlackJackGame.java b/src/main/java/blackjack/BlackJackGame.java deleted file mode 100644 index 192966ac8..000000000 --- a/src/main/java/blackjack/BlackJackGame.java +++ /dev/null @@ -1,33 +0,0 @@ -package blackjack; - -import java.util.*; - - -public class BlackJackGame { - public static Map simulateCardGame(List participants, Map> Cards) { - Map participantStates = new HashMap<>(); - - for (String participant : participants) { - participantStates.put(participant, new Participant.ParticipantState()); - } - - for (String participant : participants) { - Participant.ParticipantState participantState = participantStates.get(participant); - OperationWithCard.performCardOperation(Cards, shuffledCard -> { - int value = shuffledCard.getValue(); - String cardName = shuffledCard.getKey(); - participantState.updateSumAndNameList(value, cardName); - }); - } - return participantStates; - - } - -} - - - - - - - diff --git a/src/main/java/blackjack/CardDictionary.java b/src/main/java/blackjack/CardDictionary.java index f59eabcb4..01fbf99aa 100644 --- a/src/main/java/blackjack/CardDictionary.java +++ b/src/main/java/blackjack/CardDictionary.java @@ -37,4 +37,3 @@ public static Map> createCardDictionary() { return Cards; //HashMap [[key: 카드 이름, value = 카드 숫자]] } } -// Making functional interface!!!!!!!!! \ No newline at end of file diff --git a/src/main/java/blackjack/Participant.java b/src/main/java/blackjack/Participant.java index 877fd385d..c961a9fb6 100644 --- a/src/main/java/blackjack/Participant.java +++ b/src/main/java/blackjack/Participant.java @@ -1,6 +1,7 @@ package blackjack; import java.util.*; +import blackjack.rule.*; public class Participant { public static class ParticipantState { diff --git a/src/main/java/blackjack/rule/BlackJackGame.java b/src/main/java/blackjack/rule/BlackJackGame.java new file mode 100644 index 000000000..f7555b7c9 --- /dev/null +++ b/src/main/java/blackjack/rule/BlackJackGame.java @@ -0,0 +1,34 @@ +package blackjack.rule; + +import java.util.*; +import blackjack.Participant; + + +public class BlackJackGame { + public static Map simulateCardGame(List participants, Map> Cards) { + Map participantStates = new HashMap<>(); + + for (String participant : participants) { + participantStates.put(participant, new blackjack.Participant.ParticipantState()); + } + + for (String participant : participants) { + blackjack.Participant.ParticipantState participantState = participantStates.get(participant); + OperationWithCard.performCardOperation(Cards, shuffledCard -> { + int value = shuffledCard.getValue(); + String cardName = shuffledCard.getKey(); + participantState.updateSumAndNameList(value, cardName); + }); + } + return participantStates; + + } + +} + + + + + + + diff --git a/src/main/java/blackjack/CardGameParticipant.java b/src/main/java/blackjack/rule/CardGameParticipant.java similarity index 83% rename from src/main/java/blackjack/CardGameParticipant.java rename to src/main/java/blackjack/rule/CardGameParticipant.java index d44fa54f4..338fa38e1 100644 --- a/src/main/java/blackjack/CardGameParticipant.java +++ b/src/main/java/blackjack/rule/CardGameParticipant.java @@ -1,7 +1,8 @@ -package blackjack; +package blackjack.rule; import java.util.List; import java.util.Map; +import blackjack.Participant; public interface CardGameParticipant { void playTurn(String participant, Map.Entry participantEntry, Map> Cards); diff --git a/src/main/java/blackjack/DecideResult.java b/src/main/java/blackjack/rule/DecideResult.java similarity index 94% rename from src/main/java/blackjack/DecideResult.java rename to src/main/java/blackjack/rule/DecideResult.java index abbdfa3d2..ec686b395 100644 --- a/src/main/java/blackjack/DecideResult.java +++ b/src/main/java/blackjack/rule/DecideResult.java @@ -1,6 +1,7 @@ -package blackjack; +package blackjack.rule; import blackjack.view.OutView; +import blackjack.Participant; import java.util.List; import java.util.Map; diff --git a/src/main/java/blackjack/OperationWithCard.java b/src/main/java/blackjack/rule/OperationWithCard.java similarity index 94% rename from src/main/java/blackjack/OperationWithCard.java rename to src/main/java/blackjack/rule/OperationWithCard.java index bc2dc3f14..a5f11832e 100644 --- a/src/main/java/blackjack/OperationWithCard.java +++ b/src/main/java/blackjack/rule/OperationWithCard.java @@ -1,9 +1,9 @@ -package blackjack; +package blackjack.rule; import java.util.*; public class OperationWithCard { - interface CardOperation { + public interface CardOperation { void operate(Map.Entry shuffledCard); } diff --git a/src/main/java/blackjack/canReceiveCard.java b/src/main/java/blackjack/rule/canReceiveCard.java similarity index 95% rename from src/main/java/blackjack/canReceiveCard.java rename to src/main/java/blackjack/rule/canReceiveCard.java index 1986c62f6..4fc008b9e 100644 --- a/src/main/java/blackjack/canReceiveCard.java +++ b/src/main/java/blackjack/rule/canReceiveCard.java @@ -1,8 +1,9 @@ -package blackjack; +package blackjack.rule; import blackjack.view.InputView; import java.util.List; import java.util.Map; +import blackjack.Participant; public class canReceiveCard { public static void ParticipantBetween16And21(String participant, Map.Entry participantEntry, Map> Cards) { @@ -39,7 +40,6 @@ public static void playTurn(CardGameParticipant participant, String name, Map.En } else { break; } - //16과 21인지 판단하고 만약 중간이라면 다시 SumBetween16and21 실행 아니면 continue } else if ("n".equalsIgnoreCase(answer)) { System.out.println(name + "카드: " + entry.getValue().printcardlist()); break; diff --git a/src/main/java/blackjack/view/OutView.java b/src/main/java/blackjack/view/OutView.java index 7fe9a340b..a5d7758f3 100644 --- a/src/main/java/blackjack/view/OutView.java +++ b/src/main/java/blackjack/view/OutView.java @@ -7,7 +7,7 @@ public class OutView { public static void printFinalResults(int win, int loss, Map participantStates) { System.out.println("## 최종 승패"); - System.out.println("딜러: " + win + "승 " + loss + '패'); + System.out.println("딜러: " + win + "승 " + loss + "패"); for (Map.Entry entry : participantStates.entrySet()) { String name = entry.getValue().printname();