小王說來來鬥地主,但是木有牌,怎麼辦-------模擬鬥地主洗牌和發牌 對牌進行排序 並同時使用Map,List,Set等集合
阿新 • • 發佈:2018-12-25
直接上程式碼了。
package cn.ketang.lianxi03; import java.util.ArrayList; import java.util.Collections; /** * ArrayList實現 * 模擬鬥地主洗牌和發牌 * 分析: * A:建立一個牌盒 * B:裝牌 * C:洗牌 * D:發牌 * E:看牌 * @author nice * */ public class LandLordDemo { public static void main(String[] args) { ArrayList<String> ls = new ArrayList<>(); String[] colors = { "♠", "♥", "♦", "♣" }; String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" }; for (String s1 : colors) { for (String s2 : numbers) { ls.add(s1.concat(s2)); } } // 把大小王裝進去 ls.add("大王"); ls.add("小王"); // 洗牌 Collections.shuffle(ls); // 發牌 ArrayList<String> wujulan = new ArrayList<>(); ArrayList<String> wufan = new ArrayList<>(); ArrayList<String> nicewu = new ArrayList<>(); ArrayList<String> dipai = new ArrayList<>(); int length = ls.size(); int number = 3; for (int i = 0; i < length; i++) { if (i >= length - 3) { dipai.add(ls.get(i)); } else if (i % number == 0) { wujulan.add(ls.get(i)); } else if (i % number == 1) { wufan.add(ls.get(i)); } else if (i % number == 2) { nicewu.add(ls.get(i)); } } // 看牌 LookPoker("吳居蘭", wujulan); LookPoker("吳凡", wufan); LookPoker("nice吳", nicewu); LookPoker("底牌", dipai); } static void LookPoker(String name, ArrayList<String> array) { System.out.println(name + "的牌是:"); for (String s : array) { System.out.print(s + " "); } System.out.println(); } }
第二種實現:
package cn.ketang.lianxi04; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.TreeSet; /** * HahMap實現 * 模擬鬥地主洗牌和發牌 * 分析: * A:建立一個牌盒 * B:裝牌 * C:洗牌 * D:發牌 * E:看牌 * @author nice * */ public class LandLordDemo2 { public static void main(String[] args) { // map存放索引和對應的牌 HashMap<Integer, String> hm = new HashMap<Integer, String>(); // 存放牌對應的索引 ArrayList<Integer> al = new ArrayList<Integer>(); String[] colors = { "♠", "♥", "♦", "♣" }; String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" }; // 定義初始索引值為0 int index = 0; for (String color : colors) { for (String number : numbers) { String poker = color.concat(number); hm.put(index, poker); al.add(index); index++; } } // 把大小王放進去 hm.put(index, "大王"); al.add(index); index++; hm.put(index, "小王"); al.add(index); // 洗牌 Collections.shuffle(al); // 發牌 TreeSet<Integer> wujulan = new TreeSet<Integer>(); TreeSet<Integer> wufan = new TreeSet<Integer>(); TreeSet<Integer> nicewu = new TreeSet<Integer>(); TreeSet<Integer> dipai = new TreeSet<Integer>(); //迴圈遍歷al集合,按規則新增al中儲存的HashMap的索引到TreeSet中 for (int i = 0; i < al.size(); i++) { if (i >= (al.size() - 3)) { dipai.add(al.get(i)); } else if (i % 3 == 0) { wufan.add(al.get(i)); } else if (i % 3 == 1) { nicewu.add(al.get(i)); } else if (i % 3 == 2) { wujulan.add(al.get(i)); } } // 看牌 ShowPoker("吳居蘭", wujulan, hm); ShowPoker("吳凡", wufan, hm); ShowPoker("nice吳", nicewu, hm); ShowPoker("底牌", dipai, hm); } public static void ShowPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) { System.out.println(name + "的牌是:"); for (Integer key : ts) {//遍歷TreeSet中的值,將其作為key到HahMap中找到對應的value值 String value = hm.get(key);//將找到的value值付給變數value System.out.print(value + " "); } System.out.println(); } }
以上,謝謝觀看。