zoukankan      html  css  js  c++  java
  • 集合应用-斗地主

    /*
     * 思路:
     * 	A:创建一个HashMap集合(编号和对应的牌)
     * 	B:创建一个ArrayList集合(编号)
     * 	C:创建花色数组和点数数组
     * 	D:从0开始往HashMap里面存储编号,并存储对应的牌
     *        同时往ArrayList里面存储编号即可。
     *      E:洗牌(洗的是编号)
     *      F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
     *      G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
     */
    public class PokerDemo {
    	public static void main(String[] args) {
    		// 创建一个HashMap集合
    		HashMap<Integer, String> hm = new HashMap<Integer, String>();
    
    		// 创建一个ArrayList集合
    		ArrayList<Integer> array = new ArrayList<Integer>();
    
    		// 创建花色数组和点数数组
    		// 定义一个花色数组
    		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<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));
    			}
    		}
    
    		// 看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
    		lookPoker("张三", zhangsan, hm);
    		lookPoker("李四", lisi, hm);
    		lookPoker("王五", wangwu, 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();
    	}
    }
    

      

  • 相关阅读:
    setCapture 适用范围
    移动web页面自动探测电话号码
    WEB页面JS实现一键拨号的电话拨打功能
    highcharts动态删除标示区
    【你不知道的JavaScript
    【你不知道的JavaScript
    【JavaScipt高级程序设计 第4版】第5章笔记 日期格式
    【JavaScipt高级程序设计 第4版】第6章笔记 Map Set
    【JavaScipt高级程序设计 第4版】第6章笔记 Array 集合引用类型
    【JavaScipt高级程序设计 第4版】第4章笔记
  • 原文地址:https://www.cnblogs.com/yuefeng123/p/7392671.html
Copyright © 2011-2022 走看看