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

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

服务器之家 - 编程语言 - Java教程 - java实现斗地主游戏

java实现斗地主游戏

2021-07-02 15:01zzm0707 Java教程

这篇文章主要为大家详细介绍了java实现斗地主游戏,洗牌、发牌、看牌,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

感想:

第一次写博客,感觉编辑器挺复杂厉害的,感觉自己的内容挺简单的。有什么问题请多多指教!

思路:

1、创建一个扑克牌的实体类poker,设置了四个参数:花色、数字、牌值(判断大小)、是否地主牌,实现getset方法和构造方法;
2、创建一个玩家的实体类player,设置了四个参数: 初始牌集合,排序后牌集合,牌值集合,是否地主,实现getset方法和构造方法;
3、洗牌:循环嵌套花色数组跟数字数组生成52个poker,手动加入大小王poker,放进map(int,poker)里面,利用collections.shuffle随机排序map的key,再根据key生成排序后的poker集合
4、发牌:生成3个玩家对象,拿到洗牌后的集合,前51张牌为玩家牌,后3张为地主牌,在51张牌中随机一张为地主牌,遍历玩家牌分配个每一个player,同时根据玩家牌的牌值生成集合grades存进player对象中,将grades转为int数组并进行升序排序,根据grade取得新poker集合分配给玩家。

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.company;
 
public class poker {
  private string color;
  private string number;
  private int grade;
  boolean islandowners;
 
 public boolean islandowners() {
  return islandowners;
 }
 
 public void setlandowners(boolean landowners) {
  islandowners = landowners;
 }
 
 public int getgrade() {
  return grade;
 }
 
 public void setgrade(int grade) {
  this.grade = grade;
 }
 
 public string getcolor() {
  return color;
 }
 
 public void setcolor(string color) {
  this.color = color;
 }
 
 public string getnumber() {
  return number;
 }
 
 public void setnumber(string number) {
  this.number = number;
 }
 
 public poker() {
 }
 
 public poker(string color, string number ,int grade,boolean islandowners) {
  this.color = color;
  this.number = number;
  this.grade=grade;
  this.islandowners=islandowners;
 }
 
 
 public string tostring() {
  return this.getcolor()+this.getnumber();
 }
}

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
44
45
46
package com.company;
 
import java.util.arraylist;
import java.util.linkedlist;
 
public class player {
 private arraylist<poker> pokers;
 private boolean islandower;
 private arraylist<poker> newpokers;
 private linkedlist<integer> grades;
 
 public linkedlist<integer> getgrades() {
  return grades;
 }
 
 public void setgrades(linkedlist<integer> grades) {
  this.grades = grades;
 }
 
 public arraylist<poker> getnewpokers() {
  return newpokers;
 }
 
 public void setnewpokers(arraylist<poker> newpokers) {
  this.newpokers = newpokers;
 }
 
 public boolean islandower() {
  return islandower;
 }
 
 public void setlandower(boolean landower) {
  islandower = landower;
 }
 
 public player() {
 }
 
 public arraylist<poker> getpokers() {
  return pokers;
 }
 
 public void setpokers(arraylist<poker> pokers) {
  this.pokers = pokers;
 }
}

洗牌:

?
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
/*洗牌*/
 public static linkedlist<poker> getpokerlist() {
  linkedlist<poker> pokers = new linkedlist<poker>();
  linkedlist<poker> newpokers = new linkedlist<poker>();
  arraylist<integer> arraylist = new arraylist<>();
  int[] arr;
 
  /*生成54张扑克牌(poker对象)*/
  /*用嵌套循环生成从方块3到黑桃2的poker对象,放进poker的linkedlist保证顺序*/
  string[] colors = {"♦", "♣", "♥", "♠"};
  string[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "j", "q", "k", "a", "2"};
  for (int i = 0; i < numbers.length; i++) {
   for (int j = 0; j < colors.length; j++) {
    poker p = new poker(colors[j], numbers[i], (4 * i) + j + 1, false);
    pokers.add(p);
   }
  }
  /*添加大小王*/
  pokers.add(new poker("black", "☆", 53, false));
  pokers.add(new poker("red", "☆", 54, false));
 
  /*将生成的54张扑克牌从1到54放进map*/
  for (int z = 1; z <= pokers.size(); z++) {
   map.put(z, pokers.get(z - 1));
  }
  /*遍历map,将每个poker对应的key放进 arraylist<integer> arraylist */
  set<map.entry<integer, poker>> set = map.entryset();
  iterator it = set.iterator();
  while (it.hasnext()) {
   map.entry<integer, poker> e = (map.entry<integer, poker>) it.next();
   arraylist.add(e.getkey());
  }
  /*利用collections.shuffle随机排序key*/
  collections.shuffle(arraylist);
  /*将乱序后的key对于的poker放进newpokers里面 */
  for (integer i : arraylist) {
   newpokers.add(map.get(i));
  }
 
  return newpokers;
 }

发牌:

?
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
/*发牌*/
 /*根据玩家人数生成player,默认num=3*/
 private static void sentpoker(int num) {
  /*获取洗牌后的linkedlist,保证顺序*/
  linkedlist<poker> allpokers = getpokerlist();
  arraylist<poker> pokers = new arraylist<>();
  arraylist<poker> bottompokers = new arraylist<>();
 
  /*生成玩家对象*/
  for (int i = 0; i < num; i++) {
   player p = new player();
   players.add(p);
  }
  /*前50张牌为玩家牌,后三张为底牌*/
  for (int p = 0; p < allpokers.size(); p++) {
   if (p <= 50) {
    pokers.add(allpokers.get(p));
   } else {
    bottompokers.add(allpokers.get(p));
   }
  }
  /*玩家牌中随机一张为地主牌*/
  random rand = new random();
  int s = rand.nextint(51);
  pokers.get(s).setlandowners(true);
  system.out.println("地主牌: " + pokers.get(s));
 
  /*根据num生成player对象*/
  for (int j = 0; j < num; j++) {
   /*玩家的牌值集合*/
   linkedlist<integer> grades = new linkedlist<>();
   /*玩家的手牌集合*/
   arraylist<poker> playerpokers = new arraylist<poker>();
 
   /*根据玩家个数分配玩家牌*/
   for (int z = j; z < pokers.size(); z = z + num) {
    playerpokers.add(pokers.get(z));
    grades.add(pokers.get(z).getgrade());
    players.get(j).setpokers(playerpokers);
    players.get(j).setgrades(grades);
   }
   /*根据玩家牌中的地主牌设置玩家是否地主*/
   for (poker p : players.get(j).getpokers()) {
    if (p.islandowners() == true) {
     players.get(j).setlandower(true);
    }
   }
   /*根据玩家的牌值集合生成数组*/
   integer[] gs = new integer[players.get(j).getgrades().size()];
   grades.toarray(gs);
   /*排序数组,由低到高*/
   arrays.sort(gs);
 
   /*排序后的玩家牌*/
   arraylist<poker> newpokers = new arraylist<>();
   for (int g = 0; g < gs.length; g++) {
    poker p = map.get(gs[g]);
    newpokers.add(p);
   }
   players.get(j).setnewpokers(newpokers);
   system.out.println("玩家" + (j + 1) + ": " + players.get(j).getnewpokers() + " 是否地主:" + players.get(j).islandower());
  }
 
  system.out.print("底牌为: ");
  for (poker p : bottompokers) {
   system.out.print(p + " ");
  }
 }

效果如下:

java实现斗地主游戏

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

原文链接:https://blog.csdn.net/zzm0707/article/details/83540242

延伸 · 阅读

精彩推荐