Java模擬實現鬥地主發牌

本文實例為大傢分享瞭Java模擬實現鬥地主發牌的具體代碼,供大傢參考,具體內容如下

題目:

模擬鬥地主的發牌實現,54張牌,每張牌不同的花色(紅心,黑桃,方塊,梅花),牌的點數(3,4,5,6,7,8,9,10,J,Q,K,A,2,King,Queen),另有三名玩傢,要求(使用面向對象的方式實現):

1、隨機產生一名地主
2、隨機向三名玩傢發牌,最後的底牌自動發給地主
3、顯示出每名玩傢手中的牌
4、要求根據點數自動對玩傢手中的牌排序實現(*)

提示:玩傢類,牌類,遊戲類(算法)

步驟分析:

1.牌類:有點數和花色的差別,其中大王和小王隻有點數沒有花色

2.玩傢類:玩傢姓名,是否是地主,手中的牌(牌類集合)

3.遊戲類(實現):(首先知道發牌規則,總共54張撲克牌,每人17張,剩下3張撲克給到地主手中。)

設置一個初始化塊默認一副撲克牌,初始化三名玩傢,隨機一名玩傢為地主,三名玩傢隨機獲得17張牌,已獲得的牌要從集合中刪除。

代碼實現:

牌類(Poker):

public class Poker {
    /**點數*/
    private String point;
    /**花色*/
    private String flower;
 
    public Poker() {
    }
 
    public Poker(String point, String flower) {
        this.point = point;
        this.flower = flower;
    }
 
    public String getPoint() {
        return point;
    }
 
    public void setPoint(String point) {
        this.point = point;
    }
 
    public String getFlower() {
        return flower;
    }
 
    public void setFlower(String flower) {
        this.flower = flower;
    }
 
    @Override
    public String toString() {
        if (Objects.isNull(flower)){
        return point;
        }
        return flower+""+point;
    }
}

玩傢類(Player):

public class Player {
    /**玩傢昵稱*/
    private String nickname;
    private boolean boss;
    /**手中的牌*/
    private List<Poker>pokers = new ArrayList<>();
 
    public Player() {
    }
 
    public Player(String nickname) {
        this.nickname = nickname;
    }
 
    public String getNickname() {
        return nickname;
    }
 
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
 
    public boolean isBoss() {
        return boss;
    }
 
    public void setBoss(boolean boss) {
        this.boss = boss;
    }
 
    public List<Poker> getPokers() {
        return pokers;
    }
 
    public void setPokers(List<Poker> pokers) {
        this.pokers = pokers;
    }
 
    @Override
    public String toString() {
        return nickname+(boss?"(地主)":"(農民)")+pokers;
    }
}

遊戲類(Game):

public class Game {
    /**存儲所有牌的集合*/
    private  List<Poker> list = new ArrayList<>();
    private  String[] points = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
    private  String[] flowers = {"❤","♣","♦","♠"};
    private List<Player> players = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);
    private static Random randomUtils = new Random();
    //一副牌(笛卡爾積)
     {
        for (int i = 0; i<points.length; i++){
            for (int j = 0; j < flowers.length; j++) {
                //將帶花色的撲克牌加入集合
                list.add(new Poker(points[i],flowers[j]));
            }
        }
        //加入大小王
        list.add(new Poker("Queen",null));
        list.add(new Poker("King",null));
    }
 
    /**
     * 初始化三名玩傢
     */
    public void playerJoin(){
        System.out.println("請輸入玩傢昵稱1");
        String p1 = sc.nextLine();
        System.out.println("請輸入玩傢昵稱2");
        String p2 = sc.nextLine();
        System.out.println("請輸入玩傢昵稱3");
        String p3 = sc.nextLine();
        players.add(new Player(p1));
        players.add(new Player(p2));
        players.add(new Player(p3));
    }
 
    private void startGame(){
        //玩傢加入
        playerJoin();
        //隨機一個地主索引
        //nextInt(int bound)
        //返回偽隨機的,均勻分佈 int值介於0(含)和指定值(不包括),從該隨機數生成器的序列繪制。
        int index = randomUtils.nextInt(players.size());
        //設置指定位的玩傢為地主
        Player boss = players.get(index);
        boss.setBoss(true);
        System.out.println(boss.getNickname()+"是地主!");
        //開始發牌
        for (int i = 0; i < players.size(); i++) {
            //獲取當前遍歷到的玩傢對象
            Player player = players.get(i);
            for (int j = 0; j < 17; j++) {
                //隨機獲取一張牌給當前玩傢
                int n = randomUtils.nextInt(list.size());
                //將隨機到的牌存儲到玩傢的牌集合中
                player.getPokers().add(list.get(n));
                //將已經被取走的牌從原集合中刪除
                list.remove(n);
            }
        }
        //將剩餘三張牌給地主
        boss.getPokers().addAll(list);
        showPoker();
    }
 
    private void  showPoker(){
        for (Player player : players) {
            System.out.println(player);
        }
    }
 
 
    public static void main(String[] args) {
        new Game().startGame();
    }
}

排序:

以上實現瞭基礎的發牌功能,但沒有對牌進行排序。java集合框架出現的同時,由於實際開發的數據的排序要求,所以JDK引入用於排序的兩個接口:

Comparable<T>:自然排序

Comparator<T>:排序比較器

使用Comparable<T>:自然排序進行排序:

