终于把老师讲的斗地主程序写完了- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.Set;
- import java.util.TreeMap;
- /*
- * 模拟斗地主
- */
- public class Demo4 {
- public static void main(String[] args) {
- // 创建牌盒
- HashMap<Integer, String> hm = new HashMap<>();
- // 创建一个序号集合
- ArrayList<Integer> al = new ArrayList<>();
- // 创建花色和数值数组
- String[] arr1 = { "♦", "♣", "♥", "♠" };
- String[] arr2 = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
- "K", "A", "2" };
- // 将牌装入牌盒
- int num = 0;
- for (int i = 0; i < arr2.length; i++) {
- for (int j = 0; j < arr1.length; j++) {
- num++;
- al.add(num);
- StringBuilder sb = new StringBuilder();
- String s = sb.append(arr1[j]).append(arr2[i]).toString();
- hm.put(num, s);
- }
- }
- // 装入大小王
- num++;
- al.add(num);
- hm.put(num, "小王");
- num++;
- al.add(num);
- hm.put(num, "大王");
- // 洗牌
- Collections.shuffle(al);
- // 发牌,创建三个人的手牌
- TreeMap<Integer, String> tm1 = new TreeMap<>();
- TreeMap<Integer, String> tm2 = new TreeMap<>();
- TreeMap<Integer, String> tm3 = new TreeMap<>();
- TreeMap<Integer, String> tm4 = new TreeMap<>();
- // 发牌
- for (int i = 0; i <al.size(); i++) {
- if (i >= 51) {
- tm4.put(al.get(i), hm.get(al.get(i)));
- } else if (i % 3 == 0) {
- tm1.put(al.get(i), hm.get(al.get(i)));
- } else if (i % 3 == 1) {
- tm2.put(al.get(i), hm.get(al.get(i)));
- } else if (i % 3 == 2) {
- tm3.put(al.get(i), hm.get(al.get(i)));
- }
- }
- // 看牌
- look("我", tm1);
- look("你", tm2);
- look("老师", tm3);
- look("底牌", tm4);
- }
- private static void look(String string, TreeMap<Integer, String> tm1) {
- Set<Integer> set = tm1.keySet();
- System.out.print(string + "的牌:");
- for (Integer i : set) {
- String value = tm1.get(i);
- System.out.print(value + ",");
- }
- System.out.println();
- }
- }
复制代码
|
|