利用集合写斗地主发牌代码

斗地主发牌代码

package cn.j.day14.demo02.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;


public class DouDiZhu_zonghe {
    public static void main(String[] args) {
        HashMap<Integer, String> poker = new HashMap<>();
        List<Integer> index = new ArrayList<>();
        //准备牌
        List<String> colors = List.of("♣", "♦", "♠", "♥");
        List<String> numbers = List.of("1", "2", "k", "q", "j", "10", "9", "8", "7", "6", "5", "4", "3");
        int indexs = 0;
        poker.put(indexs,"大王");
        index.add(indexs);
        indexs++;
        poker.put(indexs,"小王");
        index.add(indexs);
        indexs++;
        for (String co : colors) {
            for (String nu : numbers) {
                String s = co + nu;
                poker.put(indexs,s);
                index.add(indexs);
                indexs++;
            }
        }
        //洗牌
        Collections.shuffle(index);
       // System.out.println(index);
        //发牌
        ArrayList<Integer> play1 = new ArrayList<>();
        ArrayList<Integer> play2 = new ArrayList<>();
        ArrayList<Integer> play3 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();
        for (int i = 0; i <=index.size()-1 ; i++) {
            Integer in = index.get(i);
            if (i>=51){
                dipai.add(in);
            }else if (i%3==0){
                play1.add(in);
            }else if (i%3==1){
                play2.add(in);
            }else if (i%3==2){
                play3.add(in);
            }
        }
        //排序
        Collections.sort(play1);
        Collections.sort(play2);
        Collections.sort(play3);
        Collections.sort(dipai);
        loolpoker("唐伯虎",poker,play1);
        loolpoker("祝枝山",poker,play2);
        loolpoker("周文斌",poker,play3);
        loolpoker("文征明",poker,dipai);
    }
    //看牌方法
    public static void loolpoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> index){
        System.out.print(name+": ");
        for (Integer i:index){
            String value = poker.get(i);
            System.out.print(value);
        }
        System.out.println();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值