模拟斗地主洗牌发牌并且是有序的

package CSDN_Test;

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

/*
 * 模拟斗地主洗牌和发牌(保证牌是有序的!)
 * 
 * 分析:	
 * 		1)创建一个牌盒 
 * 			HashMap<Integer,String>
 * 			键:存储的是牌的编号(从0开始编号----53)
 * 			值:牌  有花色和点数拼接
 * 
 * 			ArrayList<Integer>:存储牌的编号
 * 
 * 		2)装牌
 * 			定义花色数组和点数数组 
 * 			点数数组: 3,4....
 * 		
 * 			定义一个索引遍历index从0开始
 * 			遍历花色和点数,拼接起来
 * 			将编号以及牌添加到HashMap<Integer,String>
 * 			将编号添加到ArrayList<Integer>
 * 
 * 			添加小王,大王到HashMap<Integer,String> ArrayList
 * 			
 * 		3)洗牌
 * 			洗的是编号:使用Collections的随机置换的功能(编号打乱了)
 * 
 * 		4)发牌:
 * 		为了保证每一个人的牌是有序的,使用TreeSet<Integer>		
 * 		遍历ArrayList<Integer> 
 * 			规律:
 * 				如果当前索引值大于等于集合长度-3-->底牌
 * 				 如果当前值 % 3 == 0 --->第一个人
 * 					 当前值 % 3 == 1 --->第二个人
 * 					当前值% 3 == 2 ---->第三个人 
 * 		5)看牌:
 * 		将看牌封装一个功能
 * 		需要遍历每一个人的集合,
 * 				然后使用编号在HashMap<Integer,String>集合中找对应的牌即可!
 * 
 * */
public class ListDemo2 {
	
	public static void main(String[] args) {
		/*
		 * 1)创建一个牌盒 
		 * 			HashMap<Integer,String>
		 * 			键:存储的是牌的编号(从0开始编号----53)
		 * 			值:牌  有花色和点数拼接
		 * 
		 * 			ArrayList<Integer>:存储牌的编号
		 * */
		HashMap<Integer, String> hm = new HashMap<Integer,String>() ;
		ArrayList<Integer> array = new ArrayList<Integer>() ;
		/*
		 * 装牌
		 * 			定义花色数组和点数数组 
		 * 			点数数组: 3,4....
		 * 		
		 * 			定义一个索引遍历index从0开始
		 * 			遍历花色和点数,拼接起来
		 * 			将编号以及牌添加到HashMap<Integer,String>
		 * 			将编号添加到ArrayList<Integer>
		 * 
		 * 			添加小王,大王到HashMap<Integer,String> ArrayList
		 * */
		//定义花色数组
		String[] colors = {"♥","♠","♣","♦"} ;
		//定义点数数组
		String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"} ;
		
		//定义存储编号的变量
		int index = 0 ;
		//遍历
		for(String number:numbers) {
			for(String color:colors) {
				//拼接
				String poker = color.concat(number) ;
				//添加到HasMap集合中
				hm.put(index, poker) ;
				//给ArrayList集合中添加编号
				array.add(index) ;
				index ++ ;
			}
		}
		//添加大王和小王
		hm.put(index, "小王") ;
		array.add(index) ;
		index ++ ;
		hm.put(index, "大王") ;
		array.add(index) ;
		
		//测试:输出:array
		//System.out.println(array);
		//System.out.println("-------------------");
		/*
		 * 洗牌
		 * 			洗的是编号:使用Collections的随机置换的功能(编号打乱了)
		 * */
		Collections.shuffle(array);
		//System.out.println(array);
		
		/*
		 * 4)发牌:
		 * 		为了保证每一个人的牌是有序的,使用TreeSet<Integer>		
		 * 		遍历ArrayList<Integer> 
		 * 			规律:
		 * 				如果当前索引值大于等于集合长度-3-->底牌
		 * 				 如果当前值 % 3 == 0 --->第一个人
		 * 					 当前值 % 3 == 1 --->第二个人
		 * 					当前值% 3 == 2 ---->第三个人 
		 * */
		TreeSet<Integer> zhangsan = new TreeSet<Integer>() ;
		TreeSet<Integer> lisi = new TreeSet<Integer>() ;
		TreeSet<Integer> zhaowu	= 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 ) {
				//第一个人的
				zhangsan.add(array.get(x)) ;
			}else if(x % 3 == 1) {
				lisi.add(array.get(x)) ;
			}else if(x % 3 == 2) {
				zhaowu.add(array.get(x)) ;
			}
		}
		
		
		/*
		 * 5)看牌:
		 * 		将看牌封装一个功能
		 * 		需要遍历每一个人的集合,
		 * 				然后使用编号在HashMap<Integer,String>集合中找对应的牌即可!
		 * */
		lookPoker("张三", zhangsan, hm);
		lookPoker("李四", lisi, hm);
		lookPoker("赵五", zhaowu, hm);
		lookPoker("底牌", diPai, hm);
		
		
		
	}
	public static void lookPoker(String name,TreeSet<Integer> ts,
			HashMap<Integer, String> hm) {
		
		System.out.print(name+"的牌是: ");
		//增强for遍历
		for(Integer key : ts) {
			String s = hm.get(key) ;
			System.out.print(s+" ");
		}
		System.out.println();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值