这期内容当中小编将会给大家带来有关使用java实现一个斗地主游戏,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
斗地主案例
按照斗地主的规则,完成洗牌发牌的动作。
具体规则: 使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,后三张留作底牌
具体操作如下
1、准备牌:
完成数字与纸牌的映射关系:
使用双列Map(HashMap)集合,完成一个数字与字符串纸牌的对应关系(相当于一个字典)。
2、洗牌:
通过数字完成洗牌发牌
3、发牌:
将每个人以及底牌设计为ArrayList,将后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌。
存放的过程中要求数字大小与斗地主规则的大小对应。
将代表不同纸牌的数字分配给不同的玩家与底牌。
4、看牌: 通过Map集合找到对应字符展示。
通过查询纸牌与数字的对应关系,由数字转成纸牌字符串再进行展示。
/**
*斗地主案例
* @program: practice_masaike
* @author: csl
* @create: 2021-02-23 16:02
**/
/**
*步骤如下
*1.准备牌
*2.洗牌
*3.发牌
*4.排序
*5.看牌
**/
public class Poker {
public static void main(String[] args) {
//1.准备牌
//创建一个Map集合,存储牌的索引和组装好的牌
HashMap<Integer,String> poker= new HashMap<>();
//创建一个List集合,存储牌的的索引
ArrayList<Integer> pokerIndex= new ArrayList<>();
//定义连个集合 存储牌的花色和牌的序号
List<String> colors = new ArrayList<String>();
List<String> numbers = new ArrayList<String>();
//List<String> colors= Array.asList("♣","♦", "♥", "♠");
//List<String> numbers= List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
/**
* Collections集合的方法
* public static <T> boolean addAll(Collection<T> c, T... elements) `:往集合中添加一些元素。
**/
Collections.addAll(colors,"♣","♦", "♥", "♠");
Collections.addAll(numbers,"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
//把大王和小王存储到集合中
//定义一个牌的索引
int index=0;
poker.put(index,"大王");
pokerIndex.add(index);
index++; //1
poker.put(index,"小王");
pokerIndex.add(index);
index++; //2
//循环嵌套遍历两个集合,组合52张牌,存储到集合中
for(String number : numbers){
for(String color : colors){
//重点注意 Map集合poker的key为index
poker.put(index,color+number);
pokerIndex.add(index);
index++; //3
}
}
// System.out.println(poker);
// System.out.println(pokerIndex);
/**
* 2.洗牌
* 使用Collections中的方法shuffle(List)
**/
Collections.shuffle(pokerIndex);
//System.out.println(pokerIndex);
/**
* 进行发牌
**/
//需要定义四个集合,存储玩家牌的索引和底牌的索引
ArrayList<Integer> play01 = new ArrayList<>();
ArrayList<Integer> play02 = new ArrayList<>();
ArrayList<Integer> play03 = new ArrayList<>();
//底牌集合
ArrayList<Integer> diPai = new ArrayList<>();
/**
* 遍历存储牌索引的List集合,获取每一个牌的索引
**/
for(int i =0;i<pokerIndex.size();i++){
Integer in=pokerIndex.get(i);
//先判断底牌
if (i >= 51) {
//给底牌发牌
diPai.add(in);
}else if(i%3==0){
//给玩家1发牌
play01.add(in);
}else if(i%3==1){
//给玩家1发牌
play02.add(in);
}else if(i%3==2){
//给玩家1发牌
play03.add(in);
}
}
/**
* 4.进行牌的排序
* 使用Collectiond中的方法sort(List) 默认是升序排序
**/
Collections.sort(play01);
Collections.sort(play02);
Collections.sort(play03);
Collections.sort(diPai);
/**
* 5.看牌
* 调用看牌的方法
**/
lookPoker("张三",poker,play01);
lookPoker("李四",poker,play02);
lookPoker("王五",poker,play03);
lookPoker("底牌",poker,diPai);
}
/**
* 定义一个看牌的方法,提高代码的复用性
* 参数
* String name:玩家名称
* HashMap<Integer,String> poker:存储牌的poker集合
* ArrayList<Integer> pokerIndex:存储玩家和底牌的List集合
*
* 查表发:
* 遍历玩家或者底牌集合,获取牌的索引
* 使用牌的索引,去Map集合中找到对对那个的牌
**/
public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
//输出玩家的名称
System.out.print(name+": ");
for(Integer key : list){
//使用牌的索引,去Map集合中找到对对那个的牌
String value=poker.get(key);
System.out.print(value+": ");
}
//打印完每一个玩家的牌后,进行换行操作
System.out.println();
}
}
上述就是小编为大家分享的使用java实现一个斗地主游戏了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。