用Java集合模拟斗地主发牌的例子

之前学集合的时候看到了一个很有意思的例子,于是觉得可以用集合来存储牌型来模拟斗地主的发牌模式,将一副54张的扑克牌随机分发给三个玩家手中,保留三张作为底牌,每个玩家手中有17张牌。

public class Testpoker {
	
	public static void main(String[] args) {
		
		ArrayList<String> color = new ArrayList<String>();	
		
		String[] nums={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
		Collections.addAll(color,"♠","♥","♦","♣" );
		
		HashMap<Integer, String> cards = new HashMap<Integer,String>();
		int index = 1;
		for (String flower : nums) {
			for(String poker : color){
				cards.put(index++, poker+flower);
			}
		}
		cards.put(index++,"小王");
		cards.put(index++,"大王");
		
// 		System.out.println(cards);
// 		System.out.println(cards.size());
		
		ArrayList<Integer> cards_order = new ArrayList<Integer>();
		for(int i=1;i<55;i++){
		cards_order.add(i);
		}
		
		Collections.shuffle(cards_order); 
//  		System.out.println(cards_order);
		
		 ArrayList<Integer> base_key = new ArrayList<Integer>();
		 ArrayList<Integer> player1_key = new ArrayList<Integer>();
		 ArrayList<Integer> player2_key = new ArrayList<Integer>();
		 ArrayList<Integer> player3_key = new ArrayList<Integer>();
		
		for(int i=0;i<54;i++){
			if(i>50){
				base_key.add(cards_order.get(i));
			}
			else{
				if(i%3==0){
					player3_key.add(cards_order.get(i));
				}
				else if(i%2==0){
					player2_key.add(cards_order.get(i));
				}
				else{
					player1_key.add(cards_order.get(i));
				}
			}
		}
//		System.out.println(base_key);
//		System.out.println(player3_key);
//		System.out.println(player2_key);
//		System.out.println(player1_key);
		Collections.sort(player1_key);
		Collections.sort(player2_key);
		Collections.sort(player3_key);

		
		ArrayList<String> base_list = new ArrayList<String>();
		for (Integer i: base_key) {
			base_list.add(cards.get(i));
		}
		ArrayList<String> p1_list = new ArrayList<String>();
		for (Integer i: player1_key) {
			p1_list.add(cards.get(i));
		}
		ArrayList<String> p2_list = new ArrayList<String>();
		for(Integer i: player2_key){
			p2_list.add(cards.get(i));
		}
		ArrayList<String> p3_list = new ArrayList<String>();
		for(Integer i: player3_key){
			p3_list.add(cards.get(i));
		}
		
		
		System.out.println("底牌:"+base_list);
		System.out.println("玩家一手牌:"+p1_list);
		System.out.println("玩家二手牌:"+p2_list);
		System.out.println("玩家三手牌:"+p3_list);
		
		int ra = (int)(Math.random()*10);
 		while(ra==0){
 			ra = (int)(Math.random()*10);
 		}
 		if(ra%3==0){
 			System.out.println("玩家三先抢地主");
 		}
 		else if (ra%2==0){
			System.out.println("玩家二先抢地主");
		}
 		else {
			System.out.println("玩家一先抢地主");
		}
		//System.out.println(ra);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值