java用HashMap和arrayList模拟斗地主实现


 * 思路:
 * 	A:创建一个HashMap集合
 * 	B:创建一个arrayList集合
 * 	C:创建花色数组和点数数组
 * 	D:从0开始往HashMap里面存储编号,并存储对应的牌
 * 		同时往ArrayList里面存储编号即可
 *  E:洗牌(洗编号)
 *  F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合)
 *  G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
 */
public class PokerDemo2 {
	public static void main(String[] args) {
		//创建一个HashMap集合
		HashMap hm = new HashMap();
		
		//创建一个arrayList集合
		ArrayList array = new ArrayList();
		// 定义一个花色数组
		String[] colors = { "♠", "♥", "♣", "♦" };
		// 定义一个点数数组
		String[] numbers = {  "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K","A", "2" };
		
		//从0开始往HashMap里面存储编号,并存储对应的牌同时往ArrayList里面存储编号即可
		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集合)
		TreeSet zhangSan = new TreeSet();
		TreeSet liSi= new TreeSet();
		TreeSet wangWu= new TreeSet();
		TreeSet diPai	  = new TreeSet();
		
		for (int i = 0; i < array.size(); i++) {
			if(i>=array.size()-3){
				diPai.add(array.get(i));
			}else if(i%3==0){
				zhangSan .add(array.get(i));
			}else if(i%3==1){
				liSi.add(array.get(i));
			}else if(i%3==2){
				wangWu.add(array.get(i));
			}
		}
		
		//看牌
		lookPoker("张三", zhangSan, hm);
		lookPoker("李四", liSi, hm);
		lookPoker("王五", wangWu, hm);
		lookPoker("底牌", diPai, hm);
		
	}
	
	//看牌
	public static void lookPoker(String name,TreeSet ts,HashMap hm){
		System.out.print(name+"的牌是:");
		for (Integer key : ts) {
			String value = hm.get(key);
			System.out.print(value + " ");
		}
		System.out.println();
	}
}

你可能感兴趣的:(java用HashMap和arrayList模拟斗地主实现)