-
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단계 리퀘스트 #1
base: Revivekirin
Are you sure you want to change the base?
Changes from 3 commits
cf6e7a7
d27f20b
640c3ca
ca685b3
bfc893a
fd63b33
3046e9e
ea86e8d
dc886df
c57f595
dbb7168
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,68 @@ | ||
package blackjack; | ||
|
||
import java.util.HashMap; | ||
import java.util.Arrays; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.Map; | ||
import java.util.Collections; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
Map<String, List<Integer>> Cards = CardDictionary.createCardDictionary(); | ||
|
||
List<String> Dealer = Arrays.asList("딜러"); | ||
List<String> participants = getInput("게임에 참여할 사람의 이름을 입력하세요.(쉼표 기준으로 분리)", s -> Arrays.asList(s.split(","))); | ||
ParticipantSize(participants); //예외 처리 | ||
|
||
Collections.shuffle(participants); | ||
|
||
Map<String, CardGameSimulator.ParticipantState> participantStates = CardGameSimulator.simulateCardGame(participants, Cards); | ||
Map<String, CardGameSimulator.ParticipantState> dealerState = CardGameSimulator.simulateCardGame(Dealer, Cards); | ||
|
||
for (String participant : participants) { | ||
for (Map.Entry<String, CardGameSimulator.ParticipantState> entry : participantStates.entrySet()) { | ||
Between16And21.SumBetween16And21(participant, entry, Cards); | ||
} | ||
} | ||
|
||
|
||
for (Map.Entry<String, CardGameSimulator.ParticipantState> entry : dealerState.entrySet()) { | ||
Between16And21.DealerBetween16And21(Dealer.get(0), entry, Cards); | ||
} | ||
|
||
|
||
for (String participant : participants) { | ||
for (Map.Entry<String, CardGameSimulator.ParticipantState> entry : participantStates.entrySet()) { | ||
DecideResult.CompareWithDealer(); | ||
} | ||
|
||
} | ||
} | ||
|
||
private static <T> T getInput(String prompt, Function<String, T> 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<String> participants) { | ||
if (participants.size() > 25) { | ||
throw new IllegalArgumentException("가능한 참가 인원을 초과하였습니다."); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package blackjack; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Scanner; | ||
|
||
public class Between16And21 { | ||
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. 객체지향은 현실에 있을 법한 네이밍을 가진 객체에서 적절한 책임을 분배하는 것이 중요합니다. |
||
public static void SumBetween16And21(String participant, Map.Entry<String, CardGameSimulator.ParticipantState> participantEntry, Map<String, List<Integer>> 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<String, Integer> 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; | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
public static void DealerBetween16And21(String Dealer, Map.Entry<String, CardGameSimulator.ParticipantState> DealerEntry, Map<String, List<Integer>> 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<String, Integer> extraCard = CardGameSimulator.getRandomCard(Cards); | ||
int extraCardValue = extraCard.getValue(); | ||
String extraCardName = extraCard.getKey(); | ||
DealerState.updateSumAndNameList(extraCardValue, extraCardName); //DealerEntry<name, Map<String, CardGameSimulator.ParticipantState> | ||
break; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, List<Integer>> createCardDictionary() { | ||
Map<String, List<Integer>> Cards = new HashMap<>(); | ||
|
||
for (int i = 2; i <= 10; i++) { | ||
for (String suit : new String[]{"하트", "스페이드", "클로버", "다이아몬드"}) { | ||
String cardName = i + suit; | ||
List<Integer> 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<Integer> values = new ArrayList<>(); | ||
values.add(10); | ||
Cards.put(cardName, values); | ||
} | ||
} | ||
|
||
for (String suit : new String[]{"하트", "스페이드", "클로버", "다이아몬드"}) { | ||
String cardName = suit + "Ace"; | ||
List<Integer> values = new ArrayList<>(); | ||
values.add(1); | ||
values.add(11); | ||
Cards.put(cardName, values); | ||
} | ||
return Cards; //HashMap [[key: 카드 이름, value = 카드 숫자]] | ||
} | ||
} | ||
// Making functional interface!!!!!!!!! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package blackjack; | ||
|
||
import java.util.*; | ||
|
||
|
||
public class CardGameSimulator { | ||
|
||
public static class ParticipantState { | ||
private int sum; | ||
private List<String> 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. 내부 class를 사용해주셨군요! 내부 클래스 어떻게 쓰는가? 내부 클래스 언제 쓰는가? 앞서 말했듯이 저는 inner class를 대부분 사용하지 않습니다. |
||
|
||
public ParticipantState() { | ||
this.sum = 0; | ||
this.cardlist = new ArrayList<>(); | ||
} | ||
|
||
public int printsum() { | ||
return this.sum; | ||
} | ||
|
||
public List<String> printlist() { | ||
return this.cardlist; | ||
} | ||
|
||
public int getSum(Map<String, List<Integer>> 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<String, ParticipantState> simulateCardGame(List<String> participants, Map<String, List<Integer>> Cards) { | ||
Map<String, ParticipantState> 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<String, Integer> shuffledCard); | ||
} | ||
|
||
private static void performCardOperation(Map<String, List<Integer>> Cards, CardOperation operation) { | ||
for(int i = 0; i < 2; i++) { | ||
Map.Entry<String, Integer> shuffledCard = getRandomCard(Cards); | ||
operation.operate(shuffledCard); | ||
} | ||
} | ||
|
||
public static Map.Entry<String, Integer> getRandomCard(Map<String, List<Integer>> Cards) { | ||
String randomCardName = getRandomCardName(Cards.keySet()); | ||
|
||
List<Integer> cardValues = Cards.get(randomCardName); | ||
|
||
int cardvalue = cardValues.get(0); | ||
|
||
Cards.remove(randomCardName); | ||
|
||
return new AbstractMap.SimpleEntry<>(randomCardName, cardvalue); | ||
} | ||
|
||
public static <T> T getRandomCardName(Set<T> keySet) { | ||
int randomIndex = new Random().nextInt(keySet.size()); | ||
Iterator<T> iterator = keySet.iterator(); | ||
for(int i = 0; i< randomIndex; i++) { | ||
iterator.next(); | ||
|
||
} | ||
return iterator.next(); | ||
} | ||
} | ||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package blackjack; | ||
|
||
import java.util.Map; | ||
|
||
public class DecideResult { | ||
public static int DealerScore() { | ||
|
||
} | ||
Revivekirin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public static void CompareWithDealer() { | ||
|
||
} | ||
|
||
// Calculate "딜러" ParticipantState.printsum() | ||
// Compare each participant's sum value with dealer | ||
} |
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.
main 메서드가 static이기 때문에 다른 메서드들도 대부분 static 으로 작성된 것 같군요!!
이에 관련해서 팁을 드리자면
BlackJackGame이란 객체를 만들고 main 에서는
을 main에서 호출하는 것으로 변경하면 static method를 줄일 수 있을 겁니다!!