- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.TreeSet;
- /*
- * HashMap:牌的编号,及牌
- * ArrayList:牌的编号
- * TreeSet:牌的编号 -- 输出的数据是根据编号从Map集合中找到的对应的值
- */
- 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" };
- // 把牌定义完成
- int index = 0;
- for (String number : numbers) {
- for (String color : colors) {
- hm.put(index, color.concat(number)); // {0,♦3},{1,♣3}
- array.add(index); // 0,1,
- index++; // index=1,index=2
- }
- }
- hm.put(index, "小王");
- array.add(index);
- index++;
- hm.put(index, "大王");
- array.add(index);
- // 洗牌
- Collections.shuffle(array);
- // 发牌
- TreeSet<Integer> liuBei = new TreeSet<Integer>();
- TreeSet<Integer> caoCao = new TreeSet<Integer>();
- TreeSet<Integer> sunQuan = new TreeSet<Integer>();
- ArrayList<Integer> diPai = new ArrayList<Integer>();
- for (int x = 0; x < array.size(); x++) {
- if (x >= array.size() - 3) {
- diPai.add(array.get(x));
- } else if (x % 3 == 0) {
- liuBei.add(array.get(x));
- } else if (x % 3 == 1) {
- caoCao.add(array.get(x));
- } else if (x % 3 == 2) {
- sunQuan.add(array.get(x));
- }
- }
- // 看牌
- System.out.print("刘备的牌:");
- for (Integer i : liuBei) {
- String card = hm.get(i);
- System.out.print(card + " ");
- }
- System.out.println();
- System.out.print("曹操的牌:");
- for (Integer i : caoCao) {
- String card = hm.get(i);
- System.out.print(card + " ");
- }
- System.out.println();
- System.out.print("孙权的牌:");
- for (Integer i : sunQuan) {
- String card = hm.get(i);
- System.out.print(card + " ");
- }
- System.out.println();
- System.out.print("底牌:");
- for (Integer i : diPai) {
- String card = hm.get(i);
- System.out.print(card + " ");
- }
- System.out.println();
- }
- }
复制代码
涉及到了ArrayList、TreeSet还有HashMap集合,挺有意思,可以看一下。。。 |
|