package com.card;
import java.util.Comparator;
import java.util.*;
public class Card implements Comparable<Card> {
String size;// 花色
String point;// 点数
public Card(String size, String point) {
this.size = size;
this.point = point;
}
public String toString() {
return size + point;
}
public int compareTo(Card o) {
List<String> points = Arrays.asList("2", "3", "4", "5", "6", "7", "8",
"9", "10", "J", "Q", "K", "A");
List<String> sizes = Arrays.asList("方块", "梅花", "红桃", "黑桃");
if (points.indexOf(this.point) == points.indexOf(o.point))
return Integer.valueOf(sizes.indexOf(this.size)).compareTo(
Integer.valueOf(sizes.indexOf(o.size)));
else
return Integer.valueOf(points.indexOf(this.point)).compareTo(
Integer.valueOf(points.indexOf(o.point)));
}
}
package com.card;
import java.util.*;
import java.util.Set;
public class Player {
int id;
String name;
List<Card> cards;
public Player(int id, String name) {
this.id = id;
this.name = name;
cards = new ArrayList<Card>();
}
public Player() {
}
}
package com.card;
import java.util.*;
public class Test {
static List<Card> listCard;
Player player1;
Player player2;
public static void main(String[] args) {
Test test = new Test();
System.out.println("------------创建扑克牌------------");
System.out.println("------------创建扑克牌成功------------");
System.out.print("为:[");
test.cardCreate();
System.out.println("]");
System.out.println("------------开始洗牌------------");
Collections.shuffle(listCard);
System.out.println("------------洗牌结束------------");
System.out.println("------------创建玩家------------");
test.setPlayer();
System.out.println("------------开始发牌------------");
test.deal();
}
public void deal() {
System.out.println("玩家" + player1.name + "拿牌");
player1.cards.add(listCard.get(0));
System.out.println("玩家" + player2.name + "拿牌");
player2.cards.add(listCard.get(1));
System.out.println("玩家" + player1.name + "拿牌");
player1.cards.add(listCard.get(2));
System.out.println("玩家" + player2.name + "拿牌");
player2.cards.add(listCard.get(3));
System.out.println("------------发牌结束------------");
System.out.println("------------开始游戏------------");
Collections.sort(player1.cards);
System.out.println("玩家" + player1.name + "最大的手牌为"
+ player1.cards.get(1));
Collections.sort(player2.cards);
System.out.println("玩家" + player2.name + "最大的手牌为"
+ player2.cards.get(1));
Map<Card, String> cardMap = new HashMap<Card, String>();
cardMap.put(player1.cards.get(1), player1.name);
cardMap.put(player2.cards.get(1), player2.name);
Set<Card> set = cardMap.keySet();
List<Card> win = new ArrayList<Card>();
for (Card card : set) {
win.add(card);
}
Collections.sort(win);
System.out.println("------------玩家" + cardMap.get(win.get(1))
+ "获胜!------------");
System.out.println("玩家" + player1.name + "最大的手牌为:["
+ player1.cards.get(0) + "," + player1.cards.get(1) + "]");
System.out.println("玩家" + player2.name + "最大的手牌为:["
+ player2.cards.get(0) + "," + player2.cards.get(1) + "]");
}
public void setPlayer() {
Scanner console = new Scanner(System.in);
System.out.println("请输入第一个玩家的Id和姓名:");
System.out.println("请输入Id:");
int id1, id2;
try {
id1 = console.nextInt();
} catch (InputMismatchException e) {
console.next();
System.out.println("请输入整数数值:");
id1 = console.nextInt();
}
System.out.println("请输入姓名:");
String name1 = console.next();
player1 = new Player(id1, name1);
System.out.println("请输入第二个玩家的Id和姓名:");
System.out.println("请输入Id:");
try {
id2 = console.nextInt();
} catch (InputMismatchException e) {
console.next();
System.out.println("请输入整数数值:");
id2 = console.nextInt();
}
System.out.println("请输入姓名:");
String name2 = console.next();
player2 = new Player(id2, name2);
System.out.println("欢迎玩家" + name1);
System.out.println("欢迎玩家" + name2);
}
public void cardCreate() {
listCard = new ArrayList<Card>();
Random random = new Random();
for (int i = 2; i <= 10; i++) {
listCard.add(new Card("方片", Integer.toString(i)));
}
listCard.add(new Card("方片", "J"));
listCard.add(new Card("方片", "Q"));
listCard.add(new Card("方片", "K"));
listCard.add(new Card("方片", "A"));
for (int i = 2; i <= 10; i++) {
listCard.add(new Card("黑桃", Integer.toString(i)));
}
listCard.add(new Card("黑桃", "J"));
listCard.add(new Card("黑桃", "Q"));
listCard.add(new Card("黑桃", "K"));
listCard.add(new Card("黑桃", "A"));
for (int i = 2; i <= 10; i++) {
listCard.add(new Card("梅花", Integer.toString(i)));
}
listCard.add(new Card("梅花", "J"));
listCard.add(new Card("梅花", "Q"));
listCard.add(new Card("梅花", "K"));
listCard.add(new Card("梅花", "A"));
for (int i = 2; i <= 10; i++) {
listCard.add(new Card("红桃", Integer.toString(i)));
}
listCard.add(new Card("红桃", "J"));
listCard.add(new Card("红桃", "Q"));
listCard.add(new Card("红桃", "K"));
listCard.add(new Card("红桃", "A"));
for (Card card : listCard) {
System.out.print(card + ",");
}
}
}