Java扑克牌(多线程)

2022-11-29 09:32:23 浏览数 (1)

 这里将subList更换成遍历赋值的方法,因为:【https://blog.csdn.net/feng8403000/article/details/114797903】

多线程的目的是加上倒计时,可以自己加。

代码语言:javascript复制
package com.item.test;

import java.util.*;

public class Action {
    public static void main(String[] args) {
        //随机卡牌
        List<Card> list=ResetCard(StartCard());
        //由于subList在修改的时候会出现异常,所以更换新地址
//        List<Card> one=list.subList(0,17);
//        List<Card> two=list.subList(17,2*17);
//        List<Card> three=list.subList(2*17,3*17);
//        List<Card> four=list.subList(3*17,54);
        List<Card> one=new ArrayList<Card>();
        List<Card> two=new ArrayList<Card>();
        List<Card> three=new ArrayList<Card>();
        List<Card> four=new ArrayList<Card>();
        for (int i = 0; i < list.size(); i  ) {
            if(i<17){
                one.add(list.get(i));
            }else if(i<2*17){
                two.add(list.get(i));
            }else if(i<3*17){
                three.add(list.get(i));
            }else{
                four.add(list.get(i));
            }
        }

        ShowCard(one,1);
        ShowCard(two,1);
        ShowCard(three,1);
        ShowCard(four,0);
        /**
         * 抢地主线程
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                Scanner sc = new Scanner(System.in);
                //存储选择分数的数组
                int[] array=new int[3];
                for (int i = 0; i < 3; i  ) {
                    System.out.println("请" (i 1) "号农民选择分数或跳过:");
                    System.out.println("1:1分、2:2分、3:3分、4:跳过");
                    char choose = sc.next().charAt(0);
                    switch (choose){
                        case '1':array[i]=1;break;
                        case '2':array[i]=2;break;
                        case '3':array[i]=3;break;
                        default:array[i]=0;break;
                    }
                }
                if(array[0]==0&&array[1]==0&&array[2]==0){
                    System.out.println("流局,请开启下一轮");
                }
                int index=-1;
                int max=-1;
                //根据分数进行获取地主或者流局
                for (int i = 0; i < array.length; i  ) {
                    if(max<array[i]){
                        max=array[i];
                        index=i;
                    }
                }
                switch (index){
                    case 0:one.addAll(four);break;
                    case 1:two.addAll(four);break;
                    case 2:three.addAll(four);break;
                }
                ShowCard(one,one.size());
                ShowCard(two,two.size());
                ShowCard(three,three.size());
            }
        }).run();
    }

    /**
     * 0、代表底牌n17、代表农民n20、代表地主
     * @param list
     * @param idName
     */
    public static void ShowCard(List<Card> list,int idName){
        System.out.println(idName==17?"农民":(idName==20?"地主":"底牌"));
        for (Card c:DescCard(list)) {
            System.out.print(c.getName() "t");
        }
        System.out.println();
    }
    /**
     * 基本排序·Card的id由大到小
     * @param list
     * @return
     */
    public static List<Card> DescCard(List<Card> list){
        Comparator<Card> cc=new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getIndex()-o2.getIndex();
            }
        };
        Collections.sort(list,cc);//正序
        Collections.reverse(list);//倒序
        return list;
    }

    /**
     * 随机排序
     * @param list
     * @return
     */
    public static List<Card> ResetCard(List<Card> list){
        Collections.shuffle(list);
        return list;
    }

    /**
     * 初始化卡牌
     * @return
     */
    public static List<Card> StartCard(){
        List<Card> list=new ArrayList<>(54);
        int i=0;
        for (CardsEnum cardsEnum:CardsEnum.values()) {
            for (CardColorEnum cardColorEnum:CardColorEnum.values()) {
                list.add(new Card(cardsEnum,cardColorEnum,i  ));
            }
        }
        Card big=new Card();
        big.setName(CardBossEnum.BIG.getName());
        big.setIndex(i  );
        Card small=new Card();
        small.setName(CardBossEnum.SMALL.getName());
        small.setIndex(i  );
        list.add(small);
        list.add(big);
        return list;
    }
}

效果如下:

0 人点赞