Java 实现 斗地主发牌 + 排序

Java 实现 斗地主发牌+ 排序

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

public class Text {
    public static void main(String[] args) {
        ArrayList<String> poker = new ArrayList<>();
        String[] colors = {"♠", "♥", "♣", "♦"};
        String[] numbers = {"A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
        poker.add("大王");
        poker.add("小王");
        for (String color : colors) {
            for (String number : numbers) {
                poker.add(color + number);
            }
        }
        Collections.shuffle(poker);
        ArrayList<String> play01 = new ArrayList<>();
        ArrayList<String> play02 = new ArrayList<>();
        ArrayList<String> play03 = new ArrayList<>();
        ArrayList<String> dipai = new ArrayList<>();
        //发牌
        for (int i = 0; i < poker.size(); i++) {
            String s = poker.get(i);
            if (i >= 51) {
                dipai.add(s);
            } else if (i % 3 == 0) {
                play01.add(s);
            } else if (i % 3 == 1) {
                play02.add(s);
            } else if (i % 3 == 2) {
                play03.add(s);
            }
        }
        System.out.println("周星驰" + play01);
        System.out.println("周杰伦" + play02);
        System.out.println("周润发" + play03);
        System.out.println("底牌" + dipai);
    }
}

排序

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

public class Text {
    public static void main(String[] args) {
        ArrayList<Integer> sor  = new ArrayList<>();
        HashMap<Integer,String> poker = new HashMap<>();
        String[] colors = {"♠", "♥", "♣", "♦"};
        String[] numbers = {"A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
        int index = 0;
        poker.put(index,"大王");
        sor.add(index);
        index++;
        poker.put(index,"小王");
        sor.add(index);
        index++;
        for (String number : numbers) {
            for (String color : colors) {
                poker.put(index,color + number);
                sor.add(index);
                index++;
            }
        }
        System.out.println(poker);
        Collections.shuffle(sor);
        ArrayList<Integer> play01 = new ArrayList<>();
        ArrayList<Integer> play02 = new ArrayList<>();
        ArrayList<Integer> play03 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();
        //发牌
        for (int i = 0; i < poker.size(); i++) {
            Integer s = sor.get(i);
            if (i >= 51) {
                dipai.add(s);
            } else if (i % 3 == 0) {
                play01.add(s);
            } else if (i % 3 == 1) {
                play02.add(s);
            } else if (i % 3 == 2) {
                play03.add(s);
            }
        }
        System.out.println(play01);
        System.out.println(play02);
        System.out.println(play03);
        System.out.println(dipai);
        Collections.sort(play01);
        Collections.sort(play02);
        Collections.sort(play03);
        Collections.sort(dipai);
        System.out.println("=======================");
        System.out.println(play01);
        System.out.println(play02);
        System.out.println(play03);
        System.out.println(dipai);
        look("周星驰",play01,poker);
        look("周杰伦",play02,poker);
        look("周润发",play03,poker);
        look("底牌" ,dipai ,poker);

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值