斗地主案例

斗地主案例

按照斗地主的规则,完成洗牌发牌的动作。
具体规则: 使用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){
                //给玩家2发牌
                play02.add(in);
            }else if(i%3==2){
                //给玩家3发牌
                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();
    }
}

第一次洗牌的结果
第一次洗牌的结果
第二次洗牌的结果
第二次洗牌的结果

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值