美文网首页
JAVA集合摸拟斗地主发牌程序

JAVA集合摸拟斗地主发牌程序

作者: cmeizu | 来源:发表于2021-07-07 16:37 被阅读0次

    分析

    流程:

    1.造一个牌盒

    2.造牌

    3.洗牌

    4.发牌

    5.看牌

    程序分析:

    1.创建一个ArrayList<String>集合对象

    2.新建两个数组(花色,点数),将两个进行拼接

    3.用Collections.shuffle();

    5.发牌

    6.看牌

    代码如下:

    public class PokerTest {
      public static void main(String[] args) {
    //创建一个牌盒
        ArrayList<String> arrayList = new ArrayList<>();
        //装牌
        /**
         * 黑桃A,....黑桃K
         * 红桃A,....
         * 梅花A,....
         * 方块A,....
         */
        //定义一个花色数组
        String[] colors = {"♠", "♥", "♣", "♦"};
        //定义一个点数数组
        String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "K"};
        for (String color : colors) {
          for (String number : numbers) {
            arrayList.add(color.concat(number));
          }
        }
        arrayList.add("大王");
        arrayList.add("小王");
        //洗牌
        Collections.shuffle(arrayList);
        //发牌
        ArrayList<String> cmeizu = new ArrayList<>();
        ArrayList<String> lwy = new ArrayList<>();
        ArrayList<String> huangfeng = new ArrayList<>();
        ArrayList<String> diPai = new ArrayList<>();
        for (int x = 0; x < arrayList.size(); x++) {
          if (x >= arrayList.size() - 3) {
            diPai.add(arrayList.get(x));
          } else if (x % 3 == 0) {
            cmeizu.add(arrayList.get(x));
          } else if (x % 3 == 1) {
            lwy.add(arrayList.get(x));
          } else if (x % 3 == 2) {
            huangfeng.add(arrayList.get(x));
          }
        }
        System.out.println(arrayList);
        lookPoker("农民1:", cmeizu);
        lookPoker("农民2:", lwy);
        lookPoker("地主:", huangfeng);
        lookPoker("底牌:", diPai);
    
      }
    
      public static void lookPoker(String name, ArrayList<String> array) {
        System.out.println(name + "的牌是:");
        for (String s : array) {
          System.out.print(s + " ");
        }
        System.out.println();
      }
    }
    
    

    这种做法有一个问题就是没有排序

    所以第二种做法:

    步骤:

    1,创建一个HashMap<ArrayList<Integer>,String>集合

    2.创建两个数组String类型(花色,点数),然后拼接numbers.concat(colors)

    3.洗牌

    4.发牌

    5.看牌

    具体实现如下:

    import java.util.ArrayList;
    
    import java.util.Collections;
    
    
    public class PokerDemo {
      public static void main(String[] args) {
    
    //创建一个牌盒
    
        ArrayList<String> arrayList = new ArrayList<>();
    
        //装牌
    
        /**
         * 黑桃A,....黑桃K
         * 红桃A,....
         * 梅花A,....
         * 方块A,....
         */
        //定义一个花色数组
        String[] colors = {"♠", "♥", "♣", "♦"};
        //定义一个点数数组
        String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "K"};
        for (String color : colors) {
          for (String number : numbers) {
            arrayList.add(color.concat(number));
          }
        }
        arrayList.add("大王");
        arrayList.add("小王");
        //洗牌
        Collections.shuffle(arrayList);
        //发牌
        ArrayList<String> cmeizu = new ArrayList<>();
    
        ArrayList<String> lwy = new ArrayList<>();
    
        ArrayList<String> huangfeng = new ArrayList<>();
    
        ArrayList<String> diPai = new ArrayList<>();
    
        for (int x = 0; x < arrayList.size(); x++) {
          if (x >= arrayList.size() - 3) {
            diPai.add(arrayList.get(x));
          } else if (x % 3 == 0) {
            cmeizu.add(arrayList.get(x));
          } else if (x % 3 == 1) {
            lwy.add(arrayList.get(x));
          } else if (x % 3 == 2) {
            huangfeng.add(arrayList.get(x));
          }
    
        }
        System.out.println(arrayList);
        lookPoker("农民1:", cmeizu);
        lookPoker("农民2:", lwy);
        lookPoker("地主:", huangfeng);
        lookPoker("底牌:", diPai);
      }
    
      public static void lookPoker(String name, ArrayList<String> array) {
        System.out.println(name + "的牌是:");
        for (String s : array) {
          System.out.print(s + " ");
        }
        System.out.println();
      }
    }
    

    相关文章

      网友评论

          本文标题:JAVA集合摸拟斗地主发牌程序

          本文链接:https://www.haomeiwen.com/subject/fkzoultx.html