集合模拟斗地主的洗牌发牌抢地主

集合模拟斗地主的洗牌发牌抢地主

import java.util.*;

public class LandlordsGame {
    public static void main(String[] args) {
        getBox();
    }
    
    private static void getBox() {//组合牌,并给定索引
        ArrayList<String> num = new ArrayList<>();//数字
        Collections.addAll(num, "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2");//数字
        ArrayList<String> color = new ArrayList<>();
        Collections.addAll(color, "♣", "♥", "♠", "♦");//花色
        HashMap<Integer, String> box = new HashMap<>();//总牌
        int i = 0;
        for (String n : num) {
            for (String c : color) {
                String concat = c.concat(n);
                box.put(i, concat);
                i++;
            }
        }
        box.put(52, "☪");//手动添加小王
        box.put(53, "☀");//手动添加大王
        ArrayList<Integer> boxNum = new ArrayList<>();//索引集合
        Set<Integer> set = box.keySet();
        for (Integer integer : set) {
            boxNum.add(integer);
        }
        Collections.shuffle(boxNum);//打乱索引
        TreeSet<Integer> one = new TreeSet<>();//第一个人
        TreeSet<Integer> two = new TreeSet<>();//第二个人
        TreeSet<Integer> three = new TreeSet<>();//第三个人
        TreeSet<Integer> four = new TreeSet<>();//底牌
        for (int j = 0; j < boxNum.size(); j++) {
            if (j >= boxNum.size() - 3) {
                four.add(boxNum.get(j));
            } else if (j % 3 == 0) {
                one.add(boxNum.get(j));
            } else if ((j - 1) % 3 == 0) {
                two.add(boxNum.get(j));
            } else {
                three.add(boxNum.get(j));
            }
        }
        seeBox("Lee", one, box);
        seeBox("ZZ", two, box);
        seeBox("Jk", three, box);
        seeBox("底牌", four, box);
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入抢牌者姓名");
        String s = scanner.nextLine();
        if (s.equalsIgnoreCase("lee")) {
            getPoke(one, four);
            seeBox("Lee", one, box);
            seeBox("ZZ", two, box);
            seeBox("Jk", three, box);
        } else if (s.equalsIgnoreCase("zz")) {
            getPoke(two, four);
            seeBox("Lee", one, box);
            seeBox("ZZ", two, box);
            seeBox("Jk", three, box);
        } else if (s.equalsIgnoreCase("jk")) {
            getPoke(three, four);
            seeBox("Lee", one, box);
            seeBox("ZZ", two, box);
            seeBox("Jk", three, box);
        } else {
            System.out.println("输入错误,游戏结束");
            return;
        }
        //System.out.println(box);
    }

    //看牌
    public static void seeBox(String name, TreeSet<Integer> set, HashMap<Integer, String> box) {
        System.out.print(name + "\t");
        for (Integer data : set) {
            System.out.print(box.get(data) + "\t");
        }
        System.out.println();
    }

    //抢牌
    public static void getPoke(TreeSet<Integer> t1, TreeSet<Integer> t2) {
        t1.addAll(t2);
    }

}

Lee	♣44678899910	♣J	♥Q	♦Q	♣K	♦K	♥A	♠A	
ZZ	♣333466778910	♥J	♠J	♣A	♥2	☪	☀	
Jk	♥455556810	♦J	♣Q	♠Q	♥K	♠K	♦A	♣222	
底牌	♦3710	
请输入抢牌者姓名
zz
Lee	♣44678899910	♣J	♥Q	♦Q	♣K	♦K	♥A	♠A	
ZZ	♣3333466777891010	♥J	♠J	♣A	♥2	☪	☀	
Jk	♥455556810	♦J	♣Q	♠Q	♥K	♠K	♦A	♣222	

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值