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

java模拟斗地主洗牌,发牌

程序员文章站 2022-07-12 09:08:39
...

分析:
①:创建一个牌盒
②: 创建扑克牌
③:装牌
④:洗牌
⑤:发牌
⑥:看牌

//第一步创建牌盒
HashMap<Integer, String> hm = new HashMap<Integer, String>();
ArrayList array = new ArrayList();
//第二步创建扑克牌(定义花色数组)

	String[] colors = { "♠", "♥", "♣", "♦" };

//定义一个点数数组

String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
				"J", "Q", "K" };	

//装牌

int index = 0;

		for (String number : numbers) {
			for (String color : colors) {
				String poker = color.concat(number);
				hm.put(index, poker);
				array.add(index);
				index++;
			}
		}
		hm.put(index, "小王");
		array.add(index);
		index++;
		hm.put(index, "大王");
		array.add(index);

//洗牌

Collections.shuffle(array);

//发牌

TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
		TreeSet<Integer> linQingXia = new TreeSet<Integer>();
		TreeSet<Integer> liuYi = new TreeSet<Integer>();
		TreeSet<Integer> diPai = new TreeSet<Integer>();

		for (int x = 0; x < array.size(); x++) {
			if (x >= array.size() - 3) {
				diPai.add(array.get(x));
			} else if (x % 3 == 0) {
				fengQingYang.add(array.get(x));
			} else if (x % 3 == 1) {
				linQingXia.add(array.get(x));
			} else if (x % 3 == 2) {
				liuYi.add(array.get(x));
			}
		}

//看牌

lookPoker("风清扬", fengQingYang, hm);
		lookPoker("林青霞", linQingXia, hm);
		lookPoker("刘意", liuYi, hm);
		lookPoker("底牌", diPai, hm);

//因为看牌需要多次调用,所以我们来写一个看牌的方法

public static void lookPoker(String name, TreeSet<Integer> ts,
			HashMap<Integer, String> hm) {
		System.out.print(name + "的牌是:");
		for (Integer key : ts) {
			String value = hm.get(key);
			System.out.print(value + " ");
		}
		System.out.println();
	}
相关标签: Java