目录
1基础版本-发牌
要求
代码实现
结果展示
2升级版扑克牌斗地主
代码实现
结果展示
1基础版本-发牌 要求
给三个玩家依次发牌,最后三张留作底牌。
代码实现学习视频链接:零基础入门Java之集合_哔哩哔哩_bilibili
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; public class DouDiZhu { public static void main(String[] args) { //定义54张牌 ArrayList结果展示poker = new ArrayList<>(); //定义两个数组,分别存储花色和数字 String[] colors ={"♠","♥","♣","♦"}; String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; //将大小王放入扑克集合中 poker.add("大王"); poker.add("小王"); //嵌套循环遍历两个花色和数字数组,组装好52张牌 for(String number:numbers){ for (String color : colors) { //将53张牌放入扑克集合中 poker.add(color+number); } } //调用Collections工具类中的shuffle方法,将集合中的顺序打乱 Collections.shuffle(poker); System.out.println(poker.toString()); //定义4个集合 分别存放3个玩家的牌和底牌 ArrayList player1 = new ArrayList<>(); ArrayList player2 = new ArrayList<>(); ArrayList player3 = new ArrayList<>(); ArrayList Dipai = new ArrayList<>();//剩余3张底牌 for(int i = 0;i =51){//最后三张给底牌 Dipai.add(poker.get(i)); }else { if(i%3 ==0){//依次发牌 player1.add(poker.get(i)); } if(i%3 ==1){ player2.add(poker.get(i)); } if(i%3 ==2){ player3.add(poker.get(i)); } } } System.out.println("1号:"); System.out.println(player1.toString()); System.out.println("2号:"); System.out.println(player2.toString()); System.out.println("3号:"); System.out.println(player3.toString()); System.out.println("底牌:"); System.out.println(Dipai.toString()); } }
打印出洗好的牌,3位玩家的牌和底牌。
2升级版扑克牌斗地主使用HashMap组成1副有顺序的扑克牌,给每个牌添加一个索引放入ArrayList集合中,洗牌发牌都 *** 作ArrayList集合,然后通过工具类调用sort方法,对索引进行排序,最后看牌时通过索引找到对应的value值即可实现。
代码实现import java.util.*; public class DouDiZhu { public static void main(String[] args) { HashMap结果展示map = new HashMap<>(); //定义两个数组,分别存储花色和数字 String[] colors = {"♠", "♥", "♣", "♦"}; String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"}; List arrayList = new ArrayList<>(); //扑克的索引 int i = 0; arrayList.add(i); //放入大小王 map.put(i++, "大王"); arrayList.add(1); map.put(i++, "小王"); //组合剩余52张牌,放入HashMap集合中 for (String number : numbers) { for (String color : colors) { //把扑克的索引都放入ArrayList中 arrayList.add(i); map.put(i++, color + number); } } //进行洗牌 Collections.shuffle(arrayList); ArrayList arrayList1 = new ArrayList<>(); ArrayList arrayList2 = new ArrayList<>(); ArrayList arrayList3 = new ArrayList<>(); ArrayList DiPai = new ArrayList<>(); for (int j = 0; j < arrayList.size(); j++) { Integer in = arrayList.get(j); if (j >= 51) { DiPai.add(in); } else { if (j % 3 == 0) { arrayList1.add(in); } if (j % 3 == 1) { arrayList2.add(in); } if (j % 3 == 2) { arrayList3.add(in); } } } Collections.sort(arrayList1); Collections.sort(arrayList2); Collections.sort(arrayList3); //提供一个看牌方法 提供map,玩家名称,玩家的牌索引 lookpoker(map, "玩家1", arrayList1); lookpoker(map, "玩家2", arrayList2); lookpoker(map, "玩家3", arrayList3); lookpoker(map, "底牌", DiPai); } private static void lookpoker(Map map, String name, ArrayList arrayList) { System.out.println(name + ":"); for (Integer integer : arrayList) { String value = (String) map.get(integer); System.out.print(value + "t"); } System.out.println(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)