java模擬斗地主發(fā)牌功能
本文實例為大家分享了java模擬斗地主發(fā)牌的具體代碼,供大家參考,具體內(nèi)容如下
1.案例介紹
規(guī)則:
- 組裝54張撲克牌
- 54張牌順序打亂
- 三個玩家參與游戲,三人交替摸牌,每人17張牌,后三張留作底牌
- 查看三人各自手中的牌(按照牌的大小排序)、底牌
2. 分析
1)、準(zhǔn)備牌:
完成數(shù)字與紙牌的映射關(guān)系:
使用雙列Map(HashMap)集合,完成一個數(shù)字與字符串紙牌的對應(yīng)關(guān)系(相當(dāng)于一個字典)。
2)、洗牌:
通過數(shù)字完成洗牌發(fā)牌
發(fā)牌: 將每個人以及底牌設(shè)計為ArrayList,將后3張牌直接存放于底牌,剩余牌通過對3取模依次發(fā)牌。
存放的過程中要求數(shù)字大小與斗地主規(guī)則的大小對應(yīng)。
將代表不同紙牌的數(shù)字分配給不同的玩家與底牌。
3)、看牌:
通過Map集合找到對應(yīng)字符展示。
通過查詢紙牌與數(shù)字的對應(yīng)關(guān)系,由數(shù)字轉(zhuǎn)成紙牌字符串再進(jìn)行展示。
3.代碼
public class Test7 { public static void main(String[] args) { //定義一個Map集合和List集合來存取牌號和索引 Map<Integer, String> map = new HashMap(); List<Integer> pokerindex = new ArrayList<>(); //定義牌 String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}; String[] color = {"♥", "♠", "♣", "♦"}; //存牌號和與之對應(yīng)的索引 int index = 0; for (String s : num) { for (String c : color) { map.put(index, c + s); pokerindex.add(index); index++; } } //存大小王 map.put(index, "大王"); pokerindex.add(index); index++; map.put(index, "小王"); pokerindex.add(index); //打亂牌組; Collections.shuffle(pokerindex); //創(chuàng)建四個集合 List<Integer> dipai = new ArrayList<>(); List<Integer> player1 = new ArrayList<>(); List<Integer> player2 = new ArrayList<>(); List<Integer> player3 = new ArrayList<>(); //將打亂的索引數(shù)組分配給三個人 for (int i = 0; i < pokerindex.size(); i++) { if (i > 50) { dipai.add(pokerindex.get(i)); } else if (i % 3 == 0) { player1.add(pokerindex.get(i)); } else if (i % 3 == 2) { player2.add(pokerindex.get(i)); } else if (i % 3 == 1) { player3.add(pokerindex.get(i)); } } //給每個人的牌組排序 Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); Collections.sort(dipai); //顯示每個人的牌組 show("張三", map, player1); show("李四", map, player2); show("王五", map, player3); show("底牌", map, dipai); } //定義一個方法用來顯示牌組 public static void show(String name, Map<Integer, String> map, List<Integer> player) { System.out.print(name); for (int i = 0; i < player.size(); i++) { Integer ii = player.get(i); System.out.print(map.get(ii) + " "); } System.out.println(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java 實現(xiàn)websocket的兩種方式實例詳解
這篇文章主要介紹了java 實現(xiàn)websocket的兩種方式實例詳解,一種使用tomcat的websocket實現(xiàn),一種使用spring的websocket,本文通過代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07基于Java Socket實現(xiàn)一個簡易在線聊天功能(一)
這篇文章主要給大家介紹基于Java Socket實現(xiàn)一個簡易在線聊天功能(一),分為客戶端和服務(wù)端兩段代碼,非常具有參考價值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05IDEA啟動Tomcat報Unrecognized option: --add-opens=java
這篇文章主要為大家介紹了解決IDEA啟動Tomcat報Unrecognized option: --add-opens=java.base/java.lang=ALL-UNNAMED的方法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08GateWay動態(tài)路由與負(fù)載均衡詳細(xì)介紹
這篇文章主要介紹了GateWay動態(tài)路由與負(fù)載均衡,GateWay支持自動從注冊中心中獲取服務(wù)列表并訪問,即所謂的動態(tài)路由2022-11-11MyBatis-plus數(shù)據(jù)庫字段排序不準(zhǔn)確的解決
這篇文章主要介紹了MyBatis-plus數(shù)據(jù)庫字段排序不準(zhǔn)確的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02