用Java语音写使用多线程发扑克牌

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

2、最后输出每个玩家所得的牌,牌的花色要显示出来,比如:黑桃A、红桃10、梅花J、方块K。(最好是能输出花色的图案,♠️♣️♥️)

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

/**
 * @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
        );

    }
}

可以参考这篇文章,有详细的过程,希望对你有帮助:Java项目:模拟扑克牌洗牌发牌排序_SANSE_ZYL的博客-CSDN博客

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m