服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java模拟实现斗地主发牌

Java模拟实现斗地主发牌

2021-10-27 11:20菜鸟的第一天 Java教程

这篇文章主要为大家详细介绍了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):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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>:自然排序进行排序:

排序需要从三开始到大小王结束,所以需要新增一个进行排序比较的元素。

牌类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//实现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;
}

游戏类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//一副牌(笛卡尔积)
     {
         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>:排序比较器进行排序:

牌类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
    }
    
}

游戏类: 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//一副牌(笛卡尔积)
     {
         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));
        }
    }

实现效果:

Java模拟实现斗地主发牌

扩展:

以上代码实现发牌原理是给每名玩家一次性随机17张牌,与现实发牌逻辑不符;按照现实发牌逻辑,需要先进行洗牌,打乱牌的顺序,然后轮流给每名玩家发牌,直到剩余三张牌发给地主。

实现原理:在jdk1.2集合框架出现的同时,新增用于对集合处理的工具类 java.util.Collections,这个类与java.util.Arrays同一时间出现;Collections中提供的所有方法都是静态的,方法包括,对集合:查找,排序,洗牌,转换,拷贝,查找最大值/最小值,集合反转,安全集合的获取等一系列静态方法。

这里用到洗牌方法,Collections.shuffle(List<?> list) (使用默认的随机源随机排列指定的列表)。

代码实现:(将游戏类中发牌的方法进行修改)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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();
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_44873394/article/details/119140041

延伸 · 阅读

精彩推荐