基于java實(shí)現(xiàn)斗地主代碼實(shí)例解析
斗地主
規(guī)則:
1. 組裝54張撲克牌
2. 將54張牌順序打亂
3. 三個玩家參與游戲,三人交替摸牌,每人17張牌,最后三張留作底牌。
4. 查看三人各自手中的牌(按照牌的大小排序)、底牌
手中撲克牌從大到小的擺放順序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5, 4,3
分析:
準(zhǔn)備牌:
完成數(shù)字與紙牌的映射關(guān)系:
使用雙列Map(HashMap)集合,完成一個數(shù)字與字符串紙牌的對應(yīng)關(guān)系(相當(dāng)于一個字典)。
洗牌:
通過數(shù)字完成洗牌發(fā)牌
發(fā)牌:
將每個人以及底牌設(shè)計(jì)為ArrayList<String>,將最后3張牌直接存放于底牌,剩余牌通過對3取模依次發(fā)牌。
存放的過程中要求數(shù)字大小與斗地主規(guī)則的大小對應(yīng)。
將代表不同紙牌的數(shù)字分配給不同的玩家與底牌。
看牌:
通過Map集合找到對應(yīng)字符展示。
通過查詢紙牌與數(shù)字的對應(yīng)關(guān)系,由數(shù)字轉(zhuǎn)成紙牌字符串再進(jìn)行展示。
代碼:
package com.oracle.demo01;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.Map;public class DouDiZhu { public static void main(String[] args) { Map<Integer, String> pooker=new HashMap<Integer, String>(); ArrayList<Integer> pookerNumer=new ArrayList<Integer>(); //封裝Map String[] color={'♠','♦','♥','♣'}; String[] number={'2','A','K','Q','J','10','9','8','7','6','5','4','3'}; int index=2; for (String n : number) { for (String c : color) {//封裝Mappooker.put(index, c+n);//封裝集合pookerNumer.add(index);index++; } } //封裝大小王 pooker.put(0, '大王'); pookerNumer.add(0); pooker.put(1, '小王'); pookerNumer.add(1); //System.out.println(pooker); //System.out.println(pookerNumer); //洗牌 Collections.shuffle(pookerNumer); //System.out.println(pookerNumer); //創(chuàng)建四個容器 ArrayList<Integer> player1=new ArrayList<Integer>(); ArrayList<Integer> player2=new ArrayList<Integer>(); ArrayList<Integer> player3=new ArrayList<Integer>(); ArrayList<Integer> bottom=new ArrayList<Integer>(); //發(fā)牌 for (int i = 0; i< pookerNumer.size(); i++) { if(i<3){bottom.add(pookerNumer.get(i)); }else if(i%3==0){player1.add(pookerNumer.get(i)); }else if(i%3==1){player2.add(pookerNumer.get(i)); }else if(i%3==2){player3.add(pookerNumer.get(i)); } } //排序 Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); Collections.sort(bottom); //調(diào)用看牌的方法 look('渣渣灰',pooker,player1); look('古天樂',pooker,player2); look('劉嘉玲',pooker,player3); look('底牌',pooker,bottom); } //看牌的方法 public static void look(String name,Map<Integer, String> pooker,ArrayList<Integer> player){ System.out.println(name+':'); for (int num : player) { System.out.print(pooker.get(num)+' '); } System.out.println(); }}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JavaWeb Servlet中url-pattern的使用2. jsp中sitemesh修改tagRule技術(shù)分享3. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼4. React優(yōu)雅的封裝SvgIcon組件示例5. 輕松學(xué)習(xí)XML教程6. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除9. ASP基礎(chǔ)知識VBScript基本元素講解10. 詳解瀏覽器的緩存機(jī)制
