按照斗地主的规则,完成洗牌发牌的动作。具体规则为使用 54 张牌打乱顺序,3 个玩家参与游戏,3 人交替摸牌,每人 17 张牌,后 3 张留作底牌。
实现思路步骤如下:
- 准备牌:Arrylist 集合储存,for 循环遍历实现
-
洗牌:使用 Collections 的
shuffle(arryBox);
方法刷新集合中的顺序 - 发牌:使用 for 循环遍历 Arrylist 中的 51 张牌,然后用索引值对 3 取余,余数为 0,1,2 然后来随机分配牌。
- 看牌:直接输出集合
实现代码如下:
public class Main { public static void main(String[] args) { // 1. 准备牌 String[] arr1 = { "黑桃","红桃","方片","梅花" }; String[] arr2 = { "A","2","3","4","5","6","7","8","9","10","J","Q","K" }; List<String> arryBox = new ArrayList<>(); for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr2.length; j++) { arryBox.add(arr1[i] + arr2[j]); } } arryBox.add("大王"); arryBox.add("小王"); // 3个人斗地主,分别为 zhangsan、lisi、wangwu List<String> zhangsan = new ArrayList<>(); List<String> lisi = new ArrayList<>(); List<String> wangwu = new ArrayList<>(); // 2. 洗牌 Collections.shuffle(arryBox); // 3. 发牌 for (int i = 0; i < arryBox.size() - 3; i++) { if (i % 3 == 0) { zhangsan.add(arryBox.get(i)); } else if (i % 3 == 1) { lisi.add(arryBox.get(i)); } else if (i % 3 == 2) { wangwu.add(arryBox.get(i)); } } // 4. 看牌 pushBoss(); System.out.println("张三:" + zhangsan); System.out.println("李四:" + lisi); System.out.println("王五:" + wangwu); System.out.print("底牌:["); for (int i = 1; i < 4; i++) { System.out.print(arryBox.get(arryBox.size() - i)); if (i < 3) { System.out.print(","); } } System.out.print("]"); } /** 随机地主 */ public static void pushBoss() { List<String> players = new ArrayList<String>(); players.add("张三"); players.add("李四"); players.add("王五"); Random r = new Random(); int bossIndex = r.nextInt(3); String boss = players.get(bossIndex); System.out.println("此局地主是:" + boss); } }运行上面程序,输出结果为:
此局地主是:李四
张三: [梅花10,方片2,方片K,方片J,方片5,黑桃6,黑桃10,红桃3,梅花3,黑桃2,梅花8,黑桃A,红桃J,梅花9,红桃K,方片9,梅花Q]
李四: [红桃4,黑桃8,方片8,方片3,红桃2,红桃6,红桃5,黑桃3,黑桃4,梅花7,方片Q,小王,红桃8,黑桃K,方片6,梅花A,黑桃7]
王五: [方片4,梅花4,方片7,黑桃5,黑桃9,红桃10,梅花K,方片A,红桃Q,黑桃J,梅花2,大王,方片10,红桃A,梅花J,梅花5,红桃7]
底牌:[梅花6,红桃9,黑桃Q]
public class Main2 { public static void main(String[] args) { // 1.准备牌 String[] arr1 = { "黑桃","K" }; // 定义Map集合用来存放索引和牌 HashMap<Integer,String> pokerMap = new HashMap<>(); // 定义List集合存储索引(索引为0-53) List<Integer> indexList = new ArrayList<>(); // 定义索引值变量 int index = 0; // 将扑克牌与索引建立对应关系放入Map和List集合中 for (String num : arr2) { for (String color : arr1) { pokerMap.put(index,color + num); indexList.add(index); index++; } } pokerMap.put(index,"小王"); indexList.add(index++); pokerMap.put(index,"大王"); indexList.add(index); // 定义玩家 TreeSet<Integer> zhangsan = new TreeSet<>(); TreeSet<Integer> lisi = new TreeSet<>(); TreeSet<Integer> wangwu = new TreeSet<>(); TreeSet<Integer> buttoms = new TreeSet<>(); // 2. 洗牌 Collections.shuffle(indexList); // 3. 发牌 for (int i = 0; i < indexList.size(); i++) { if (i >= indexList.size() - 3) { buttoms.add(indexList.get(i)); } else if (i % 3 == 0) { zhangsan.add(indexList.get(i)); } else if (i % 3 == 1) { lisi.add(indexList.get(i)); } else { wangwu.add(indexList.get(i)); } } // 4. 看牌 pushBoss(); check(pokerMap,zhangsan,"张三"); check(pokerMap,lisi,"李四"); check(pokerMap,wangwu,"王五"); check(pokerMap,buttoms,"底牌"); } /** * 看牌 * * @param pokerMap * @param player 玩家扑克牌对应索引集合 * @param name 玩家昵称 */ public static void check(HashMap<Integer,String> pokerMap,TreeSet<Integer> player,String name) { // 查看手中的牌 System.out.print(name + ":["); for (Integer i : player) { System.out.print(pokerMap.get(i) + " "); } System.out.println("]"); } /** 随机地主 */ public static void pushBoss() { List<String> players = new ArrayList<String>(); players.add("张三"); players.add("李四"); players.add("王五"); Random r = new Random(); int bossIndex = r.nextInt(3); String boss = players.get(bossIndex); System.out.println("此局地主是:" + boss); } }运行上面程序,输出结果为:
此局地主是:张三
张三:[方片A 红桃2 方片2 黑桃4 红桃4 梅花4 红桃6 方片6 梅花6 黑桃7 红桃8 黑桃9 黑桃10 方片J 方片Q 黑桃K 红桃K ]
李四:[梅花2 黑桃3 红桃3 方片3 梅花3 红桃5 方片5 红桃7 梅花8 红桃9 梅花9 红桃10 梅花10 黑桃Q 梅花Q 梅花K 小王 ]
王五:[黑桃A 红桃A 黑桃2 方片4 黑桃5 黑桃6 方片7 黑桃8 方片8 方片9 方片10 黑桃J 红桃J 梅花J 红桃Q 方片K 大王 ]
底牌:[梅花A 梅花5 梅花7 ]