📌
package chap13;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
* EastCard 클래스 구현하기
* 멤버 변수 : int num, boolean isKwang;
* 생성자 : 멤버값들을 입력받아 객체 생성.
* 매개변수가 없는 경우 num=1, isKwang=true인 객체 생성
* 멤버 메서드 : toString() 오버라이딩
* 1k,1,2,.....
*/
class EastCard {
int num;
boolean isKwang;
EastCard() {
this(1, true);
}
EastCard(int num, boolean isKwang) {
this.num = num;
this.isKwang = isKwang;
}
@Override
public String toString() {
return num + ((isKwang) ? "K" : "");
}
}
/*
* CardDeck 클래스 구현하기
* 멤버 변수 : List<EastCard> cards;
* static Map<String,Integer> jokbo;
* 생성자 : 매개변수 없음.
* EastCard 20장을 cards List 객체에 저장 1~10의 숫자를 가진 카드가 각각 2장씩.
* 숫자값이 1,3,8인 카드 중 한장은 isKwang이 true임.
* 메서드 :
* EastCard pick(int idx) : idx번째 카드를 제거하여 리턴.
* EastCard pick() : 임의의번째 카드를 제거하여 리턴.
* void shuffle() : cards 섞어 주는 기능.
* Collections.shuffle 메서드 이용
*/
class CardDeck {
List<EastCard> cards;
static Map<String, Integer> jokbo; // 클래스 변수
// static초기화 : 클래스 변수의 초기화 담당영역
static {
jokbo = new HashMap<>();
jokbo.put("KK", 4000);
for (int i = 1; i <= 10; i++) {
jokbo.put("" + i + i, 3000 + (i * 10));
}
jokbo.put("12", 2060);
jokbo.put("21", 2060);
jokbo.put("14", 2050);
jokbo.put("41", 2050);
jokbo.put("19", 2040);
jokbo.put("91", 2040);
jokbo.put("110", 2030);
jokbo.put("101", 2030);
jokbo.put("410", 2020);
jokbo.put("104", 2020);
jokbo.put("64", 2010);
jokbo.put("46", 2010);
}
CardDeck() { // 생성자 : 인스턴스변수 초기화
cards = new ArrayList<>(); // EastCard 20장을 cards List 객체에 저장
for (int i = 0; i < 20; i++) { // 1~10의 숫자를 가진 카드가 각각 2장씩 전체 20장 카드 저장
cards.add(new EastCard(i % 10 + 1, ((i == 0 || i == 2 || i == 7) ? true : false))); // 숫자값이 1,3,8인 카드 중 한장은
// isKwang이 true임.
}
}
EastCard pick() { // List 객체에서 임의번째의 객체를 제거,변환함
return cards.remove((int) (Math.random() * cards.size()));
}
EastCard pick(int idx) { // List 객체에서 idx번째의 객체를 제거,변환함
// List.remave(int) : index에 해당하는 EastCard를 제거. 리턴
return cards.remove(idx);
}
void shuffle() { // cards 섞어 주는 기능.
// shuffle(List) 메서드 : List객체를 임의로 섞기
Collections.shuffle(cards);
}
@Override
public String toString() {
return cards.toString();
}
}
/*
* Player 클래스
* 멤버 변수 : String name EastCard c1,c2
* 생성자 : 멤버변수값을 입력받아 객체 생성.
* 멤버 메서드 :
* int getScore() : c1,c2 카드의 점수 리턴
* c1,c2 카드가 jokbo에 있는 경우 jokbo 점수 리턴
* jokbo에 없는 경우 (c1.num + c2.num)%10 리턴
* String toString() 오버라이딩 : 이름과 카드2장, 점수를 문자열로 리턴
*/
class Player { // 클래스 생성
String name;
EastCard c1, c2;
Player(String name, EastCard c1, EastCard c2) { // 생성자
this.name = name;
this.c1 = c1;
this.c2 = c2;
}
int getScore() {
Integer score = 0;
if (c1.isKwang && c2.isKwang) {
score = CardDeck.jokbo.get("KK");
} else {
score = CardDeck.jokbo.get("" + c1.num + c2.num);
if (score == null) {
score = (c1.num + c2.num) % 10;
}
}
return score;
}
@Override
public String toString() {
return name + "("+c1+","+c2+"):"+getScore();
}
}
public class EastCardEx1 {
public static void main(String[] args) {
System.out.println(new EastCard()); // 1K
System.out.println(new EastCard(9, false)); // 9
CardDeck deck = new CardDeck();
System.out.println(deck); // toString 메서드 호출
deck.shuffle();
System.out.println(deck);
List<Player> list = new ArrayList<>();
list.add(new Player("홍길동", deck.pick(0), deck.pick(0)));
list.add(new Player("김삿갓", deck.pick(0), deck.pick(0)));
list.add(new Player("이몽룡", deck.pick(0), deck.pick(0)));
System.out.println(list);
System.out.println("Dsck에 남은 카드 갯수:"+deck.cards.size());
System.out.println("남은 카드:"+deck.cards);
/*
* Player 중 가장 높은 점수를 가진 Player의 이름 출력
* 예시 : 홍길동 승리.
* 1등과 2등의 점수가 같은 경우 홍길동,김삿갓 비김.
*/
Collections.sort(list,(p1,p2)->p2.getScore()-p1.getScore()); //점수합계 내림차순 정렬(큰점수에서 작은점수로)
System.out.println("점수합계 내림차순 정렬 : "+list);
if(list.get(0).getScore() == list.get(1).getScore()) {
System.out.println(list.get(0).name+","+list.get(1).name+" :비김");
} else {
System.out.println(list.get(0).name+" :승리");
}
}
}
'수업(국비지원) > Java' 카테고리의 다른 글
[Java] chap13 : 컬렉션 프레임워크 - StackQueue (0) | 2023.04.17 |
---|---|
[Java] chap13 : 컬렉션 프레임워크 - Iterator 인터페이스 (0) | 2023.04.17 |
[Java] chap13 : 컬렉션 프레임워크 - Collection(Map, TreeMap) (0) | 2023.04.17 |
[Java] chap13 : 컬렉션 프레임워크 - Collection(Set, LinkedHashSet, TreeSet) (0) | 2023.04.17 |
[Java] chap13 : 컬렉션 프레임워크 - Collection(List) (0) | 2023.04.17 |