java模拟斗地主发牌功能

网友投稿 247 2022-11-15


java模拟斗地主发牌功能

本文实例为大家分享了java模拟斗地主发牌的具体代码,供大家参考,具体内容如下

1.案例介绍

规则:

组装54张扑克牌

54张牌顺序打乱

三个玩家参与游戏,三人交替摸牌,每人17张牌,后三张留作底牌

查看三人各自手中的牌(按照牌的大小排序)、底牌

2. 分析

1)、准备牌:

完成数字与纸牌的映射关系:

使用双列Map(HashMap)集合,完成一个数字与字符串纸牌的对应关系(相当于一个字典)。

2)、洗牌:

通过数字完成洗牌发牌

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

存放的过程中要求数字大小与斗地主规则的大小对应。

将代表不同纸牌的数字分配给不同的玩家与底牌。

3)、看牌:

通过Map集合找到对应字符展示。

通过查询纸牌与数字的对应关系,由数字转成纸牌字符串再进行展示。

3.代码

public class Test7 {

public static void main(String[] args) {

//定义一个Map集合和List集合来存取牌号和索引

Map map = new HashMap();

List pokerindex = new ArrayList<>();

//定义牌

String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

String[] color = {"♥", "♠", "♣", "♦"};

//存牌号和与之对应的索引

int index = 0;

for (String s : num) {

for (String c : color) {

map.put(index, c + s);

pokerindex.add(index);

index++;

}

}

//存大小王

map.put(index, "大王");

pokerindex.add(index);

index++;

map.put(index, "小王");

pokerindex.add(index);

//打乱牌组;

Collections.shuffle(pokerindex);

//创建四个集合

List dipai = new ArrayList<>();

List player1 = new ArrayList<>();

List player2 = new ArrayList<>();

List();

//将打乱的索引数组分配给三个人

for (int i = 0; i < pokerindex.size(); i++) {

if (i > 50) {

dipai.add(pokerindex.get(i));

} else if (i % 3 == 0) {

player1.add(pokerindex.get(i));

} else if (i % 3 == 2) {

player2.add(pokerindex.get(i));

} else if (i % 3 == 1) {

player3.add(pokerindex.get(i));

}

}

//给每个人的牌组排序

Collections.sort(player1);

Collections.sort(player2);

Collections.sort(player3);

Collections.sort(dipai);

//显示每个人的牌组

show("张三", map, player1);

show("李四", map, player2);

show("王五", map, player3);

show("底牌", map, dipai);

}

//定义一个方法用来显示牌组

public static void show(String name, Map map, List player) {

System.out.print(name);

for (int i = 0; i < player.size(); i++) {

Integer ii = player.get(i);

System.out.print(map.get(ii) + " ");

}

System.out.println();

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java dbcp连接池基本使用方法详解
下一篇:关于idea引入spring boot <parent></parent>父依赖标红问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~