EthanHa 2021-06-16 19:59 采纳率: 100%
浏览 206
已采纳

用Java写多线程随机发扑克牌

1、Java:使用多线程,模拟将52张扑克牌随机分发给4个人,每人13张。

2、最后输出每个玩家所得的牌,牌的花色要显示出来,比如:黑桃A、红桃10、梅花J、方块K。

3、用最基础的Java语言写,尽量多注释。(我基础较差,比较难看懂)

  • 写回答

2条回答 默认 最新

  • ㅤᅠᅠㅤ 2021-06-17 15:39
    关注
    package com.nxftl.study.question.csdn.poke;
    
    import lombok.extern.slf4j.Slf4j;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Stack;
    import java.util.concurrent.atomic.AtomicInteger;
    
    /**
     * @author darkltl
     * @className Poke
     * @email darkltl@163.com
     * @description
     * 多线程发扑克牌
     */
    @Slf4j
    public class Poke {
    
        private static final Stack<String> POKES = new Stack<>();
    
        private static final String[] POKE_ARRAY = {
                " ♠A "," ♠2 "," ♠3 "," ♠4 "," ♠5 "," ♠6 "," ♠7 "," ♠8 "," ♠9 "," ♠10 "," ♠J "," ♠Q "," ♠K ",
                " ♣A "," ♣2 "," ♣3 "," ♣4 "," ♣5 "," ♣6 "," ♣7 "," ♣8 "," ♣9 "," ♣10 "," ♣J "," ♣Q "," ♣K ",
                " ♥A "," ♥2 "," ♥3 "," ♥4 "," ♥5 "," ♥6 "," ♥7 "," ♥8 "," ♥9 "," ♥10 "," ♥J "," ♥Q "," ♥K ",
                " ♦A "," ♦2 "," ♦3 "," ♦4 "," ♦5 "," ♦6 "," ♦7 "," ♦8 "," ♦9 "," ♦10 "," ♦J "," ♦Q "," ♦K "
        };
    
        static {
            POKES.addAll(Arrays.asList(POKE_ARRAY));
        }
    
    
    
        /**
         * 洗牌
         */
        private static void shuffle(){
            //简单洗牌,利用随机数交换两数位置,表示洗牌
            for (int i = 0; i < POKES.size(); i++) {
                int index = (int) ((Math.random() * 52));
                String currentElement = POKES.get(i);
                POKES.set(i,POKES.get(index));
                POKES.set(index,currentElement);
            }
        }
    
    
    
    
    
        public static void main(String[] args) {
            Gamer gamerOne = new Gamer("张三");
            Gamer gamerTwo = new Gamer("李四");
            Gamer gamerThree = new Gamer("王五");
            Gamer gamerFour = new Gamer("趙六");
            List<String> gamerOnePokes = gamerOne.getPokes();
            List<String> gamerTwoPokes = gamerTwo.getPokes();
            List<String> gamerThreePokes = gamerThree.getPokes();
            List<String> gamerFourPokes = gamerFour.getPokes();
            shuffle();
    
            Thread executor = new Thread(
                    () -> {
                        //利用AtomicInteger自增 等同于i++ (只不过是线程安全的)
                        AtomicInteger index = new AtomicInteger(0);
                        //循环条件是当扑克总量-4大于等于自增下标时进入循环体(-4代表一轮)
                        while(POKES.size()-4 >= index.get()){
                            gamerOnePokes.add(POKES.get(index.getAndIncrement()));
                            gamerTwoPokes.add(POKES.get(index.getAndIncrement()));
                            gamerThreePokes.add(POKES.get(index.getAndIncrement()));
                            gamerFourPokes.add(POKES.get(index.getAndIncrement()));
                        }
                    }
            );
            executor.start();
    
            //等待线程结束
            while(executor.isAlive()){
            }
            gamerFourPokes.forEach(
                    System.out::print
            );
            System.out.println();
            gamerThreePokes.forEach(
                    System.out::print
            );
            System.out.println();
            gamerTwoPokes.forEach(
                    System.out::print
            );
    
            System.out.println();
            gamerOnePokes.forEach(
                    System.out::print
            );
    
        }
    }
    
    class Gamer {
    
        private List<String> pokes = new ArrayList<>();
    
        private AtomicInteger atomicInteger = new AtomicInteger(0);
    
        private String gamerName ;
    
        public String getGamerName() {
            return gamerName;
        }
    
        public void setGamerName(String gamerName) {
            this.gamerName = gamerName;
        }
    
        public List<String> getPokes() {
            return pokes;
        }
    
        public void setPokes(List<String> pokes) {
            this.pokes = pokes;
        }
    
        public Gamer(String gamerName){
            this.gamerName = gamerName;
        }
    
        public int increment(){
            return atomicInteger.incrementAndGet();
        }
    
    
        public void inputHasPoke(){
            pokes.forEach(
                    System.out::print
            );
            System.out.println();
        }
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用