# JAVA基础--斗地主案例

• 准备牌

ArrayList<String> poker=new ArrayList<>();
String[] color={"♠","♥","♣","♦"};
String[] number={"A","J","Q","K","2","3","4","5","6","7","8","9","10"};
for (String c : color) {
for (String n : number) {
}
}


[大王, 小王, ♠A, ♠J, ♠Q, ♠K, ♠2, ♠3, ♠4, ♠5, ♠6, ♠7, ♠8, ♠9, ♠10, ♥A, ♥J, ♥Q, ♥K, ♥2, ♥3, ♥4, ♥5, ♥6, ♥7, ♥8, ♥9, ♥10, ♣A, ♣J, ♣Q, ♣K, ♣2, ♣3, ♣4, ♣5, ♣6, ♣7, ♣8, ♣9, ♣10, ♦A, ♦J, ♦Q, ♦K, ♦2, ♦3, ♦4, ♦5, ♦6, ♦7, ♦8, ♦9, ♦10]

• 洗牌

poker被打乱

 /*
* static void shuffle(List<?> list)
*使用默认随机源对指定列表进行置换。
*/
Collections.shuffle(poker);

• 发牌
 		ArrayList<String> player1=new ArrayList<>();
ArrayList<String> player2=new ArrayList<>();
ArrayList<String> player3=new ArrayList<>();
ArrayList<String> dipai=new ArrayList<>();
for (int i = 0; i < poker.size(); i++) {
if (i >= 51) {
} else if (i % 3 == 0) {
} else if (i % 3 == 1) {
} else if (i % 3 == 2) {
}

}

• 看牌
System.out.println(dipai);
System.out.println(player1);
System.out.println(player2);
System.out.println(player3);

• 结果
[♣3, 大王, ♥10]
[♥J, ♦6, ♥8, ♥6, ♣5, ♣A, ♠6, ♦A, ♣9, ♠5, ♣Q, ♣10, ♦5, ♠A, ♥3, ♠Q, ♣4]
[小王, ♦8, ♦Q, ♠K, ♥9, ♥2, ♥5, ♠8, ♥K, ♠9, ♣J, ♠J, ♥4, ♦10, ♥A, ♦9, ♦K]
[♠3, ♠7, ♠10, ♣K, ♦3, ♠2, ♣8, ♦J, ♣6, ♥7, ♠4, ♦4, ♥Q, ♦7, ♦2, ♣2, ♣7]


• 准备牌

（一）中是将扑克牌组合好后存入list集合
（二）将扑克牌存入map<K,V>集合的V，此时扑克牌按照大小排列。并给每张牌按照0-53编号存入map<K,V>集合的键值K。这样每张牌都有一个自己唯一的编号。

	    Map<Integer,String> poker=new HashMap<>();
List<String> colors=List.of("♠","♥","♣","♦");
List<String> numbers=List.of("A","J","Q","K","2","3","4","5","6","7","8","9","10");
int index=0;
poker.put(index,"大王");
index++;
poker.put(index,"小王");
index++;
for (String number:numbers) {
for (String color : colors) {
poker.put(index,color+number);
index++;
}
}



{0=大王, 1=小王, 2=♠A, 3=♥A, 4=♣A, 5=♦A, 6=♠J, 7=♥J, 8=♣J, 9=♦J, 10=♠Q, 11=♥Q, 12=♣Q, 13=♦Q, 14=♠K, 15=♥K, 16=♣K, 17=♦K, 18=♠2, 19=♥2, 20=♣2, 21=♦2, 22=♠3, 23=♥3, 24=♣3, 25=♦3, 26=♠4, 27=♥4, 28=♣4, 29=♦4, 30=♠5, 31=♥5, 32=♣5, 33=♦5, 34=♠6, 35=♥6, 36=♣6, 37=♦6, 38=♠7, 39=♥7, 40=♣7, 41=♦7, 42=♠8, 43=♥8, 44=♣8, 45=♦8, 46=♠9, 47=♥9, 48=♣9, 49=♦9, 50=♠10, 51=♥10, 52=♣10, 53=♦10}


• 洗牌


Set<Integer> pIndex=poker.keySet();//获取poker的键集
List<Integer> pokerIndex=new ArrayList<>();//因为要打乱牌的编号，所以打乱之前先存起来
Collections.shuffle(pokerIndex);//打乱牌的编号


[4, 15, 29, 26, 28, 8, 13, 46, 36, 48, 11, 20, 32, 17, 9, 12, 25, 33, 47, 22, 23, 18, 1, 27, 35, 19, 3, 51, 10, 43, 16, 52, 41, 34, 49, 2, 30, 38, 5, 21, 44, 14, 40, 7, 45, 31, 42, 0, 53, 39, 24, 37, 50, 6]


• 发牌
		ArrayList<Integer> nodipai=new ArrayList<>();
ArrayList<Integer> no01=new ArrayList<>();
ArrayList<Integer> no02=new ArrayList<>();
ArrayList<Integer> no03=new ArrayList<>();
//将牌的编号发给玩家
for (Integer in : pokerIndex) {
if(in>=51){
}else if(in%3==0){
}else if(in%3==1){
}else if(in%3==2){
}
}
//对编号排序
Collections.sort(nodipai);
Collections.sort(no01);
Collections.sort(no02);
Collections.sort(no03);

• 看牌

		//调用方法
lookpoker(nodipai,poker);
lookpoker(no01,poker);
lookpoker(no02,poker);
lookpoker(no03,poker);

//Lookpoker方法
private static void lookpoker(List<Integer> playernum,Map poker) {
for (Integer i : playernum) {
System.out.print(poker.get(i)+" ");
}
System.out.println();
}


♠J ♦6 ♠10
♣A ♣Q ♦Q ♣K ♠2 ♦2 ♠4 ♠5 ♥5 ♣5 ♠6 ♥6 ♣7 ♥9 ♣9 ♥10 ♦10


05-18 1万+

07-31 222
03-12 56
04-17 1700
05-08
02-28 41
01-18
02-29 35