java 模拟斗地主发牌

14 篇文章 0 订阅
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;

public class Main {
	public static void display(String name, TreeSet<Integer> P, HashMap<Integer, String> hm) {
		System.out.print(name + "的牌是:");
		for(Integer i:P) {
			String s = hm.get(i);
			System.out.print(s+"  ");
		}
		System.out.println();
	}

	public static void main(String[] args) {
		HashMap<Integer, String> hm = new HashMap<Integer, String>(); //创建一个54张牌和各种花色对应的牌盒儿
		ArrayList<Integer> L = new ArrayList<Integer>();       //用来存储每张牌的编号
		String[] s = { "♣", "♠", "♥", "♦" };
		String[] Num = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
		int index = 0;
		for (String s1 : Num) {
			for (String s2 : s) {
				hm.put(index, s2.concat(s1));
				L.add(index);
				index++;
			}
		}
		L.add(index);
		hm.put(index, "小王");
		index++;
		L.add(index);
		hm.put(index, "大王");
		Collections.shuffle(L);       //洗牌打乱编号
		TreeSet<Integer> dun = new TreeSet<Integer>();   //三个玩家和底牌的TreeSet集合,自动对拿到的牌的序号进行排序
		TreeSet<Integer> sao = new TreeSet<Integer>();
		TreeSet<Integer> wo = new TreeSet<Integer>();
		TreeSet<Integer> dipai = new TreeSet<Integer>();

		for (int i = 0; i < L.size(); ++i) {
			if (i >= L.size() - 3) {
				dipai.add(L.get(i));
			} else if (i % 3 == 0) {
				dun.add(L.get(i));
			} else if (i % 3 == 1) {
				sao.add(L.get(i));
			} else {
				wo.add(L.get(i));
			}
		}
		display("一号", dun, hm);  //根据每个人起到的牌的序号到牌盒里寻找键值对对应的Value值
		display("二号", sao, hm);
		display("三号", wo, hm);
		display("底牌", dipai, hm);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值