java實現(xiàn)斗地主小案例
本文實例為大家分享了java實現(xiàn)斗地主案例的具體代碼,供大家參考,具體內(nèi)容如下
斗地主案例按照斗地主的規(guī)則,完成洗牌發(fā)牌的動作。具體規(guī)則: 使用54張牌打亂順序,三個玩家參與游戲,三人交替摸牌,每人17張牌,后三張留作底牌
具體操作如下
1、準備牌:
完成數(shù)字與紙牌的映射關(guān)系:使用雙列Map(HashMap)集合,完成一個數(shù)字與字符串紙牌的對應關(guān)系(相當于一個字典)。
2、洗牌:
通過數(shù)字完成洗牌發(fā)牌
3、發(fā)牌:
將每個人以及底牌設計為ArrayList,將后3張牌直接存放于底牌,剩余牌通過對3取模依次發(fā)牌。存放的過程中要求數(shù)字大小與斗地主規(guī)則的大小對應。將代表不同紙牌的數(shù)字分配給不同的玩家與底牌。
4、看牌: 通過Map集合找到對應字符展示。
通過查詢紙牌與數(shù)字的對應關(guān)系,由數(shù)字轉(zhuǎn)成紙牌字符串再進行展示。
/** *斗地主案例 * @program: practice_masaike * @author: csl * @create: 2021-02-23 16:02 **//** *步驟如下 *1.準備牌 *2.洗牌 *3.發(fā)牌 *4.排序 *5.看牌 **/public class Poker { public static void main(String[] args) { //1.準備牌 //創(chuàng)建一個Map集合,存儲牌的索引和組裝好的牌 HashMap<Integer,String> poker= new HashMap<>(); //創(chuàng)建一個List集合,存儲牌的的索引 ArrayList<Integer> pokerIndex= new ArrayList<>(); //定義連個集合 存儲牌的花色和牌的序號 List<String> colors = new ArrayList<String>(); List<String> numbers = new ArrayList<String>(); //List<String> colors= Array.asList('♣','♦', '♥', '♠'); //List<String> numbers= List.of('2', 'A', 'K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3'); /** * Collections集合的方法 * public static <T> boolean addAll(Collection<T> c, T... elements) `:往集合中添加一些元素。 **/ Collections.addAll(colors,'♣','♦', '♥', '♠'); Collections.addAll(numbers,'2', 'A', 'K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4', '3'); //把大王和小王存儲到集合中 //定義一個牌的索引 int index=0; poker.put(index,'大王'); pokerIndex.add(index); index++; //1 poker.put(index,'小王'); pokerIndex.add(index); index++; //2 //循環(huán)嵌套遍歷兩個集合,組合52張牌,存儲到集合中 for(String number : numbers){ for(String color : colors){ //重點注意 Map集合poker的key為index poker.put(index,color+number); pokerIndex.add(index); index++; //3 } }// System.out.println(poker);// System.out.println(pokerIndex); /** * 2.洗牌 * 使用Collections中的方法shuffle(List) **/ Collections.shuffle(pokerIndex); //System.out.println(pokerIndex); /** * 進行發(fā)牌 **/ //需要定義四個集合,存儲玩家牌的索引和底牌的索引 ArrayList<Integer> play01 = new ArrayList<>(); ArrayList<Integer> play02 = new ArrayList<>(); ArrayList<Integer> play03 = new ArrayList<>(); //底牌集合 ArrayList<Integer> diPai = new ArrayList<>(); /** * 遍歷存儲牌索引的List集合,獲取每一個牌的索引 **/ for(int i =0;i<pokerIndex.size();i++){ Integer in=pokerIndex.get(i); //先判斷底牌 if (i >= 51) { //給底牌發(fā)牌 diPai.add(in); }else if(i%3==0){ //給玩家1發(fā)牌 play01.add(in); }else if(i%3==1){ //給玩家1發(fā)牌 play02.add(in); }else if(i%3==2){ //給玩家1發(fā)牌 play03.add(in); } } /** * 4.進行牌的排序 * 使用Collectiond中的方法sort(List) 默認是升序排序 **/ Collections.sort(play01); Collections.sort(play02); Collections.sort(play03); Collections.sort(diPai); /** * 5.看牌 * 調(diào)用看牌的方法 **/ lookPoker('張三',poker,play01); lookPoker('李四',poker,play02); lookPoker('王五',poker,play03); lookPoker('底牌',poker,diPai); } /** * 定義一個看牌的方法,提高代碼的復用性 * 參數(shù) * String name:玩家名稱 * HashMap<Integer,String> poker:存儲牌的poker集合 * ArrayList<Integer> pokerIndex:存儲玩家和底牌的List集合 * * 查表發(fā): * 遍歷玩家或者底牌集合,獲取牌的索引 * 使用牌的索引,去Map集合中找到對對那個的牌 **/ public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){ //輸出玩家的名稱 System.out.print(name+': '); for(Integer key : list){ //使用牌的索引,去Map集合中找到對對那個的牌 String value=poker.get(key); System.out.print(value+': '); } //打印完每一個玩家的牌后,進行換行操作 System.out.println(); }}
第一次洗牌的結(jié)果
第二次洗牌的結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
