Java模拟斗地主洗牌发牌

方法一

package mv;

import java.util.*;

public class pk {
public static void main(String[] args){
List puke=new ArrayList<>();
String[] nums={“A”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“J”,“Q”,“K”};
String[] colors={“♥”,“♠”,“♣”,“♦”};

    for(String  color:colors){
        for(String num:nums){
            String pai=color+num;
            puke.add(pai);
        }
    }
    puke.add("大王");
    puke.add("小王");
    System.out.println(puke);
    //洗牌
    Collections.shuffle(puke);
    System.out.println(puke);
    //留3张牌
    List dipai=new ArrayList<>();
    Random random=new Random();
    for (int i=0;i<3;i++){
        int index=random.nextInt(puke.size());
        String pai=puke.remove(index);
        dipai.add(pai);
    }
    System.out.println(dipai);
    System.out.println(puke);
    //发牌
    List wwc=new ArrayList<>();
    List hll=new ArrayList<>();
    List tkh=new ArrayList<>();
    for(int i=0;i

}

方法二

package mv;

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

public class puke {

public static void main(String[] args) {
    HashMap pokers = new HashMap();
    String[] colors = {"♥","♠","♣","♦"};
    String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

    int count = 0;
    ArrayList indexs = new ArrayList();

    for (int i = 0; i < numbers.length; i++) {
        for (int j = 0; j < colors .length; j++) {
            pokers.put(count, colors[j]+numbers[i]);
            indexs.add(count);
            count++;
        }
    }
    indexs.add(count);
    pokers.put(count, "小王");
    count++;
    indexs.add(count);
    pokers.put(count, "大王");

    Collections.shuffle(indexs);

    TreeSet zhangsan = new TreeSet();
    TreeSet lisi = new TreeSet();
    TreeSet wanger = new TreeSet();
    TreeSet dipai = new TreeSet();
    for (int i = 0; i < indexs.size(); i++) {
        if(i>=indexs.size()-3){
            dipai.add(indexs.get(i));
        }else if(i%3==0){
            zhangsan.add(indexs.get(i));
        }else if(i%3==1){
            lisi.add(indexs.get(i));
        }else if(i%3==2){
            wanger.add(indexs.get(i));
        }
    }
    System.out.println(zhangsan);

    lookpokers("玩家1",zhangsan,pokers);
    lookpokers("玩家2",lisi,pokers);
    lookpokers("玩家3",wanger,pokers);
    lookpokers("底牌",dipai,pokers);
}

private static void lookpokers(String name, TreeSet ts,
                               HashMap pokers) {
    System.out.print(name+":");
    for (Integer index : ts) {
        System.out.print(pokers.get(index)+" ");
    }
    System.out.println();
}

}

你可能感兴趣的:(Java模拟斗地主洗牌发牌)