import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
/*
* 思路:
* A:创建一个HashMap集合
* B:创建一个ArrayList集合
* C:创建花色数组和点数数组
* D:从0开始往HashMap里面存储编号,并存储对应的牌
* 同时往ArrayList里面存储编号即可。
* E:洗牌(洗的是编号)
* F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
* G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
*/
public class PokerDemo {
public static void main(String[] args) {
// 创建一个HashMap集合
HashMaphm = new HashMap ();
// 创建一个ArrayList集合
ArrayListarray = new ArrayList ();
// 创建花色数组和点数数组
// 定义一个花色数组
String[] colors = { "♠", "♥", "♣", "♦" };
// 定义一个点数数组
String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
"K", "A", "2", };
// 从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。
int index = 0;
for (String number : numbers) {
for (String color : colors) {
String poker = color.concat(number);
hm.put(index, poker);
array.add(index);
index++;
}
}
hm.put(index, "小王");
array.add(index);
index++;
hm.put(index, "大王");
array.add(index);
// 洗牌(洗的是编号)
Collections.shuffle(array);
// 发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
TreeSetfengQingYang = new TreeSet ();
TreeSetlinQingXia = new TreeSet ();
TreeSetliuYi = new TreeSet ();
TreeSetdiPai = new TreeSet ();
for (int x = 0; x < array.size(); x++) {
if (x >= array.size() - 3) {
diPai.add(array.get(x));
} else if (x % 3 == 0) {
fengQingYang.add(array.get(x));
} else if (x % 3 == 1) {
linQingXia.add(array.get(x));
} else if (x % 3 == 2) {
liuYi.add(array.get(x));
}
}
// 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
lookPoker("风清扬", fengQingYang, hm);
lookPoker("林青霞", linQingXia, hm);
lookPoker("刘意", liuYi, hm);
lookPoker("底牌", diPai, hm);
}
// 写看牌的功能
public static void lookPoker(String name, TreeSetts,
HashMaphm) {
System.out.print(name + "的牌是:");
for (Integer key : ts) {
String value = hm.get(key);
System.out.print(value + " ");
}
System.out.println();
}
}
public class Card {
private String face;
private String suit;
public String getFace() {
return face;
}
public void setFace(String face) {
this.face = face;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public Card(String face, String suit) {
super();
this.face = face;
this.suit = suit;
}
public Card() {
// TODO Auto-generated constructor stub
}
----------------------------------------------------------------------------------
public class Consts {
public static final String BLACK = "3"; //黑桃
public static final String RED = "2"; //红桃
public static final String CLUB = "1"; //梅花
public static final String DIAMOND = "0";//方块
}
------------------------------------------------------------------------------------
public class OrderTool {
public List
List
// 按类排序
if (card_array != null && card_array.length > 0) {
Card temp = null;
int flag = 0;
for (int i = 0; i < card_array.length; i++) {
if (flag > 0) {
System.out.println("请检查传入的纸牌值!");
break;
}
for (int j = i + 1; j < card_array.length; j++) {
int i_suit = this.convert(card_array[i].getFace());
int j_suit = this.convert(card_array[j].getFace());
if (i_suit == -1 || j_suit == -1) {
flag++;
break;
}
if (i_suit > j_suit) {
temp = card_array[i];
card_array[i] = card_array[j];
card_array[j] = temp;
} else if (i_suit == j_suit) {
int i_face = Integer.parseInt(card_array[i].getSuit());
int j_face = Integer.parseInt(card_array[j].getSuit());
if (i_face < j_face) {
temp = card_array[i];
card_array[i] = card_array[j];
card_array[j] = temp;
}
}
}
}
for(Card card : card_array){
list.add(card);
}
}
return list;
}
private int convert(String param) {
int res = 0;
try {
res = Integer.parseInt(param);
return res;
} catch (Exception e) {
if ("A".equalsIgnoreCase(param)) {
return 1;
} else if ("J".equalsIgnoreCase(param)) {
return 11;
} else if ("Q".equalsIgnoreCase(param)) {
return 12;
} else if ("K".equalsIgnoreCase(param)) {
return 13;
} else {
System.out.println("拿的是王吧!");
return -1;
}
}
}
public static String faceTranslater(int face){
String res = "";
switch(face){
case 3:res = "黑桃";break;
case 2:res = "红桃";break;
case 1:res = "梅花";break;
case 0:res = "方块";break;
}
return res;
}
//测试
public static void main(String[] args) {
Card c0 = new Card("A",Consts.BLACK);
Card c1 = new Card("2",Consts.BLACK);
Card c2 = new Card("A",Consts.RED);
Card c3 = new Card("5",Consts.CLUB);
Card c4 = new Card("4",Consts.RED);
Card c5 = new Card("3",Consts.DIAMOND);
Card c6 = new Card("4",Consts.BLACK);
Card[] c = {c0,c1,c2,c3,c4,c5,c6};
List
for(Card card : list){
System.out.println("花色:"+OrderTool.faceTranslater(Integer.parseInt(card.getSuit()))+"&"+"牌面:"+card.getFace());
}
}