欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-14 10:39:54
import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class DouDiZhu { ... ......
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++;
            }
        }
    }
}
运行结果:
Java模拟斗地主(实现大小排序)