简介:使用java模拟斗地主的底层原理,让大家更好的理解java。
普通版
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main{ public static void main(String[] args) { ArrayList<String> array = new ArrayList<String>(); // 定义花色数组 String[] colors = {"♦", "♣", "♥", "♠"}; // 定义点数数组 String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; // 创建棋牌 for (String color : colors) { for (String number : numbers) { array.add(color + number); } } array.add("小王"); array.add("大王"); // 发牌,遍历集合,给三个玩家发牌 ArrayList<String> lqxArray = new ArrayList<String>(); ArrayList<String> lyArray = new ArrayList<String>(); ArrayList<String> fqyArray = new ArrayList<String>(); ArrayList<String> dpArray = new ArrayList<String>(); for (int i = 0; i < array.size(); i ++) { String poker = array.get(i); if (i >= array.size() - 3) { dpArray.add(poker); } else if (i % 3 == 0) { lqxArray.add(poker); } else if (i % 3 == 1) { lyArray.add(poker); } else if (i % 3 == 2) { fqyArray.add(poker); } } lookPoker("林青霞", lqxArray); lookPoker("李阳", lyArray); lookPoker("风清扬", fqyArray); lookPoker("底牌", dpArray); } // 看牌的方法 public static void lookPoker(String name, ArrayList<String> array) { System.out.println(name + "的牌是:"); for (String poker : array) { System.out.print(poker + " "); } System.out.println(); } }
运行结果:
升级版
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.TreeSet; public class Main{ public static void main(String[] args) { // 创建HashMap, 键 是编号, 值 是牌 HashMap<Integer, String> hm = new HashMap<Integer, String>(); // 存放编号 ArrayList<Integer> array = new ArrayList<Integer>(); // 定义花色数组 String[] colors = {"♦", "♣", "♥", "♠"}; // 定义点数数组 String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}; // 从0开始往HashMap里面存放编号, 并存储对应的牌,同时往ArrayList里面存储编号 int index = 0; for (String number : numbers) { for (String color : colors) { hm.put(index, color+number); array.add(index ++); } } hm.put(index, "小王"); array.add(index ++); hm.put(index, "大王"); array.add(index); // 洗牌(洗的是牌的编号), 用Collections的shuffle()方法实现 Collections.shuffle(array); // 发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收) TreeSet<Integer> lqxSet = new TreeSet<Integer>(); TreeSet<Integer> lySet = new TreeSet<Integer>(); TreeSet<Integer> fqySet = new TreeSet<Integer>(); TreeSet<Integer> dpSet = new TreeSet<Integer>(); for (int i = 0; i < array.size(); ++ i) { int x = array.get(i); if (i >= array.size() - 3) { dpSet.add(x); } else if(i % 3 == 0) { lqxSet.add(x); } else if (i % 3 == 1) { lySet.add(x); } else if (i % 3 == 2) { fqySet.add(x); } } // 调用方法看牌 lookPoker("林青霞", lqxSet, hm); lookPoker("李阳", lySet, hm); lookPoker("风清扬", fqySet, hm); lookPoker("底牌", dpSet, hm); } // 定义看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌) public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm){ System.out.println(name + "的牌是:"); for (Integer key : ts) { String poker = hm.get(key); System.out.print(poker + " "); } System.out.println(); } }
运行结果: