Java 用集合實現簡單的斗地主發牌


創建數組、集合,存放數據

public class FightAgainstLandlords {
    /**
     * poker集合,存儲54張牌
     */
    private ArrayList<String> poker;

    /**
     * colors數組存儲牌的花色
     */
    private String[] colors;

    /**
     * numbers數組存儲牌的值
     */
    private String[] numbers;
}

構造方法FightAgainstLandlords

public class FightAgainstLandlords {
    public FightAgainstLandlords(ArrayList<String> poker, String[] colors, String[] numbers) {
        this.poker = poker;
        this.colors = colors;
        this.numbers = numbers;
    }
}

定義打亂牌牌序方法

public class FightAgainstLandlords {
    /**
     * 存儲54張牌
     */
    public ArrayList<String> fiftyFive() {
        for (String color: colors) {
            for (String number: numbers) {
                poker.add(color + number);
            }
        }
        poker.add("大王");
        poker.add("小王");
        // 洗牌,調用Collections類的靜態方法shuffle(),用默認隨機源對指定列表進行置換
        Collections.shuffle(poker);
        return poker;
    }
}

發牌

public class FightAgainstLandlords {
    /**
     * 發牌
     * 獲取玩家牌或者底牌
     * j = 1, 2, 3 代表玩家牌
     * j = 其他數字 代表底牌
     */
    public ArrayList<String> licensing(int j, ArrayList<String> pokers) {
        // 三個玩家
        ArrayList<String> people1 = new ArrayList<>();
        ArrayList<String> people2 = new ArrayList<>();
        ArrayList<String> people3 = new ArrayList<>();
        // 底牌
        ArrayList<String> basePoker = new ArrayList<>();

        for (int i = 0; i < pokers.size(); i++) {
            String p = pokers.get(i);
            if ( i < 51) {
                if (i % 3 == 0) {
                    people1.add(p);
                } else if (i % 3 == 1) {
                    people2.add(p);
                } else {
                    people3.add(p);
                }
            } else {
                basePoker.add(p);
            }
        }

        // 返回玩家的牌、底牌
        if (j == 1) {
            return people1;
        } else if (j == 2) {
            return people2;
        } else if (j == 3) {
            return people3;
        } else {
            return basePoker;
        }
    }
}

測試FightAgainstLandlords類

import java.util.ArrayList;

public class DemoFightAgainstLandlords {
    public static void main(String[] args) {

        ArrayList<String> poker = new ArrayList<>();
        String[] colors = {"紅桃", "黑桃", "梅花", "方塊"};
        String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

        // new一個斗地主
        FightAgainstLandlords fightAgainstLandlords = new FightAgainstLandlords(poker, colors, numbers);
        // 54張牌
        ArrayList<String> pokers = fightAgainstLandlords.fiftyFive();

        // 獲取每個人的牌,和底牌
        ArrayList<String> people1 = fightAgainstLandlords.licensing(1, pokers);
        ArrayList<String> people2 = fightAgainstLandlords.licensing(2, pokers);
        ArrayList<String> people3 = fightAgainstLandlords.licensing(3, pokers);
        ArrayList<String> basePoker = fightAgainstLandlords.licensing(4, pokers);

        // 看一下它們每個人的牌,和底牌
        System.out.println("people1:" + people1);
        System.out.println("people2:" + people2);
        System.out.println("people3:" + people3);
        System.out.println("basePoker:" + basePoker);
    }
}
輸出結果(每個人的牌,和底牌都是隨機的):
people1:[紅桃3, 梅花J, 梅花K, 方塊J, 方塊K, 梅花10, 紅桃6, 梅花9, 黑桃Q, 紅桃Q, 梅花4, 黑桃A, 方塊2, 紅桃8, 方塊4, 黑桃8, 紅桃K]
people2:[梅花A, 方塊3, 小王, 黑桃J, 紅桃7, 方塊5, 方塊9, 黑桃10, 方塊8, 梅花Q, 方塊6, 梅花6, 紅桃10, 方塊Q, 黑桃5, 黑桃2, 紅桃A]
people3:[梅花5, 梅花8, 黑桃7, 黑桃4, 紅桃9, 黑桃9, 黑桃K, 方塊7, 黑桃6, 梅花3, 方塊10, 紅桃4, 黑桃3, 紅桃5, 大王, 紅桃J, 方塊A]
basePoker:[紅桃2, 梅花2, 梅花7]

FightAgainstLandlords類的所有代碼

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

public class FightAgainstLandlords {
    /**
     * poker集合,存儲54張牌
     * 不是斗地主也可以存儲52張牌(不存儲大王、小王牌)
     */
    private ArrayList<String> poker;

    /**
     * colors數組存儲牌的花色
     */
    private String[] colors;

    /**
     * numbers數組存儲牌的值
     */
    private String[] numbers;

    public FightAgainstLandlords(ArrayList<String> poker, String[] colors, String[] numbers) {
        this.poker = poker;
        this.colors = colors;
        this.numbers = numbers;
    }

    /**
     * 存儲54張牌
     */
    public ArrayList<String> fiftyFive() {
        for (String color: colors) {
            for (String number: numbers) {
                poker.add(color + number);
            }
        }
        poker.add("大王");
        poker.add("小王");
        // 洗牌,調用Collections類的靜態方法shuffle(),用默認隨機源對指定列表進行置換
        Collections.shuffle(poker);
        return poker;
    }

    /**
     * 發牌
     * 獲取玩家牌或者底牌
     * j = 1, 2, 3 代表玩家牌
     * j = 其他數字 代表底牌
     */
    public ArrayList<String> licensing(int j, ArrayList<String> pokers) {
        // 三個玩家
        ArrayList<String> people1 = new ArrayList<>();
        ArrayList<String> people2 = new ArrayList<>();
        ArrayList<String> people3 = new ArrayList<>();
        // 底牌
        ArrayList<String> basePoker = new ArrayList<>();

        for (int i = 0; i < pokers.size(); i++) {
            String p = pokers.get(i);
            if ( i < 51) {
                if (i % 3 == 0) {
                    people1.add(p);
                } else if (i % 3 == 1) {
                    people2.add(p);
                } else {
                    people3.add(p);
                }
            } else {
                basePoker.add(p);
            }
        }

        // 返回玩家的牌、底牌
        if (j == 1) {
            return people1;
        } else if (j == 2) {
            return people2;
        } else if (j == 3) {
            return people3;
        } else {
            return basePoker;
        }
    }
}


免責聲明!

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



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