Java多线程实现红五发牌程序

Java多线程实现红五发牌程序

题目要求

在这里插入图片描述
可能我写太麻烦了,还是我题目意思理解错了。
做法是随机发牌,之后对于每个人的牌先按花色分成四类,然后每种花色再根据优先级排序。

import jdk.internal.org.objectweb.asm.tree.InnerClassNode;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

public class Dealer {
    public static class poker {
        Integer color;  //花色 1~4 分别代表 红桃、方块、黑桃、黑梅花
        Integer point;  //点数,A~K,大王为100, 小王为99

        poker() {

        }

        poker(int x, int y) {
            this.color = x;
            this.point = y;
        }
    }

    public static poker[] pokers = new poker[109];

    public static int cnt = 0;

    public static HashMap<Integer, poker> mp = new HashMap<>();
    public static Map<Integer,String> color = new HashMap<>();
    public static Map<Integer,String> point = new HashMap<>();

    public static int[] vis = new int[109];

    public static void init() {   //初始化一副牌
        Arrays.fill(vis, 0);
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= 13; j++) {
                cnt++;
                pokers[cnt] = new poker(i, j);
                mp.put(cnt, pokers[cnt]);
            }
        }
        pokers[++cnt] = new poker(1, 99);
        mp.put(cnt, pokers[cnt]);
        pokers[++cnt] = new poker(1, 100);
        mp.put(cnt, pokers[cnt]);
    }

    public static Map<Integer, Integer> priorityRed = new HashMap<>();  //红桃优先级
    public static Map<Integer, Integer> prioritySpade = new HashMap<>();  //黑桃、梅花
    public static Map<Integer, Integer> priorityDiamonds = new HashMap<>();  //方块

    public static void init1() {
        init();init();
        color.put(1,"红桃♥");color.put(2,"黑桃♠");  //映射花色
        color.put(3,"梅花♣");color.put(4,"方块♦");point.put(1,"A");
        for(int i=2;i<=10;i++){
            point.put(i,String.valueOf(i));    //映射点数
        }
        point.put(11,"J");point.put(12,"Q");point.put(13,"K");
        point.put(100,"大王");point.put(99,"小王");
        priorityRed.put(100, 2);
        priorityRed.put(99, 3);
        for (int i = 1; i <= 13; i++) {
            if (i == 5) priorityRed.put(i, 1);
            else if (i == 3) priorityRed.put(i, 4);
            else if (i == 2) priorityRed.put(i, 5);
            else if (i == 1) priorityRed.put(i, 6);
            else if (i == 13) priorityRed.put(i, 7);
            else priorityRed.put(i, 8);
        }
        for (int i = 1; i <= 13; i++) {
            if (i == 3) prioritySpade.put(i, 1);
            else if (i == 2) prioritySpade.put(i, 2);
            else if (i == 1) prioritySpade.put(i, 3);
            else if (i == 13) prioritySpade.put(i, 4);
            else prioritySpade.put(i, 5);
        }
        for (int i = 1; i <= 13; i++) {
            if (i == 2) priorityDiamonds.put(i, 1);
            else if (i == 1) priorityDiamonds.put(i, 2);
            else if (i == 13) priorityDiamonds.put(i, 3);
            else priorityDiamonds.put(i, 4);
        }
    }

    public static List<poker> l1 = new ArrayList<>();   //第一个人的牌
    public static List<poker> l2 = new ArrayList<>();   //第二个人的牌
    public static List<poker> l3 = new ArrayList<>();   //第三个人的牌
    public static List<poker> l4 = new ArrayList<>();   //第四个人的牌

    public static void deal() {    //随机发牌
        init1();
        Set<Integer> set = new HashSet<>();
        Random random = new Random();
        while (set.size() < 25) {
            int x = random.nextInt(108) + 1;
            if (vis[x] == 0) {
                l1.add(mp.get(x));
                vis[x] = 1;
                set.add(x);
            }
        }
        while (set.size() < 50) {
            int x = random.nextInt(108) + 1;
            if (vis[x] == 0) {
                l2.add(mp.get(x));
                vis[x] = 1;
                set.add(x);
            }
        }
        while (set.size() < 75) {
            int x = random.nextInt(108) + 1;
            if (vis[x] == 0) {
                l3.add(mp.get(x));
                vis[x] = 1;
                set.add(x);
            }
        }
        while (set.size() < 100) {
            int x = random.nextInt(108) + 1;
            if (vis[x] == 0) {
                l4.add(mp.get(x));
                vis[x] = 1;
                set.add(x);
            }
        }
//        for(int i = 1;i<=108;i++) System.out.println(vis[i]);
    }

    private static AtomicBoolean isLock = new AtomicBoolean(false);   //锁初始化变量

    public static boolean lock(){    //加锁
        return isLock.compareAndSet(false, true);
    }

    public static void unlock() {   //解锁
        isLock.set(false);
    }

    public static void solve(List<poker> l1){
        l1.sort((Comparator.comparing(o -> o.color)));   //先根据花色排序
        List<ArrayList<poker>> list = new ArrayList<>(5);
        for(int i=0;i<5;i++) list.add(new ArrayList<>());
        for (int i = 0; i < l1.size(); i++) {     //将牌分成四组,每组各自排序
            (list.get(l1.get(i).color)).add(l1.get(i));
        }
        list.get(1).sort((o1, o2) -> {   //lambda表达式排序
            if (priorityRed.get(o1.point) != priorityRed.get(o2.point)) {
                return priorityRed.get(o1.point) - priorityRed.get(o2.point);
            }
            return o2.point - o1.point;
        });
        list.get(2).sort((o1, o2) -> {
            if (prioritySpade.get(o1.point) != prioritySpade.get(o2.point)) {
                return prioritySpade.get(o1.point) - prioritySpade.get(o2.point);
            }
            return o2.point - o1.point;
        });
        list.get(3).sort((o1, o2) -> {
            if (prioritySpade.get(o1.point) != prioritySpade.get(o2.point)) {
                return prioritySpade.get(o1.point) - prioritySpade.get(o2.point);
            }
            return o2.point - o1.point;
        });
        list.get(4).sort((o1, o2) -> {
            if (priorityDiamonds.get(o1.point) != priorityDiamonds.get(o2.point)) {
                return priorityDiamonds.get(o1.point) - priorityDiamonds.get(o2.point);
            }
            return o2.point - o1.point;
        });
        while (!lock()) {
            //加锁失败,自旋
        }
        System.out.println("当前线程" + Thread.currentThread().getName() + " " + l1.size());
        for(int i=1;i<=4;i++){
            for(int j=0;j<list.get(i).size();j++){
                System.out.println(color.get(i)+" "+point.get(list.get(i).get(j).point));
            }
        }
        unlock();
    }

    public static void main(String[] args) {
        deal();
        ExecutorService service = Executors.newFixedThreadPool(4);   //线程池
        service.submit(() -> {
            solve(l1);
        });
        service.submit(() -> {
            solve(l2);
        });
        service.submit(() -> {
            solve(l3);
        });
        service.submit(() -> {
            solve(l4);
        });
    }

}

运行结果:
在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值