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

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

服务器之家 - 编程语言 - Java教程 - 详解java_ 集合综合案例:斗地主

详解java_ 集合综合案例:斗地主

2021-07-30 11:09qq_35670694 Java教程

这篇文章主要介绍了java_ 集合综合案例:斗地主,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

案例介绍

按照斗地主的规则,完成洗牌发牌的动作。 具体规则: 使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。

案例分析

1.准备牌:

牌可以设计为一个arraylist,每个字符串为一张牌。 每张牌由花色数字两部分组成,我们可以使用花色 集合与数字集合嵌套迭代完成每张牌的组装。 牌由collections类的shuffle方法进行随机排序。

2.发牌

将每个人以及底牌设计为arraylist,将最后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌。

3.看牌

直接打印每个集合。

代码实现

?
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
77
78
79
80
81
82
import java.util.arraylist;
import java.util.collections;
public class poker {
public static void main(string[] args) {
/*
* 1: 准备牌操作
*/
//1.1 创建牌盒 将来存储牌面的
arraylist<string> pokerbox = new arraylist<string>();
//1.2 创建花色集合
arraylist<string> colors = new arraylist<string>();
//1.3 创建数字集合
arraylist<string> numbers = new arraylist<string>();
//1.4 分别给花色 以及 数字集合添加元素
colors.add("♥");
colors.add("♦");
colors.add("♠");
colors.add("♣");
for(int i = 2;i<=10;i++){
numbers.add(i+"");
}
numbers.add("j");
numbers.add("q");
numbers.add("k");
numbers.add("a");
//1.5 创造牌 拼接牌操作
// 拿出每一个花色 然后跟每一个数字 进行结合 存储到牌盒中
for (string color : colors) {
//color每一个花色 guilian
//遍历数字集合
for(string number : numbers){
//结合
string card = color+number;
//存储到牌盒中
pokerbox.add(card);
}
}
//1.6大王小王
pokerbox.add("小");
pokerbox.add("大");
// system.out.println(pokerbox);
//洗牌 是不是就是将 牌盒中 牌的索引打乱
// collections类 工具类 都是 静态方法
// shuffer方法
/*
* static void shuffle(list<?> list)
* 使用默认随机源对指定列表进行置换。
*/
//2:洗牌
collections.shuffle(pokerbox);
//3 发牌
//3.1 创建 三个 玩家集合 创建一个底牌集合
arraylist<string> player1 = new arraylist<string>();
arraylist<string> player2 = new arraylist<string>();
arraylist<string> player3 = new arraylist<string>();
arraylist<string> dipai = new arraylist<string>();
//遍历 牌盒 必须知道索引
for(int i = 0;i<pokerbox.size();i++){
//获取 牌面
string card = pokerbox.get(i);
//留出三张底牌 存到 底牌集合中
if(i>=51){//存到底牌集合中
dipai.add(card);
} else {
//玩家1 %3 ==0
if(i%3==0){
player1.add(card);
}else if(i%3==1){//玩家2
player2.add(card);
}else{//玩家3
player3.add(card);
}
}
}
//看看
system.out.println("令狐冲:"+player1);
system.out.println("田伯光:"+player2);
system.out.println("绿竹翁:"+player3);
system.out.println("底牌:"+dipai);
}
}
 

以上所述是小编给大家介绍的java_ 集合综合案例:斗地主详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/qq_35670694/article/details/89060478

延伸 · 阅读

精彩推荐