Java代码模拟斗地主实现

package com.test;

import org.junit.Test;

import java.util.*;

/**
 * @program: study
 * @description: 模拟斗地主
 * @author: Elias.Guo
 * @create: 2020-06-11 16:41
 **/
public class MoNiDouDiZhu {
    @Test
    public void test(){
        //创建HashMap key是编号,值是牌
        HashMap<Integer,String> map =new HashMap<>();
        //创建List,存储编号
        List<Integer> list =new ArrayList<>();

        //创建花色数组
        String[] colors ={"♦", "♣", "♠", "♥"};
        //创建牌数组
        String[] numbers={"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        //遍历,存储牌和编号
        int index =0;
        for (String color : colors) {
            for (String number : numbers) {
                String poker =color+number;
                map.put(index,poker);
                list.add(index);
                index++;
            }
        }
        //添加大小王
        map.put(index,"大王");
        list.add(index);
        index++;
        map.put(index,"小王");
        list.add(index);

        //洗牌
        Collections.shuffle(list);

        //定义玩家
        TreeSet<Integer> faGe=new TreeSet<>();
        TreeSet<Integer> reBa=new TreeSet<>();
        TreeSet<Integer> me=new TreeSet<>();
        TreeSet<Integer> dePai=new TreeSet<>();

        //发牌
        for(int i=0;i<list.size();i++){
            if(i>=list.size()-3){
                //底牌
                dePai.add(list.get(i));
            }else if(i%3==0){
                //发哥
                faGe.add(list.get(i));
            }else if(i%3==1){
                //热巴
                reBa.add(list.get(i));
            }else if(i%3==2){
                //华仔
                me.add(list.get(i));
            }
        }

        //看牌方法调用
        showPoker("发哥", faGe, map);
        showPoker("迪丽热巴", reBa, map);
        showPoker("迪丽热巴的老公(我)", me, map);
        showPoker("底牌",dePai,map);

    }
    public static void showPoker(String name, TreeSet<Integer> tr, HashMap<Integer, String> map) {
        System.out.print(name + "的牌是:");
        for (Integer key : tr) {
            String value = map.get(key);
            System.out.print(value + " ");
        }
        System.out.println();
        System.out.println("-------------------");
    }


}

输出结果:

发哥的牌是:♦5 ♦J ♦A ♣7 ♣10 ♣J ♣K ♣2 ♠6 ♠10 ♠A ♠2 ♥6 ♥7 ♥8 ♥10 ♥K 
-------------------
迪丽热巴的牌是:♦4 ♦9 ♣3 ♣6 ♣8 ♣Q ♣A ♠4 ♠5 ♠7 ♠J ♠K ♥3 ♥9 ♥Q ♥A 小王 
-------------------
迪丽热巴的老公(我)的牌是:♦3 ♦6 ♦7 ♦8 ♦K ♦2 ♣4 ♣5 ♣9 ♠3 ♠8 ♠Q ♥4 ♥5 ♥J ♥2 大王 
-------------------
底牌的牌是:♦10 ♦Q ♠9 
-------------------

Process finished with exit code 0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值