斗地主综合案例

斗地主综合案例分析
    1.准备牌
    2.洗牌
    3.发牌
    4.看牌
/*
    斗地主综合案例分析
        1.准备牌
        2.洗牌
        3.发牌
        4.看牌
 */

import java.util.ArrayList;
import java.util.Collections;

public class Test12 {
    public static void main(String[] args) {
        //1.准备牌
        //定义一个存储54张牌的ArrayList集合,泛型使用String
        ArrayList<String> poker = new ArrayList<>();
        //定义两个数组,一个数组存储花色一个数组存储牌的序号
        String[] colors = {"♥", "♠", "♦", "♣"};
        String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        //先把大王小王存储到poker集合中
        poker.add("大王");
        poker.add("小王");
        //循环嵌套两个数组,组装52张牌
        for (String number : numbers) {
            for (String color : colors) {
                //System.out.println(color + number);
                //把组装好的牌存储到poker集合中
                poker.add(color + number);
            }
        }
        /*
            2.洗牌
            使用集合的工具类Collection中的方法
            static void shuffle(List<?> list) 使用默认随机源对指定列表进行置换
         */
        Collections.shuffle(poker);

        /*
            3.发牌
         */
        //定义4个集合,存储玩家的牌和底牌
        ArrayList<String> play01 = new ArrayList<>();
        ArrayList<String> play02 = new ArrayList<>();
        ArrayList<String> play03 = new ArrayList<>();
        ArrayList<String> dipai = new ArrayList<>();

        /*
            遍历poker集合,获取每一张牌
            使用poker集合的索引%3给3个玩家轮流发牌
            剩余3张牌给底牌
                注意:
                 先判断底牌(i>=51)否则牌就发完了

         */
        for (int i = 0; i < 54; i++) {
            if (i >= 51) {
                dipai.add(poker.get(i));
            }else if (i%3==0){
                play01.add(poker.get(i));
            }else if (i%3==1){
                play02.add(poker.get(i));
            }else{
                play03.add(poker.get(i));
            }
        }

        /*
            4.看牌
         */
        System.out.println(play01);
        System.out.println(play02);
        System.out.println(play03);
        System.out.println(dipai);

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值