Java高級(jí)應(yīng)用之斗地主游戲
斗地主綜合案例,供大家參考,具體內(nèi)容如下
運(yùn)用HashMap、ArrayList、List類(lèi)實(shí)現(xiàn)斗地主綜合案例,模擬斗地主游戲的隨機(jī)發(fā)牌,并按照牌的大小和花色進(jìn)行排列。
斗地主玩家每輪都有三個(gè)玩家,運(yùn)用Collections類(lèi)中的shuffle()方法打亂一整幅撲克牌,利用取余原理將湊亂的牌發(fā)放給三個(gè)玩家,整副牌發(fā)完后的最后三張永一個(gè)ArrayList存儲(chǔ)作為底牌。具體代碼實(shí)現(xiàn)如下:
import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;/***斗地主綜合案例:有序版本*1.準(zhǔn)備牌*2.洗牌*3.發(fā)牌*4.排序*5.看牌**/public class DouDizhu02 { public static void main(String[] args) {//1。準(zhǔn)備牌//創(chuàng)建一個(gè)Map集合,存儲(chǔ)牌的索引和組裝好的牌HashMap<Integer, String> poker = new HashMap<>();//List集合,存儲(chǔ)牌的索引ArrayList<Integer> pokerIndex = new ArrayList<>();//定義兩個(gè)集合,存儲(chǔ)花色和序號(hào)List<String> colors = List.of('♥', '♦', '♠', '♣');List<String> numbers = List.of('2', 'A', 'K', 'Q', 'J', '10', '9', '8', '7', '6', '5', '4','3');//把大王和小王存儲(chǔ)到集合中int index = 0;poker.put(index, '大王');pokerIndex.add(index);index++;poker.put(index, '小王');pokerIndex.add(index);index++;//循環(huán)嵌套52張牌for(String number: numbers){ for(String color : colors){poker.put(index, color + number);pokerIndex.add(index);index++; }}//System.out.println(poker);//System.out.println(pokerIndex);/*2.洗牌使用Collections中的方法shuffle(list) */Collections.shuffle(pokerIndex);/*3.發(fā)牌 *///定義四個(gè)集合,存儲(chǔ)玩家牌的索引,和底牌的索引ArrayList<Integer> player01 = new ArrayList<>();ArrayList<Integer> player02 = new ArrayList<>();ArrayList<Integer> player03 = new ArrayList<>();ArrayList<Integer> dipai = new ArrayList<>();//遍歷存儲(chǔ)牌索引的list集合,獲取每一個(gè)牌的索引for (int i = 0; i < pokerIndex.size(); i++) { Integer in = pokerIndex.get(i); if(in >= 51){dipai.add(in); }else if(i % 3 == 0){player01.add(in); }else if(i % 3 == 1){player02.add(in); }else if(i % 3 == 2){player03.add(in); }}/*4.排序使用Collections中的方法sort(List) */Collections.sort(player01);Collections.sort(player02);Collections.sort(player03);Collections.sort(dipai);/*5.看牌調(diào)用看牌的方法 */lookPoker('劉德華', poker, player01);lookPoker('周星馳', poker, player02);lookPoker('周潤(rùn)發(fā)', poker, player03);lookPoker('底牌', poker, dipai); } /* 定義一個(gè)看牌的方法,提高代碼的復(fù)用性 參數(shù): String name:玩家名稱(chēng) HashMap<Integer, String> poker:存儲(chǔ)牌的poker集合 ArrayList<Integer> list:存儲(chǔ)玩家和底牌的list集合 查表法: 遍歷玩家或者底牌集合,獲取牌的索引 使用牌的索引,去Map集合中找到對(duì)應(yīng)的牌 */ public static void lookPoker(String name, HashMap<Integer, String> poker, ArrayList<Integer> list){//輸出玩家名稱(chēng),不換行System.out.print(name + ' :');for(int key : list){ String value = poker.get(key); System.out.print(value + ' ');}System.out.println(); }}
運(yùn)行結(jié)果如下
牌為隨機(jī)打亂,每次運(yùn)行結(jié)果都不一樣。
劉德華 :♥2 ♦2 ♠A ♦K ♠Q ♣Q ♥9 ♣9 ♥8 ♣8 ♦7 ♠7 ♣7 ♦6 ♣6 ♥5 ♠5 ♥3 周星馳 :♠2 ♥A ♦A ♣K ♦Q ♥J ♦J ♣J ♥10 ♦10 ♠10 ♦9 ♠9 ♦8 ♥7 ♠4 ♣4 周潤(rùn)發(fā) :大王 小王 ♣2 ♣A ♥K ♠K ♥Q ♠J ♣10 ♠8 ♥6 ♠6 ♦5 ♣5 ♥4 ♦4 底牌 :♦3 ♠3 ♣3
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)2. 詳解Android studio 動(dòng)態(tài)fragment的用法3. 什么是python的自省4. 解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題5. Springboot Druid 自定義加密數(shù)據(jù)庫(kù)密碼的幾種方案6. Vuex localStorage的具體使用7. php模擬實(shí)現(xiàn)斗地主發(fā)牌8. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)9. Spring MVC+ajax進(jìn)行信息驗(yàn)證的方法10. vue 使用localstorage實(shí)現(xiàn)面包屑的操作

網(wǎng)公網(wǎng)安備