Java模拟斗地主(实现大小排序)

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DouDiZhu {
    public static Integer[] index = new Integer[54];
    private static Map<Integer, String> allCard = new HashMap<Integer, String>();

    public static void main(String[] args) {
        initMap(allCard);//1.) 初始化卡牌
        List<Integer> list = Arrays.asList(initIndex(index));
        Collections.shuffle(list);//2.)洗牌
        //3.)发牌
        List<Integer> player1 = list.subList(0, 17);  
        List<Integer> player2 = list.subList(17, 34);
        List<Integer> player3 = list.subList(34, 51);
        List<Integer> dipai = list.subList(51, 54);
        //4.)按大小排序
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        Collections.sort(dipai);
        System.out.print("玩家1:");
        for (Integer i :player1){
            System.out.print(allCard.get(i));
        }
        System.out.println();
        System.out.print("玩家2:");
        for (Integer i :player2){
            System.out.print(allCard.get(i));
        }
        System.out.println();
        System.out.print("玩家3:");
        for (Integer i :player3){
            System.out.print(allCard.get(i));
        }
        System.out.println();
        System.out.print("底牌   :");
        for (Integer i :dipai){
            System.out.print(allCard.get(i));
        }

        
    }
    // 初始化卡牌数量
    public static Integer[] initIndex(Integer[] index) {
        for (int i = 1; i < 55; i++) {
            index[i - 1] = i;
        }
        return index;
    }
    //将卡牌按大小以及花色(黑红梅方)添加到map
    public static void initMap(Map<Integer, String> map) {
        String[] num = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6",
                "5", "4", "3" };
        String[] color = { "♠","♥","♦","♣"};
        map.put(1, "|BK|");
        map.put(2, "|SK|");
        int k = 3;
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < color.length; j++) {
                map.put(k, color[j] + num[i]);
                k++;
            }
        }
    }
}
运行结果:

 

转载于:https://www.cnblogs.com/Num9527/p/10559557.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值