斗地主案例

package com.zhongruan;

import java.util.*;

public class DouDiZzhu2 {
    public static void main(String[] args){
//        使用map创建一副扑克
        String [] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        String [] colors={"♥","♠","♣","♦"};
        Map<Integer,String> poker=new HashMap<>();
        int index=1;
        poker.put(index++,"大王");
        poker.put(index++,"小王");
        for (String number:numbers){
            for (String color:colors){
            String pai=color+number;
            poker.put(index++,pai);
        }
        }
//        System.out.println(poker);
//        将牌的索引创建为一个新的集合
        List<Integer> pokerIndexs=new ArrayList<>();
        Set<Integer>integers=poker.keySet();
        System.out.println(integers);
        for (Integer i:poker.keySet()){
            pokerIndexs.add(i);
        }
//        将牌的索引打乱
        Collections.shuffle(pokerIndexs);
        System.out.println(pokerIndexs);
//        创建四个Set接受三个玩家和底牌的索引值
        Set<Integer> player1=new TreeSet<>();
        Set<Integer> player2=new TreeSet<>();
        Set<Integer> player3=new TreeSet<>();
        Set<Integer> dipai=new TreeSet<>();
        dipai.add(pokerIndexs.remove(0));
        dipai.add(pokerIndexs.remove(0));
        dipai.add(pokerIndexs.remove(0));
        for (int i=0;i<pokerIndexs.size();i++){
            int pi=pokerIndexs.get(i);
            int mod=i%3;
            if(mod==0){
                player1.add(pi);
            }else if (mod==1){
                player2.add(pi);
            }else {
                player3.add(pi);
            }
        }
//        调用look方法将索引值转换为扑克牌
        look(poker,player1);
        look(poker,player2);
        look(poker,player3);
        look(poker,dipai);
    }
//    创建look方法,通过索引从扑克牌中取牌
    public static void look(Map<Integer,String> poker,Set<Integer> indexs){
        List<String> p=new ArrayList<>();
        for(Integer i:indexs){
            String pai=poker.get(i);
            p.add(pai);
        }
        System.out.println(p);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值