标签:
/*
* 思路:
* A:创建一个HashMap集合
* B:创建一个ArrayList集合
* C:创建花色数组和点数数组
* D:从0开始往HashMap里面存储编号,并存储对应的牌同时往ArrayList里面存储编号即可。
* E:洗牌(洗的是编号)
* F:发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)
* G:看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
*/
1 package com.swust.集合; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Set; 9 import java.util.TreeSet; 10 11 public class Example4 { 12 public static void main(String[] args) { 13 puKe(); 14 } 15 public static void puKe(){ 16 /** 17 * 装牌 18 */ 19 String[] strings = {"♦","♣","♠","♥"}; 20 String[] number ={"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; 21 Map<Integer,String> map = new HashMap<Integer,String>(); 22 List<Integer> newList = new ArrayList<Integer>(); 23 int index = 0; 24 /** 25 * 错误原因:未考虑牌数递增情况,自始至终只有十三张牌 26 */ 27 for(String var : number){ 28 for(String color : strings){ 29 String string = color.concat(var); 30 map.put(index, string); 31 newList.add(index); 32 index++; 33 } 34 } 35 map.put(index, "小王"); 36 newList.add(index); 37 index++; 38 map.put(index, "大王"); 39 newList.add(index); 40 index++; 41 42 /** 43 * 洗牌 44 */ 45 Collections.shuffle(newList); 46 47 /** 48 * 发牌 49 */ 50 Set<Integer> seta = new TreeSet<Integer>(); 51 Set<Integer> setb = new TreeSet<Integer>(); 52 Set<Integer> setc = new TreeSet<Integer>(); 53 Set<Integer> setd = new TreeSet<Integer>(); 54 55 for(int i=0;i<newList.size();i++){ 56 if(i>=newList.size()-3){ 57 setd.add(newList.get(i)); 58 }else if(i%3==0){ 59 seta.add(newList.get(i)); 60 }else if(i%3==1){ 61 setb.add(newList.get(i)); 62 }else if(i%3==2){ 63 setc.add(newList.get(i)); 64 } 65 } 66 67 lookUp(map,setb); 68 } 69 public static void lookUp(Map<Integer,String> map,Set<Integer> set){ 70 for(Integer var : set){ 71 System.out.print(map.get(var)+" "); 72 } 73 } 74 }
标签:
原文地址:http://www.cnblogs.com/sunfie/p/4811454.html