基于集合实现斗地主发牌功能

基于单列集合实现

package com.demo03;

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

public class doudizhu {
    public static void main(String[] args) {
//        System.out.println(createPoker());
        ArrayList poker = createPoker();//先创建扑克牌
        shufflePoker(poker);// 洗牌
//        System.out.println(poker);
        sendPoker(poker);
    }

    /*
    1 创建扑克牌
    * */
    public static ArrayList createPoker() {
        String[] mode = new String[]{"红桃", "方块", "梅花", "黑桃"};
        String[] num = new String[]{"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
        ArrayList<String> poker = new ArrayList<>();
        for (String s : mode) {
            for (String s1 : num) {
                poker.add(s + ":" + s1);
            }
        }
        poker.add("小王");
        poker.add("大王");
        return poker;
    }

    /*
     * 2 洗牌
     * */
    public static void shufflePoker(ArrayList<String> poker) {
        Collections.shuffle(poker);
    }

    /*
     * 3 发牌
     * */
    public static void sendPoker(ArrayList<String> poker) {
        ArrayList<String> play01 = new ArrayList<>();
        ArrayList<String> play02 = new ArrayList<>();
        ArrayList<String> play03 = new ArrayList<>();
        ArrayList<String> dipai = new ArrayList<>();
        for (int i = 0; i < poker.size(); i++) {
            if (i >= 51) {
                dipai.add(poker.get(i));
            } else if (i % 3 == 0) {
                play01.add(poker.get(i));
            } else if (i % 3 == 1) {
                play02.add(poker.get(i));
            } else if (i % 3 == 2) {
                play03.add(poker.get(i));
            }
        }
        System.out.println("玩家1:" + play01);
        System.out.println("玩家2:" + play02);
        System.out.println("玩家3:" + play03);
        System.out.println("底牌:" + dipai);
    }
}

基于双列集合实现

package com.demo03;

import java.util.*;

public class doudizhuplus {
    public static void main(String[] args) {
        //1 准备扑克牌
        HashMap<Integer, String> map = new HashMap<Integer, String>();// 存储扑克牌的容器
        ArrayList<Integer> pokerIndex = new ArrayList<>(); // 存储扑克牌索引的容器
        List<String> colors = List.of("红桃", "方块", "梅花", "黑桃");
        List<String> nums = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        // 2 生成扑克
        int count = 0;
        pokerIndex.add(count);
        map.put(count++, "大王");
        pokerIndex.add(count);
        map.put(count++, "小王");
        for (String num : nums) {
            for (String color : colors) {
                pokerIndex.add(count);
                map.put(count++, color + ":" + num);
            }
        }
        // 3 洗牌
        Collections.shuffle(pokerIndex);
        // 4 发牌
        ArrayList<Integer> play01 = new ArrayList<>();
        ArrayList<Integer> play02 = new ArrayList<>();
        ArrayList<Integer> play03 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();
        for (int index = 0; index < pokerIndex.size(); index++) {
            if (index >= 51) {
                dipai.add(pokerIndex.get(index));
            } else if (index % 3 == 0) {
                play01.add(pokerIndex.get(index));
            } else if (index % 3 == 1) {
                play02.add(pokerIndex.get(index));
            } else if (index % 3 == 2) {
                play03.add(pokerIndex.get(index));
            }
        }
        // 5 看牌
        ArrayList<String> poker_play01 = show(play01, map);
        ArrayList<String> poker_play02 = show(play02, map);
        ArrayList<String> poker_play03 = show(play03, map);
        ArrayList<String> poker_dipai = show(dipai, map);

        System.out.println("玩家1:" + poker_play01);
        System.out.println("玩家2:" + poker_play02);
        System.out.println("玩家3:" + poker_play03);
        System.out.println("底牌:" + poker_dipai);
    }

    public static ArrayList show(ArrayList<Integer> play, Map<Integer, String> map) {
        ArrayList<String> poker_play = new ArrayList<>();
        Collections.sort(play);
        for (Integer index : play) {
            String item = map.get(index);
            poker_play.add(item);
        }
        return poker_play;
    }
}

 

posted @ 2020-05-11 00:04  佛祖让我来巡山  阅读(179)  评论(0编辑  收藏  举报

佛祖让我来巡山博客站 - 创建于 2018-08-15

开发工程师个人站,内容主要是网站开发方面的技术文章,大部分来自学习或工作,部分来源于网络,希望对大家有所帮助。

Bootstrap中文网