实现模拟斗地主

/**
 * 实现模拟斗地主的功能
 * 1.组合牌
 * 2.洗牌
 * 3.发牌
 * 4.看牌
 */
public class DouDiZhu {
    public static void main(String[] args) {
        //1.组合牌
        //创建Map集合,键是编号,值是排
        HashMap<Integer, String> poker = new HashMap<Integer, String>();
        //创建List集合,存储编号
        ArrayList<Integer> pokerNumber = new ArrayList<Integer>();
        //    定义13个点数的数组
        String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
        // 定义4个花色数组
        String[] colors = {"♠", "♥", "♣", "◇"};
        // 定义整数变量 作为键出现
        int index = 2;
        // 遍历数组 花色+点数的组合,存储到Map集合
        for (String number : numbers) {
            for (String color : colors) {
                poker.put(index, color + number);
                pokerNumber.add(index);
                index++;
            }
        }

        //存储大王 小王
        poker.put(0, "大王");
        pokerNumber.add(0);
        poker.put(1, "小王");
        pokerNumber.add(1);


        // 洗牌功能 把牌的编号打乱
        Collections.shuffle(pokerNumber);

        // 发牌功能 将牌编号,发给玩家集合,底牌集合
        ArrayList<Integer> player1 = new ArrayList<Integer>();
        ArrayList<Integer> player2 = new ArrayList<Integer>();
        ArrayList<Integer> player3 = new ArrayList<Integer>();
        ArrayList<Integer> boottm = new ArrayList<Integer>();
        // 发牌采用的是集合索引%3
        for (int i = 0; i < pokerNumber.size(); i++) {
            if (i < 3) {
                boottm.add(pokerNumber.get(i));
            } else if (i % 3 == 0) {
                player1.add(pokerNumber.get(i));
            } else if (i % 3 == 1) {
                player2.add(pokerNumber.get(i));
            } else if (i % 3 == 2) {
                player3.add(pokerNumber.get(i));
            }

        }
        //对玩家手中的牌进行排序
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);

        // 看牌
        look("player1",player1,poker);
        look("player2",player2,poker);
        look("player3",player3,poker);
        look("底牌",boottm,poker);

    }

    public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> poker){
        //遍历ArrayList集合
        for(Integer key:player){
            String value = poker.get(key);
            System.out.print(value+" ");
        }
        System.out.println();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值