一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java如何实现斗地主 java实现斗地主代码示例

时间:2021-02-25 编辑:袖梨 来源:一聚教程网

java如何实现斗地主?本篇文章小编给大家分享一下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 poker= new HashMap<>();
  //创建一个List集合,存储牌的的索引
  ArrayList pokerIndex= new ArrayList<>();

  //定义连个集合 存储牌的花色和牌的序号
  List colors = new ArrayList();
  List numbers = new ArrayList();
  //List colors= Array.asList("♣","♦", "♥", "♠");
  //List numbers= List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
  /**
   * Collections集合的方法
   * public static  boolean addAll(Collection 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 play01 = new ArrayList<>();
  ArrayList play02 = new ArrayList<>();
  ArrayList play03 = new ArrayList<>();
  //底牌集合
  ArrayList diPai = new ArrayList<>();

  /**
   * 遍历存储牌索引的List集合,获取每一个牌的索引
   **/
  for(int i =0;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 poker:存储牌的poker集合
  * ArrayList pokerIndex:存储玩家和底牌的List集合
  *
  * 查表发:
  * 遍历玩家或者底牌集合,获取牌的索引
  * 使用牌的索引,去Map集合中找到对对那个的牌
  **/

 public static void lookPoker(String name,HashMap poker,ArrayList list){
  //输出玩家的名称
  System.out.print(name+": ");
  for(Integer key : list){
   //使用牌的索引,去Map集合中找到对对那个的牌
   String value=poker.get(key);
   System.out.print(value+": ");
  }
  //打印完每一个玩家的牌后,进行换行操作
  System.out.println();
 }
}

第一次洗牌的结果

第二次洗牌的结果

热门栏目