s-day04--斗地主案例1
This commit is contained in:
@@ -0,0 +1,108 @@
|
|||||||
|
package com.inmind.map01;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Collection:单列集合父接口
|
||||||
|
List:
|
||||||
|
ArrayList:底层是object[],查询快,增删慢
|
||||||
|
LinkedList:底层是双向链接,查询慢,增删快
|
||||||
|
Set:
|
||||||
|
HashSet:底层哈希表,在实现去重的基础上,能够高效查询,HashCode,equals重写
|
||||||
|
LinkedHashSet:底层哈希表+链表,实现Set集合的有序
|
||||||
|
Map:
|
||||||
|
HashMap:存取无序的,不可重复的双列集合
|
||||||
|
LinkedhashMap:存取有序,不可重复的双列集合
|
||||||
|
-----------------------------------------------------
|
||||||
|
Map<Integer,String>
|
||||||
|
♠3>♥3>♦3>♣3
|
||||||
|
案例介绍
|
||||||
|
按照斗地主的规则,完成洗牌发牌的动作。(洗牌打乱Collections.shuffle()只能打乱list集合)
|
||||||
|
具体规则:
|
||||||
|
组装54张扑克牌将54张牌顺序打乱
|
||||||
|
三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。(创建4个集合,3个玩家,1个底牌)
|
||||||
|
查看三人各自手中的牌(按照牌的大小排序)、底牌【自然排序或者比较器Comparable Comparator】
|
||||||
|
规则:手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3【打印牌即可】
|
||||||
|
|
||||||
|
54键----“大王”
|
||||||
|
*/
|
||||||
|
public class Test08 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
|
||||||
|
String[] colors = {"♦", "♣", "♥", "♠"};
|
||||||
|
//准备54张牌,保存到map中
|
||||||
|
//自己维护牌的序号
|
||||||
|
Map<Integer, String> map = new HashMap<>();
|
||||||
|
//准备一个只保存牌号的List集合
|
||||||
|
ArrayList<Integer> pokerIndexs = new ArrayList<>();
|
||||||
|
int index = 1;
|
||||||
|
for (String num : nums) {
|
||||||
|
for (String color : colors) {
|
||||||
|
String poker = color+num;
|
||||||
|
map.put(index, poker);
|
||||||
|
pokerIndexs.add(index);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
map.put(index++, "小王");
|
||||||
|
pokerIndexs.add(53);
|
||||||
|
map.put(index++, "大王");
|
||||||
|
pokerIndexs.add(54);
|
||||||
|
System.out.println(map);//54张牌
|
||||||
|
System.out.println(pokerIndexs);
|
||||||
|
System.out.println(map.size());
|
||||||
|
|
||||||
|
//洗牌
|
||||||
|
Collections.shuffle(pokerIndexs);
|
||||||
|
System.out.println(pokerIndexs);
|
||||||
|
//(创建4个集合,3个玩家,1个底牌)
|
||||||
|
ArrayList<Integer> play1 = new ArrayList<>();
|
||||||
|
ArrayList<Integer> play2 = new ArrayList<>();
|
||||||
|
ArrayList<Integer> play3 = new ArrayList<>();
|
||||||
|
ArrayList<Integer> dipai = new ArrayList<>();
|
||||||
|
/*
|
||||||
|
玩家1 0 3 6 %3 = 0
|
||||||
|
玩家2 1 4 7 %3 = 1
|
||||||
|
玩家3 2 5 8 %3 = 2
|
||||||
|
*/
|
||||||
|
for (int i = 0; i < pokerIndexs.size(); i++) {
|
||||||
|
if (i > 50) {
|
||||||
|
dipai.add(pokerIndexs.get(i));
|
||||||
|
} else {
|
||||||
|
switch (i%3) {
|
||||||
|
case 0:
|
||||||
|
play1.add(pokerIndexs.get(i));
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
play2.add(pokerIndexs.get(i));
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
play3.add(pokerIndexs.get(i));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
showPokers(play1,map);
|
||||||
|
showPokers(play2,map);
|
||||||
|
showPokers(play3,map);
|
||||||
|
showPokers(dipai,map);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void showPokers(ArrayList<Integer> pokerIndexList,Map<Integer, String> pokerMap) {
|
||||||
|
//从大到小展示:降序
|
||||||
|
Collections.sort(pokerIndexList, new Comparator<Integer>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Integer o1, Integer o2) {
|
||||||
|
return o2-o1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//将牌号映射为牌面
|
||||||
|
String pokerStr = "";
|
||||||
|
for (Integer index : pokerIndexList) {
|
||||||
|
String poker = pokerMap.get(index);
|
||||||
|
pokerStr += poker + " ";
|
||||||
|
}
|
||||||
|
System.out.println(pokerStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user