进阶day04-Map实现摸牌案例
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
package com.inmind.test04;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
|
||||
/*
|
||||
按照斗地主的规则,完成洗牌发牌的动作。
|
||||
(双列集合实现,不要使用自定义类)
|
||||
@@ -17,4 +23,99 @@ package com.inmind.test04;
|
||||
注意:最后打印牌时,要有排序的效果
|
||||
*/
|
||||
public class Test14 {
|
||||
public static void main(String[] args) {
|
||||
//1.准备54张牌(花色+数字)
|
||||
HashMap<Integer, String> pokerMap = new HashMap<>();
|
||||
//创建List来保存键值,针对键值进行打乱,摸牌,最后再根据键找值,拿到真正的牌即可
|
||||
ArrayList<Integer> keys = new ArrayList<>();
|
||||
|
||||
String[] nums = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
|
||||
String[] colors = { "♣", "♦","♥","♠"};
|
||||
|
||||
int index = 1;
|
||||
for (String num : nums) {
|
||||
for (String color : colors) {
|
||||
String poker = color + num;
|
||||
//将花色和排序整数,作为一个键值对,保存到map中
|
||||
pokerMap.put(index, poker);
|
||||
keys.add(index);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
//添加大小王
|
||||
pokerMap.put(index, "小王");
|
||||
keys.add(index);
|
||||
index++;
|
||||
pokerMap.put(index, "大王");
|
||||
keys.add(index);
|
||||
index++;
|
||||
System.out.println(pokerMap.size());
|
||||
System.out.println(pokerMap);
|
||||
System.out.println(keys);
|
||||
|
||||
//2.洗牌(打乱顺序)
|
||||
Collections.shuffle(keys);
|
||||
System.out.println(keys);
|
||||
|
||||
//3.创建3个集合对象保存每个玩家的牌,创建1个集合保存底牌(摸牌就是模指定的键即可,因为后期可以键(1~54)找值)
|
||||
ArrayList<Integer> player1 = new ArrayList<>();
|
||||
ArrayList<Integer> player2 = new ArrayList<>();
|
||||
ArrayList<Integer> player3 = new ArrayList<>();
|
||||
ArrayList<Integer> dipai = new ArrayList<>();
|
||||
|
||||
/*
|
||||
按顺序,轮着抓
|
||||
玩家1 0 3 6 index%3 == 0
|
||||
玩家2 1 4 7 index%3 == 1
|
||||
玩家3 2 5 8 index%3 == 2
|
||||
|
||||
最后三张牌都给底牌
|
||||
*/
|
||||
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
Integer key = keys.get(i);
|
||||
//最后三张都给底牌,其他的判断下分别发给我每个玩家
|
||||
if (i >= 51) {//最后三张都给底牌
|
||||
dipai.add(key);
|
||||
} else {
|
||||
switch (i%3) {
|
||||
case 0:
|
||||
player1.add(key);
|
||||
break;
|
||||
case 1:
|
||||
player2.add(key);
|
||||
break;
|
||||
case 2:
|
||||
player3.add(key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//4.打印集合的内容
|
||||
showPokers("玩家1",player1,pokerMap);
|
||||
showPokers("玩家2",player2,pokerMap);
|
||||
showPokers("玩家3",player3,pokerMap);
|
||||
showPokers("底牌",dipai,pokerMap);
|
||||
}
|
||||
|
||||
//定义一个方法,来接收每个玩家手中的键值集合,根据双列集合找到对应的值,拼接展示即可
|
||||
public static void showPokers(String name, ArrayList<Integer> keys, HashMap<Integer, String> pokerMap) {
|
||||
//先排序,遍历keys,找到值,拼接
|
||||
Collections.sort(keys, new Comparator<Integer>() {
|
||||
@Override
|
||||
public int compare(Integer o1, Integer o2) {
|
||||
return o2-o1;
|
||||
}
|
||||
});//自然排序
|
||||
|
||||
String pokers = "";
|
||||
for (Integer key : keys) {
|
||||
String poker = pokerMap.get(key);
|
||||
pokers += poker + " ";
|
||||
}
|
||||
|
||||
System.out.println(name+":"+pokers);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user