当前位置: 移动技术网 > IT编程>开发语言>Java > 详解java_ 集合综合案例:斗地主

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

案例介绍

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

案例分析

1.准备牌:

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

2.发牌

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

3.看牌

直接打印每个集合。

代码实现

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_ 集合综合案例:斗地主详解整合,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网