Java中Map集合模拟斗地主发牌(发完不洗牌)

代码:

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

public class Test2 {
	public static void main(String[] args) {
		//1.创建一副扑克牌,其实就是创建一个集合对象,把扑克牌存储进去
		String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
		String[] color = {"♥","♠","♦","♣"};
		ArrayList<String> poker = new ArrayList<>();
		//拼接花色和数字
		for (String s1 : color) {
			for (String s2 : num) {
				poker.add(s1.concat(s2));
			}
		}
		poker.add("大王");
		poker.add("小王");
		//2.洗牌
		Collections.shuffle(poker);
		//3.发牌
		ArrayList<String> zhangsan = new ArrayList<>();
		ArrayList<String> lisi = new ArrayList<>();
		ArrayList<String> wangwu = new ArrayList<>();
		ArrayList<String> dipai = new ArrayList<>();
		for (int i = 0; i < poker.size(); i++) {
			if (i >= poker.size() - 3) {
				dipai.add(poker.get(i));
			}else if (i % 3 == 0) {
				zhangsan.add(poker.get(i));
			}else if (i % 3 == 1) {
				lisi.add(poker.get(i));
			}else {
				wangwu.add(poker.get(i));
			}
		}
		//4.看牌
		System.out.println(poker);
		System.out.println(zhangsan);
		System.out.println(lisi);
		System.out.println(wangwu);
		System.out.println(dipai);
	}
}

你可能感兴趣的:(Java)