无聊时间,打发下游戏
简单说下思路
1.创建牌对象
2.创建52张牌,不包含大小王
3.洗牌
4.发牌
/**
* 扑克牌
*/
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;
}
}
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();
});
}
}
原文地址:https://blog.csdn.net/weixin_59383491/article/details/134548103
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_3212.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。