本文主要是介绍java斗牛,咋金花,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
无聊时间,打发下游戏
简单说下思路
目录
1.创建牌对象
2.创建52张牌,不包含大小王
3.洗牌
4.发牌
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();});}
}
这篇关于java斗牛,咋金花的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!