久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

基于java實現斗地主代碼實例解析

瀏覽:6日期:2022-08-28 17:03:16

斗地主

規則:

1. 組裝54張撲克牌

2. 將54張牌順序打亂

3. 三個玩家參與游戲,三人交替摸牌,每人17張牌,最后三張留作底牌。

4. 查看三人各自手中的牌(按照牌的大小排序)、底牌

手中撲克牌從大到小的擺放順序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5, 4,3

分析:

準備牌:

完成數字與紙牌的映射關系:

使用雙列Map(HashMap)集合,完成一個數字與字符串紙牌的對應關系(相當于一個字典)。

洗牌:

通過數字完成洗牌發牌

發牌:

將每個人以及底牌設計為ArrayList<String>,將最后3張牌直接存放于底牌,剩余牌通過對3取模依次發牌。

存放的過程中要求數字大小與斗地主規則的大小對應。

將代表不同紙牌的數字分配給不同的玩家與底牌。

看牌:

通過Map集合找到對應字符展示。

通過查詢紙牌與數字的對應關系,由數字轉成紙牌字符串再進行展示。

基于java實現斗地主代碼實例解析

代碼:

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); //創建四個容器 ArrayList<Integer> player1=new ArrayList<Integer>(); ArrayList<Integer> player2=new ArrayList<Integer>(); ArrayList<Integer> player3=new ArrayList<Integer>(); ArrayList<Integer> bottom=new ArrayList<Integer>(); //發牌 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); //調用看牌的方法 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(); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 离岛区| 同心县| 涟水县| 右玉县| 临海市| 久治县| 新安县| 嘉善县| 宣武区| 巴林左旗| 和顺县| 潍坊市| 石河子市| 鄂州市| 奎屯市| 交城县| 宿松县| 桃江县| 新兴县| 资阳市| 清镇市| 寻甸| 建平县| 永修县| 颍上县| 霍州市| 武川县| 沿河| 托克逊县| 萝北县| 邓州市| 高邮市| 池州市| 和硕县| 资中县| 广饶县| 新疆| 广安市| 东丰县| 和硕县| 磐安县|