- package it.cast.test01;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.TreeSet;
- /**
- * 分析:
- * a、创建集合作为牌盒(hashMap)
- * b、创建集合集合存储索引(ArrayList)
- * c、创建两个数组 一个是花色,一个是数据
- * d、遍历这两个数组,拼接后存放到牌盒中去;
- * e、 洗牌
- * f、发牌
- * g、看牌
- */
- public class porkerTest
- {
- public static void main(String[] args)
- {
- HashMap<Integer,String> porkerBox = new HashMap<Integer,String>();
-
- int index=0;
-
- ArrayList<Integer> indexs = new ArrayList<Integer>();
-
-
-
- String [] colors ={"♥","♣","♦","♠"};
-
- String[] nums = {"3","4","5","6","7","8","9","10","J","Q","k","A","2"};
- for(String num: nums)
- {
- for(String color: colors)
- {
-
- //拼接两个数组 并添加到牌盒中
- porkerBox.put(index,color.concat(num));
-
- indexs.add(index);
-
- index++;
- }
- }
-
-
- porkerBox.put(index,"大王");
- indexs.add(index);
- index++;
-
- porkerBox.put(index,"小王");
- indexs.add(index);
- index++;
-
- //洗牌
- Collections.shuffle(indexs);
-
- //发牌
-
- TreeSet<Integer> one = new TreeSet<Integer>();
- TreeSet<Integer> two = new TreeSet<Integer>();
- TreeSet<Integer> three = new TreeSet<Integer>();
- TreeSet<Integer> dipai = new TreeSet<Integer>();
-
- for(int x = 0;x<porkerBox.size();x++)
- {
- if(x>=porkerBox.size()-3)
- {
- dipai.add(indexs.get(x));
- }
- else if(x%3==0)
- {
- one.add(indexs.get(x));
- }
- else if(x%3==1)
- {
- two.add(indexs.get(x));
-
- }
- else if(x%3==2)
- {
- three.add(indexs.get(x));
- }
- }
-
- lookporker("张三",one, porkerBox);
- lookporker("李四",two, porkerBox);
- lookporker("王五",three, porkerBox);
- lookporker("底牌",dipai, porkerBox);
-
- }
- //看牌
- public static void lookporker(String name,TreeSet<Integer> i,HashMap<Integer,String> p)
- {
-
- System.out.println(name+"\t");
-
- for(Integer index: i)
- {
- String s = p.get(index);
-
- System.out.print(s+"\t");
- }
-
- System.out.println();
- }
- }
复制代码
C:\Users\Administrator\Desktop\QQ截图20150925110412.png |