排序需要從三開始到大小王結束,所以需要新增一個進行排序比較的元素。

牌類:

//實現Comparable接口,實現其comparaTo(T t)方法
public class Poker implements Comparable<Poker>
/**用於排序的屬性*/
    private int sort;
 
    public Poker(String point, String flower, int sort) {
        this.point = point;
        this.flower = flower;
        this.sort = sort;
    }
 
    public int getSort() {
        return sort;
    }
 
    public void setSort(int sort) {
        this.sort = sort;
    }
 
//實現方法進行排序
@Override
    public int compareTo(Poker p) {
        return this.sort-p.sort;
}

遊戲類:

//一副牌(笛卡爾積)
     {
         int sort = 0;
        for (int i = 0; i<points.length; i++){
            for (int j = 0; j < flowers.length; j++) {
                Poker p = new Poker(points[i],flowers[j],sort);
                //將帶花色的撲克牌加入集合
                list.add(p);
            }
            sort++;
        }
        //加入大小王
         list.add(new Poker("Queen", null,13));
         list.add(new Poker("King", null,14));
     }
 
  private void  showPoker(){
        //排序實現
        Collections.sort(players.get(0).getPokers());
        Collections.sort(players.get(1).getPokers());
        Collections.sort(players.get(2).getPokers());
        for (Player player : players) {
            System.out.println(player);
        }
}

使用Comparator<T>:排序比較器進行排序:

牌類:

public class Poker {
    /**點數*/
    private String point;
    /**花色*/
    private String flower;
    /**用於排序的屬性*/
    private int size;
 
    public Poker() {
    }
 
    public Poker(String point, String flower) {
        this.point = point;
        this.flower = flower;
    }
 
    public Poker(String point, String flower, int size) {
        this.point = point;
        this.flower = flower;
        this.size = size;
    }
 
    public String getPoint() {
        return point;
    }
 
    public void setPoint(String point) {
        this.point = point;
    }
 
    public String getFlower() {
        return flower;
    }
 
    public void setFlower(String flower) {
        this.flower = flower;
    }
 
    public int getSize() {
        return size;
    }
 
    public void setSize(int size) {
        this.size = size;
    }
    
 
    @Override
    public String toString() {
        if (Objects.isNull(flower)){
        return point;
        }
        return flower+""+point;
    }
    
}

遊戲類: 

//一副牌(笛卡爾積)
     {
         int size = 0;
        for (int i = 0; i<points.length; i++){
            for (int j = 0; j < flowers.length; j++) {
                Poker p = new Poker(points[i],flowers[j],size);
                //將帶花色的撲克牌加入集合
                list.add(p);
            }
            //加入點數
            size++;
        }
        //加入大小王
 
        list.add(new Poker("Queen", null,13));
        list.add(new Poker("King", null,14));
     }
 
 
    private void  showPoker(){
        //排序實現
        for (int i = 0; i < players.size(); i++) {
            players.get(i).getPokers().sort(new Comparator<Poker>() {
                @Override
                public int compare(Poker p1, Poker p2) {
                    return p1.getSize()-p2.getSize();
                }
            });
            System.out.println(players.get(i));
        }
    }

實現效果:

擴展:

以上代碼實現發牌原理是給每名玩傢一次性隨機17張牌,與現實發牌邏輯不符;按照現實發牌邏輯,需要先進行洗牌,打亂牌的順序,然後輪流給每名玩傢發牌,直到剩餘三張牌發給地主。

實現原理:在jdk1.2集合框架出現的同時,新增用於對集合處理的工具類 java.util.Collections,這個類與java.util.Arrays同一時間出現;Collections中提供的所有方法都是靜態的,方法包括,對集合:查找,排序,洗牌,轉換,拷貝,查找最大值/最小值,集合反轉,安全集合的獲取等一系列靜態方法。

這裡用到洗牌方法,Collections.shuffle(List<?> list) (使用默認的隨機源隨機排列指定的列表)。

代碼實現:(將遊戲類中發牌的方法進行修改)

private void startGame() {
        //玩傢加入
        playerJoin();
        //隨機一個地主索引
        int index = randomUtils.nextInt(players.size());
        //設置指定位的玩傢為地主
        Player boss = players.get(index);
        boss.setBoss(true);
        System.out.println(boss.getNickname() + "是地主!");
        //洗牌
        Collections.shuffle(list);
        //開始發牌
//        for (int i = 0; i < players.size(); i++) {
//            //獲取當前遍歷到的玩傢對象
//            Player player = players.get(i);
//            for (int j = 0; j < 17; j++) {
//                //隨機獲取一張牌給當前玩傢
//                int n = randomUtils.nextInt(list.size());
//                //將隨機到的牌存儲到玩傢的牌集合中
//                player.getPokers().add(list.get(n));
//                //將已經被取走的牌從原集合中刪除
//                list.remove(n);
//            }
//        }
        for (int i = 0; i < list.size(); i++) {
            if (i < 51) {
                switch (i % 3) {
                    case 0:
                        players.get(0).getPokers().add(list.get(i));
                        break;
                    case 1:
                        players.get(1).getPokers().add(list.get(i));
                        break;
                    case 2:
                        players.get(2).getPokers().add(list.get(i));
                        break;
                    default:
                        break;
                }
            } else {
                //將剩餘三張牌給地主
                boss.getPokers().add(list.get(i));
            }
        }
        showPoker();
    }

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: