本文实例为大家分享了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();
 }
}

第一次洗牌的结果

第二次洗牌的结果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。