java 从入门到精通

java 简易斗地主

2021-05-10  本文已影响0人  Exception_Cui
package DouDiZhu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by kumamon on 2021/5/10.
 */
public class Main {
    public static void main(String[] args) {

        /**
         * 第一步 组装牌
         *
         **** 定义集合 表示54张牌
         **** 定义数组 存储花色(♠ ♥ ♣ ♦)
         **** 定义 数组存储(A,2,3,4,5,6,7,8,9,10,J,Q,K)
         **** 定义 大小王
         **** 拼接牌
         */

        List poker = new ArrayList<String>();
        String[] color = new String[]{"♠", "♥", "♣", "♦"};
        String[] number = new String[]{"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        for (String c1 : color) {
            for (String s1 : number) {
                poker.add(c1 + s1);
            }
        }
        poker.add("大王");
        poker.add("小王");


        /**
         * 第二部
         * 洗牌
         **/
        Collections.shuffle(poker);  //使用shuffle 随机打乱

        /**
         * 第三部
         * 发牌
         *定义玩家牌的集合 和底牌集合
         **** 使用index %3 来进行发牌
         **** 0%3=0 1%3=1 2%3=2  3%3=0 使用这种方式发牌。只发到51张 后面的3张存到底牌
         */

        List lubenwei = new ArrayList();
        List malaoshi = new ArrayList();
        List saozuPdd = new ArrayList();
        List dipai = new ArrayList();

        for (int i = 0; i < poker.size(); i++) {
            if (i >= 51) {
                dipai.add(poker.get(i));
            } else if (i % 3 == 0) {
                lubenwei.add(poker.get(i));
                //Collections.sort(lubenwei);
            } else if (i % 3 == 1) {
                malaoshi.add(poker.get(i));
                //Collections.sort(malaoshi);
            } else if (i % 3 == 2) {
                saozuPdd.add(poker.get(i));
               // Collections.sort(saozuPdd);
            }
        }

        /**
         * 看牌
         * */
        System.out.println("卢本伟:"+lubenwei);
        System.out.println("马老师:"+malaoshi);
        System.out.println("PDD:"+saozuPdd);

        System.out.println("底牌:"+dipai);
    }
}
image.png

手里的牌自动整理

package DouDiZhu;

import java.util.*;

/**
 * Created by kumamon on 2021/5/11.
 */
public class Main1_Sort {

    public static void main(String[] args) {
        
        //组合牌
        Map<Integer, String> poker = new HashMap<>();
        String[] color = new String[]{"♠", "♥", "♣", "♦"};
        String[] number = new String[]{"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
        List<Integer> indexList = new ArrayList<>();
        int index = 0;
        poker.put(index, "大王");
        indexList.add(index);
        index++;

        poker.put(index, "小王");
        indexList.add(index);
        index++;

        for (String s1 : number) {
            for (String s : color) {
                poker.put(index, s + s1);
                indexList.add(index);
                index++;
            }
        }

        ///System.out.println(poker);
        //System.out.println(indexList);

        //洗牌
        Collections.shuffle(indexList);


        
        //发牌
        List<Integer> lubenwei = new ArrayList();
        List<Integer> malaoshi = new ArrayList();
        List<Integer> saozuPdd = new ArrayList();
        List<Integer> dipai = new ArrayList();

        for (int i = 0; i < indexList.size(); i++) {
            if (i >= 51) {
                dipai.add(indexList.get(i));
            } else if (i % 3 == 0) {
                lubenwei.add(indexList.get(i));
            } else if (i % 3 == 1) {
                malaoshi.add(indexList.get(i));
            } else if (i % 3 == 2) {
                saozuPdd.add(indexList.get(i));
            }
        }



        //排序
        Collections.sort(lubenwei);
        Collections.sort(malaoshi);
        Collections.sort(saozuPdd);
        Collections.sort(dipai);

        //看牌
        
        printPoker("马老师",malaoshi,poker);
        printPoker("卢本伟",lubenwei,poker);
        printPoker("PDD",saozuPdd,poker);
        printPoker("底牌",dipai,poker);


    }

    private static void printPoker(String name,List<Integer> list, Map<Integer, String> poker) {
        System.out.print(name+"的牌:{ ");
        for (int i = 0; i < list.size(); i++) {
            int index=list.get(i);
            System.out.print(poker.get(index)+" ");
        }
        System.out.println(" }");
        
    }
};
image.png
上一篇 下一篇

猜你喜欢

热点阅读