您的当前位置:首页正文

java模拟斗地主发牌功能

来源:汇意旅游网
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\ 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 player3 = new ArrayList<>(); //将打乱的索引数组分配给三个⼈

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(\"张三\ show(\"李四\ show(\"王五\ show(\"底牌\

}

//定义⼀个⽅法⽤来显⽰牌组

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(); }}

以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

因篇幅问题不能全部显示,请点此查看更多更全内容