(詳細通俗易懂)Java 實現 斗地主 案例 實現有序


上次已經介紹了Java實現斗地主案例到手的牌無序斗地主到手牌無序的案例,這次分享Java實現斗地主到手牌有序的實現。

斗地主案例實現步驟

分析:

首先來梳理一下規則:
(1)准備牌階段:斗地主總共54張牌,大王小王各一張(特殊對待),其他52張牌,分別4種花色,每種花色13張。四種花色分別為♥ ♦ ♠ ♣;每一種花色中的13張牌(由大到小)2 A K Q J 10 9 8 7 6 5 4 3,定義Map集合進行存儲牌的索引和組裝好的牌,循環遍歷兩個集合組裝52張牌;如:♥ 7,♠ 8等;

(2)洗牌階段:使用集合工具類Collections方法,其中static void shuffle(List<?> list)方法對牌進行隨機打亂。

(3)發牌階段:要求每一位玩家擁有17張牌,剩余三張作為底牌,一人一張輪流發牌:集合的索引(0-53)%3,定義4個集合,來存儲3個玩家的牌和場上的底牌。索引%3,有三個值(0,1,2),0%3=0,1%3=1,2%3=2,3%3=0,就可以給三名玩家發牌了,當索引>=51時,改為發底牌.

(4)排序階段:使用Collections中的方法,sort方法對集合中的元素進行排序

(5)看牌階段:使用查表法,遍歷一個集合,獲取到另一個集合的key,通過key查找到value。

實現:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class DouDiZhu {
public static void main(String[] args) {
    //1.准備牌
    //創建一個Map集合,存儲牌的索引和組裝好的牌
    HashMap<Integer, String> poker = new HashMap<>();
    //創建一個ArrayList集合,存儲牌的索引
    ArrayList<Integer> pokerNumber = new ArrayList<>();
    //定義兩個數組存儲花色和牌的順序
    String[] colors = {"♠", "♥", "♣", "♦"};
    String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
    //先把大王和小王存儲到集合中
    //定義第一張牌的索引
    int index = 0;
    poker.put(index, "大王");
    pokerNumber.add(index);
    index++;
    poker.put(index, "小王");
    pokerNumber.add(index);
    index++;
    //循環嵌套遍歷兩個數組,花色和順序,組裝52張牌,存儲到集合中
    for (String number : numbers) {
        for (String color : colors) {
            poker.put(index, color + number);
            pokerNumber.add(index);
            index++;
        }
    }
    //2.洗牌
    //使用Collections中的方法shuffle(List)方法,對poker的索引進行洗牌
    Collections.shuffle(pokerNumber);
    //3.發牌,定義四個集合,存儲玩家牌的索引和底牌的索引
    ArrayList<Integer> player01 = new ArrayList<>();
    ArrayList<Integer> player02 = new ArrayList<>();
    ArrayList<Integer> player03 = new ArrayList<>();
    ArrayList<Integer> diPai = new ArrayList<>();
    //遍歷索引ArrayList集合,獲取每一張牌的索引
    for (int i = 0; i < pokerNumber.size(); i++) {
        Integer in = pokerNumber.get(i);
        //分出三張底牌
        if (i >= 51) {
            //給底牌發牌
            diPai.add(in);
        } else if (i % 3 == 0) {
            //給玩家1發牌
            player01.add(in);
        } else if (i % 3 == 1) {
            //給玩家2發牌
            player02.add(in);
        } else if (i % 3 == 2) {
            //給玩家3發牌
            player03.add(in);
        }
    }
    //4.排序,使用Collections中的方法sort(List),對玩家的牌進行排序
    Collections.sort(player01);
    Collections.sort(player02);
    Collections.sort(player03);
    //5.看牌,為了提高代碼的復用性,定義一個方法
    lookPoker("玩家1",poker,player01);
    lookPoker("玩家2",poker,player02);
    lookPoker("玩家3",poker,player03);
    lookPoker("底牌",poker,diPai);


}

/*看牌的方法,應該傳遞姓名,Map集合和索引
  查表法:遍歷玩家或底牌集合,獲取牌的索引
  使用牌的索引去Map集合,找到對應的牌*/
public static void lookPoker(String name, HashMap<Integer, String> poker, ArrayList<Integer> list) {
    //輸出玩家姓名
    System.out.print(name+": ");
    //遍歷玩家或底牌集合,獲取牌的索引
    for(Integer key:list){
        //通過牌的索引,通過Map集合get()方法找到牌
        String value = poker.get(key);
        //輸出牌
        System.out.print(value+" ");
    }
    System.out.println();
  }
}

結果

玩家1: ♦2 ♦A ♣K ♦K ♠Q ♥10 ♣10 ♣9 ♥8 ♣8 ♦8 ♦6 ♠5 ♦5 ♥4 ♣3 ♦3 
玩家2: ♠2 ♠K ♥K ♥Q ♣Q ♦Q ♥J ♥9 ♦9 ♠7 ♣7 ♦7 ♣6 ♥5 ♣5 ♠3 ♥3 
玩家3: 大王 小王 ♥2 ♣2 ♠A ♥A ♣A ♠J ♣J ♦J ♠9 ♥7 ♠6 ♥6 ♠4 ♣4 ♦4 
底牌: ♦10 ♠8 ♠10 

感謝您的閱讀,不足之處歡迎指正!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM