Java实现斗地主小游戏
作者:huicooling 时间:2021-08-05 19:50:17
本文实例为大家分享了Java实现斗地主小游戏的具体代码,供大家参考,具体内容如下
原理图:
斗地主过程:
* 1、组合牌
* 2、洗牌
* 3、发牌
* 4、看牌
代码实现:
package itcast.demo6;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class DouDiZhu {
public static void main(String[] args) {
//1、组合牌
//创建Map集合,键:编号 值:牌
HashMap<Integer,String> pooker = new HashMap<Integer,String>();
//创建List集合,存储编号
ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
//定义13个点数的数组
String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//定义4个花色组合
String[] colors = {"♠","♣","♥","♦"};
//定义一个整数变量,作为Map的键
int index = 2;
//遍历数组,用花色+点数的组合,存储到Map集合中
for(String number : numbers) {
for(String color : colors) {
pooker.put(index, color + number);
pookerNumber.add(index);
index++;
}
}
//System.out.println(pooker);
//System.out.println(pookerNumber);
//单独存储大王和小王
pooker.put(0, "大王");
pookerNumber.add(0);
pooker.put(1, "小王");
pookerNumber.add(1);
//洗牌,将牌的编号打乱
Collections.shuffle(pookerNumber);
//System.out.println(pookerNumber);
//发牌,将牌编号,发给3个玩家集合+1个底牌集合
ArrayList<Integer> player1 = new ArrayList<Integer>();
ArrayList<Integer> player2 = new ArrayList<Integer>();
ArrayList<Integer> player3 = new ArrayList<Integer>();
ArrayList<Integer> buttom = new ArrayList<Integer>();
//发牌,采用的是集合的索引%3
for(int i = 0; i < pookerNumber.size();i++) {
//现将底牌做好
if(i<3) {
//存到底牌去
buttom.add(pookerNumber.get(i));
//对索引%3判断
}else if(i % 3 == 0) {
//索引上的编号,发给玩家1
player1.add(pookerNumber.get(i));
}else if(i % 3 == 1) {
//发给玩家2
player2.add(pookerNumber.get(i));
}else if(i % 3 == 2) {
//发给玩家3
player3.add(pookerNumber.get(i));
}
}
//对玩家手中的编号进行排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
//看牌,就是将玩家手中的编号,到Map集合中查找,根据键找值
//定义实现方法
look("包身工 ",player1,pooker);
look("清洁工 ",player2,pooker);
look("洗碗工 ",player3,pooker);
look("底牌 ",buttom,pooker);
}
public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker) {
//遍历ArrayList集合,获取元素,作为键,到集合Map中找值
System.out.print(name+" ");
for(Integer key : player) {
String value = pooker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}
运行结果:
来源:https://blog.csdn.net/weixin_40521823/article/details/84144861
标签:java,斗地主
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
WPF中使用WebView2控件的方法及常见问题
2023-10-02 18:23:28
![](https://img.aspxhome.com/file/2023/6/119496_0s.png)
Jackson多态序列化图文详解
2022-01-26 19:46:08
![](https://img.aspxhome.com/file/2023/1/69891_0s.jpg)
C#限速下载网络文件的方法实例
2023-07-01 01:33:15
![](https://img.aspxhome.com/file/2023/3/104323_0s.jpg)
MybatisPlus字段类型转换的实现示例
2022-12-09 22:29:13
![](https://img.aspxhome.com/file/2023/6/78906_0s.jpg)
java中如何判断JSONObject是否存在某个Key
2022-06-10 15:07:24
![](https://img.aspxhome.com/file/2023/8/119148_0s.png)
Java开发环境jdk 1.8安装配置方法(Win7 64位系统/windows server 2008)
2022-05-11 20:00:58
![](https://img.aspxhome.com/file/2023/0/60310_0s.jpg)
Spring Boot+Shiro实现一个Http请求的Basic认证
2022-06-01 22:22:31
![](https://img.aspxhome.com/file/2023/5/61065_0s.png)
Java日常练习题,每天进步一点点(13)
2023-12-25 15:48:06
![](https://img.aspxhome.com/file/2023/7/116117_0s.jpg)
Android ContentProvider实现获取手机联系人功能
2023-08-07 00:52:27
C语言代码实现三子棋小游戏
2023-04-16 02:12:41
![](https://img.aspxhome.com/file/2023/6/112306_0s.jpg)
基于SpringBoot实现图片上传及图片回显
2023-01-17 06:26:15
c#初学简单程序实例代码介绍
2023-11-04 10:53:53
Mybatis Interceptor 拦截器的实现
2022-11-02 05:05:44
如何完成spring的最小化XML配置
2022-02-12 11:33:58
C#实现围棋游戏
2022-04-20 11:08:57
![](https://img.aspxhome.com/file/2023/0/96400_0s.jpg)
Java分页查询--分页显示(实例讲解)
2023-02-05 11:07:15
C#利用DesignSurface如何实现简单的窗体设计器
2023-10-18 18:43:28
![](https://img.aspxhome.com/file/2023/9/126099_0s.png)
C#中使用IFormattable实现自定义格式化字符串输出示例
2023-05-31 23:34:04
![](https://img.aspxhome.com/file/2023/6/104226_0s.png)
java实现顺序结构线性列表的函数代码
2023-01-28 13:49:31
利用java实现邮箱群发功能
2021-07-11 21:55:23