使用java写斗地主的部分代码

/**
 * 斗地主
 */

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

public class Demo {
    public static void main(String[] args) {
        //定义一个集合
        HashMap<Integer, String> map = new HashMap<>();
        //定义牌的花色
        String[] colors = {"♠", "♥", "♣", "♦"};
        //定义牌的大小
        String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        //定义count记录map键
        int count = 1;
        //利用循环把花色和数字组合起来
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < colors.length; j++) {
                //组合花色和数字
                String s = colors[j] + num[i];
                //把键和键值存入到map中
                map.put(count, s);
                //每存入一个键值对,键加1
                count++;
            }
        }
        //单独添加,大王,小王(54张牌定义完毕)
        map.put(53, "小S");
        map.put(54, "大S");
        //定义一个集合用来存54个数
        ArrayList<Integer> cards = new ArrayList<>();
        for (int i = 1; i < 55; i++) {
            cards.add(i);
        }
        //打乱54个数
        Collections.shuffle(cards);
        //定义三个人和底牌(三张)
        ArrayList<Integer> play1 = new ArrayList<>();
        ArrayList<Integer> play2 = new ArrayList<>();
        ArrayList<Integer> play3 = new ArrayList<>();
        ArrayList<Integer> diPai = new ArrayList<>();
        //遍历cards留三张底牌
        for (int i = 0; i < cards.size() - 3; i++) {
            if (i % 3 == 0) {
                //得到玩家1得牌
                play1.add(cards.get(i));
            } else if (i % 3 == 1) {
                //得到玩家2得牌
                play2.add(cards.get(i));
            } else {
                //得到玩家3得牌
                play3.add(cards.get(i));
            }
        }
        //把三张底牌添加到底牌集合中
        diPai.add(cards.get(53));
        diPai.add(cards.get(52));
        diPai.add(cards.get(51));
        //排序玩家123和底牌
        Collections.sort(play1);
        Collections.sort(play2);
        Collections.sort(play3);
        Collections.sort(diPai);
        //调用方法打印牌
        lookPai(play1, map);
        lookPai(play2, map);
        lookPai(play3, map);
        lookPai(diPai, map);

    }

    //定义一个方法 play1,2,3和底牌都可以调用lookPai打印出来
    public static void lookPai(ArrayList<Integer> cards, HashMap<Integer, String> map) {
        //增强for
        for (Integer card : cards) {
            String s = map.get(card);
            System.out.print(s + " ");
        }
        System.out.println();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值