基于java實(shí)現(xiàn)斗地主代碼實(shí)例解析
斗地主
規(guī)則:
1. 組裝54張撲克牌
2. 將54張牌順序打亂
3. 三個(gè)玩家參與游戲,三人交替摸牌,每人17張牌,最后三張留作底牌。
4. 查看三人各自手中的牌(按照牌的大小排序)、底牌
手中撲克牌從大到小的擺放順序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5, 4,3
分析:
準(zhǔn)備牌:
完成數(shù)字與紙牌的映射關(guān)系:
使用雙列Map(HashMap)集合,完成一個(gè)數(shù)字與字符串紙牌的對(duì)應(yīng)關(guān)系(相當(dāng)于一個(gè)字典)。
洗牌:
通過(guò)數(shù)字完成洗牌發(fā)牌
發(fā)牌:
將每個(gè)人以及底牌設(shè)計(jì)為ArrayList<String>,將最后3張牌直接存放于底牌,剩余牌通過(guò)對(duì)3取模依次發(fā)牌。
存放的過(guò)程中要求數(shù)字大小與斗地主規(guī)則的大小對(duì)應(yīng)。
將代表不同紙牌的數(shù)字分配給不同的玩家與底牌。
看牌:
通過(guò)Map集合找到對(duì)應(yīng)字符展示。
通過(guò)查詢紙牌與數(shù)字的對(duì)應(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)建四個(gè)容器 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('古天樂(lè)',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(); }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何基于Python Matplotlib實(shí)現(xiàn)網(wǎng)格動(dòng)畫2. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法3. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向4. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明5. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)6. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)7. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析8. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題9. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法10. PHP設(shè)計(jì)模式中工廠模式深入詳解
