java斗牛,咋金花

无聊时间,打发下游戏

简单说下思路

目录

1.创建牌对象

2.创建52张牌,不包含大小王

3.洗牌

4.发牌


1.创建牌对象

2.创建52张牌,不包含大小王

3.洗牌

4.发牌

java 复制代码
/**
 * 扑克牌
 */
public class Poker {
    /**
     * 花色
     */
    private String cardSuits;

    /**
     * 数字
     */
    private int cardNum;

    /**
     * 人名
     */
    private String peopleName;

    public Poker() {

    }

    public Poker(String cardSuits, int cardNum) {
        this.cardSuits = cardSuits;
        this.cardNum = cardNum;
    }

    public Poker(String cardSuits, int cardNum, String peopleName) {
        this.cardSuits = cardSuits;
        this.cardNum = cardNum;
        this.peopleName = peopleName;
    }

    public String getCardSuits() {
        return cardSuits;
    }

    public void setCardSuits(String cardSuits) {
        this.cardSuits = cardSuits;
    }

    public int getCardNum() {
        return cardNum;
    }

    public void setCardNum(int cardNum) {
        this.cardNum = cardNum;
    }

    public String getPeopleName() {
        return peopleName;
    }

    public void setPeopleName(String peopleName) {
        this.peopleName = peopleName;
    }
}
java 复制代码
import java.util.*;

public class CardUtils {

    public static String[] strSuits = {"♠", "♥", "♣", "♦"};

    public static void main(String[] args) {
        //组装52张扑克
        List<Poker> pokerList = getPokerList();

        //洗牌
        Set<Poker> pokerSet = getPokerSet(pokerList);

        //发牌
        licensing(5,pokerSet);

    }

    /**
     * 获取一副新牌
     *
     * @return
     */
    public static List<Poker> getPokerList() {
        //组装52张扑克
        List<Poker> pokerList = new ArrayList<>();
        for (int i = 0; i < strSuits.length; i++) {
            for (int j = 1; j <= 13; j++) {
                Poker poker = new Poker(strSuits[i], j);
                pokerList.add(poker);
            }
        }

        System.out.println("======新牌=====未打乱顺序 begin======");
        pokerList.forEach(l -> System.out.print(l.getCardSuits() + l.getCardNum() + "===="));
        System.out.println();
        System.out.println("======新牌=====未打乱顺序  end======");
        System.out.println();

        return pokerList;
    }

    /**
     * 洗牌
     *
     * @param pokerList
     * @return
     */
    public static Set<Poker> getPokerSet(List<Poker> pokerList) {
        //打乱顺序
        Set<Poker> pokerSet = new HashSet<>();
        Random random = new Random();
        while (pokerSet.size() < pokerList.size()) {
            pokerSet.add(pokerList.get(random.nextInt(pokerList.size())));
        }

        //重新洗牌之后的顺序
        System.out.println("======洗牌======打乱顺序 begin======");
        pokerSet.forEach(l -> System.out.print(l.getCardSuits() + l.getCardNum() + "===="));
        System.out.println();
        System.out.println("======洗牌======打乱顺序  end======");
        System.out.println();

        return pokerSet;
    }


    /**
     * 发牌(不得超过10人)
     *
     * @param num      几人再玩
     * @param pokerSet 打乱顺序的牌
     */
    public static void licensing(int num, Set<Poker> pokerSet) {
        if (num < 2 || num > 10) {
            System.out.println("参与人数不得小于2人或者大于10人");
            return;
        }
        //针对每个人的集合====几个人玩就循环几次
        List<List<Poker>> peopleList = new ArrayList();
        for (int i = 0; i < num; i++) {
            List<Poker> list = new ArrayList<>();
            peopleList.add(list);
        }
        //已发过的牌
        List<Poker> tempList = new ArrayList<>();

        //发牌    3人  每人5张
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < peopleList.size(); j++) {
                List<Poker> pokers = peopleList.get(j);
                Poker poker = pokerSet.iterator().next();
                poker.setPeopleName("持牌者" + j);
                pokers.add(poker);
                tempList.add(poker);
                pokerSet.remove(poker);
                continue;
            }
        }

        System.out.println("======已发过的牌 begin======");
        tempList.forEach(l -> System.out.print(l.getCardSuits() + l.getCardNum() + "===="));
        System.out.println();
        System.out.println("======已发过的牌 end======");
        System.out.println();
        System.out.println("======剩余的牌 begin======");
        pokerSet.forEach(l -> System.out.print(l.getCardSuits() + l.getCardNum() + "===="));
        System.out.println();
        System.out.println("======剩余的牌 end======");
        System.out.println();
        peopleList.forEach(pl -> {
            System.out.println("======分割线======" + pl.get(0).getPeopleName());
            pl.forEach(poker -> System.out.print(poker.getCardSuits() + poker.getCardNum() + "===="));
            System.out.println();
        });
    }
}
相关推荐
可峰科技1 分钟前
斗破QT编程入门系列之二:认识Qt:编写一个HelloWorld程序(四星斗师)
开发语言·qt
oliveira-time2 分钟前
golang学习2
算法
customer084 分钟前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
全栈开发圈6 分钟前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫
WaaTong8 分钟前
《重学Java设计模式》之 单例模式
java·单例模式·设计模式
面试鸭10 分钟前
离谱!买个人信息买到网安公司头上???
java·开发语言·职场和发展
小白学大数据11 分钟前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
Python大数据分析@14 分钟前
python操作CSV和excel,如何来做?
开发语言·python·excel
上海_彭彭39 分钟前
【提效工具开发】Python功能模块执行和 SQL 执行 需求整理
开发语言·python·sql·测试工具·element