溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用java實(shí)現(xiàn)一個斗地主游戲

發(fā)布時間:2021-02-25 16:35:42 來源:億速云 閱讀:321 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)使用java實(shí)現(xiàn)一個斗地主游戲,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

斗地主案例

按照斗地主的規(guī)則,完成洗牌發(fā)牌的動作。
具體規(guī)則: 使用54張牌打亂順序,三個玩家參與游戲,三人交替摸牌,每人17張牌,后三張留作底牌

具體操作如下

1、準(zhǔn)備牌:

完成數(shù)字與紙牌的映射關(guān)系:
使用雙列Map(HashMap)集合,完成一個數(shù)字與字符串紙牌的對應(yīng)關(guān)系(相當(dāng)于一個字典)。

2、洗牌:

通過數(shù)字完成洗牌發(fā)牌

3、發(fā)牌:

將每個人以及底牌設(shè)計為ArrayList,將后3張牌直接存放于底牌,剩余牌通過對3取模依次發(fā)牌。
存放的過程中要求數(shù)字大小與斗地主規(guī)則的大小對應(yīng)。
將代表不同紙牌的數(shù)字分配給不同的玩家與底牌。

4、看牌: 通過Map集合找到對應(yīng)字符展示。

通過查詢紙牌與數(shù)字的對應(yīng)關(guān)系,由數(shù)字轉(zhuǎn)成紙牌字符串再進(jìn)行展示。

/**
 *斗地主案例
 * @program: practice_masaike
 * @author: csl
 * @create: 2021-02-23 16:02
 **/

/**
 *步驟如下
 *1.準(zhǔn)備牌
 *2.洗牌
 *3.發(fā)牌
 *4.排序
 *5.看牌
 **/
public class Poker {
 public static void main(String[] args) {
  //1.準(zhǔn)備牌
  //創(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){
    //重點(diǎn)注意 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);

  /**
   * 進(jìn)行發(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.進(jìn)行牌的排序
   * 使用Collectiond中的方法sort(List) 默認(rèn)是升序排序
   **/
  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);
 }


 /**
  * 定義一個看牌的方法,提高代碼的復(fù)用性
  * 參數(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+": ");
  }
  //打印完每一個玩家的牌后,進(jìn)行換行操作
  System.out.println();
 }
}

上述就是小編為大家分享的使用java實(shí)現(xiàn)一個斗地主游戲了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI