java模擬斗地主發(fā)牌功能
本文實(shí)例為大家分享了java模擬斗地主發(fā)牌的具體代碼,供大家參考,具體內(nèi)容如下
1.案例介紹
規(guī)則:
組裝54張撲克牌 54張牌順序打亂 三個(gè)玩家參與游戲,三人交替摸牌,每人17張牌,后三張留作底牌 查看三人各自手中的牌(按照牌的大小排序)、底牌2. 分析
1)、準(zhǔn)備牌:
完成數(shù)字與紙牌的映射關(guān)系:使用雙列Map(HashMap)集合,完成一個(gè)數(shù)字與字符串紙牌的對(duì)應(yīng)關(guān)系(相當(dāng)于一個(gè)字典)。
2)、洗牌:
通過(guò)數(shù)字完成洗牌發(fā)牌發(fā)牌: 將每個(gè)人以及底牌設(shè)計(jì)為ArrayList,將后3張牌直接存放于底牌,剩余牌通過(guò)對(duì)3取模依次發(fā)牌。存放的過(guò)程中要求數(shù)字大小與斗地主規(guī)則的大小對(duì)應(yīng)。將代表不同紙牌的數(shù)字分配給不同的玩家與底牌。
3)、看牌:
通過(guò)Map集合找到對(duì)應(yīng)字符展示。通過(guò)查詢紙牌與數(shù)字的對(duì)應(yīng)關(guān)系,由數(shù)字轉(zhuǎn)成紙牌字符串再進(jìn)行展示。
3.代碼
public class Test7 { public static void main(String[] args) { //定義一個(gè)Map集合和List集合來(lái)存取牌號(hào)和索引 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 = {'♥', '♠', '♣', '♦'}; //存牌號(hào)和與之對(duì)應(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)建四個(gè)集合 List<Integer> dipai = new ArrayList<>(); List<Integer> player1 = new ArrayList<>(); List<Integer> player2 = new ArrayList<>(); List<Integer> player3 = new ArrayList<>(); //將打亂的索引數(shù)組分配給三個(gè)人 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)); } } //給每個(gè)人的牌組排序 Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); Collections.sort(dipai); //顯示每個(gè)人的牌組 show('張三', map, player1); show('李四', map, player2); show('王五', map, player3); show('底牌', map, dipai); } //定義一個(gè)方法用來(lái)顯示牌組 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(); }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
