A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

  1. import java.util.HashMap;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.TreeSet;

  5. /*
  6. * 需求:模拟斗地主游戏
  7. *
  8. * 思路:
  9. *         用 HashMap 来存储牌:定义一个对应的索引,以键为索引,值为牌的对应关系存入 hashMap 集合
  10. *  用 ArrayList 来存储索引,看牌的时候,通过索引来拿对应的牌。同时,发牌为了保证牌的顺序,所以用 TreeSet 来接收牌。
  11. */
  12. public class PokerDemo {
  13.         public static void main(String[] args) {

  14.                 // 分析:

  15.                 // 1、创建一副牌(集合)
  16.                 // 创建 HashMap 集合,用于存储索引和牌
  17.                 HashMap<Integer, String> hm = new HashMap<Integer, String>();
  18.                 // 创建 ArrayList 存储索引
  19.                 ArrayList<Integer> array = new ArrayList<Integer>();

  20.                 // 创建牌的花色数组
  21.                 String[] colors = { "♠", "♦", "♥", "♣" };
  22.                 // 创建牌的数字数组
  23.                 String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
  24.                                 "K", "A", "2" };

  25.                 // 定义一个索引
  26.                 int index = 0;
  27.                 // 把牌的颜色和数字组合在一块儿
  28.                 // 通过双重 for 循环,得到每一个牌的颜色和数字
  29.                 for (String number : numbers) {
  30.                         for (String color : colors) {
  31.                                 // 获取到每一张牌
  32.                                 String poker = color.concat(number);
  33.                                 // 把牌和索引添加进 HashMap 集合
  34.                                 hm.put(index, poker);
  35.                                 // 把索引添加进 ArrayList 集合
  36.                                 array.add(index);
  37.                                 // 索引自增
  38.                                 index++;
  39.                         }
  40.                 }
  41.                 // 创建大小王
  42.                 hm.put(index, "小王");
  43.                 array.add(index);
  44.                 index++;
  45.                 hm.put(index, "大王");
  46.                 array.add(index);

  47.                 // 2、洗牌
  48.                 // 调用 Collections 的 shuffle 方法:随机置换功能
  49.                 Collections.shuffle(array);

  50.                 // 3、发牌
  51.                 // 创建玩家和底牌 TreeMap 集合
  52.                 TreeSet<Integer> wanPing = new TreeSet<Integer>();
  53.                 TreeSet<Integer> maoXuePing = new TreeSet<Integer>();
  54.                 TreeSet<Integer> yangXiao = new TreeSet<Integer>();
  55.                 TreeSet<Integer> diPai = new TreeSet<Integer>();

  56.                 // 用取模的方式,进行发牌
  57.                 for (int x = 0; x < array.size(); x++) {
  58.                         // 剩余的3张底牌
  59.                         if (x >= array.size() - 3) {
  60.                                 diPai.add(array.get(x));
  61.                         } else if (x % 3 == 1) {
  62.                                 wanPing.add(array.get(x));
  63.                         } else if (x % 3 == 2) {
  64.                                 maoXuePing.add(array.get(x));
  65.                         } else if (x % 3 == 0) {
  66.                                 yangXiao.add(array.get(x));
  67.                         }
  68.                 }

  69.                 // 4、看牌
  70.                 // 调用看牌功能即可
  71.                 lookPoker("万平", wanPing, hm);
  72.                 lookPoker("毛雪平", maoXuePing, hm);
  73.                 lookPoker("杨晓", yangXiao, hm);
  74.                 lookPoker("底牌", diPai, hm);
  75.         }

  76.         // 由于有三个人看牌,所以定义看牌功能
  77.         public static void lookPoker(String name, TreeSet<Integer> ts,
  78.                         HashMap<Integer, String> hm) {
  79.                 System.out.println(name + "的牌是:");
  80.                 // 遍历 TreeSet 集合,获得索引
  81.                 for (Integer key : ts) {
  82.                         // 通过索引获取值
  83.                         String value = hm.get(key);
  84.                         System.out.print(value + " ");
  85.                 }
  86.                 System.out.println();
  87.         }
  88. }
复制代码


64 个回复

正序浏览
好厉害的样子
回复 使用道具 举报
这个都敲了好几遍了
回复 使用道具 举报
膜拜大神,菜鸟过来学习学习希望能赐点黑马币啦
回复 使用道具 举报
花色是怎么选的?特殊符号?
回复 使用道具 举报
感谢版主,马上要点招了,黑马币能抛抛吗,顶贴了
回复 使用道具 举报
好,顶,赞,,
回复 使用道具 举报
好像很牛的样子啊
回复 使用道具 举报
呵呵,原来斗地主也不过如此!
回复 使用道具 举报
是不是真的有黑马币?
回复 使用道具 举报
少侠好身手!
回复 使用道具 举报
赞一个!
回复 使用道具 举报
支持!!!!!!
回复 使用道具 举报
谢谢版主,非常好的技术贴
回复 使用道具 举报
新人报道!(我是为黑马币来的)
回复 使用道具 举报
哥们  可以  加油 加油
回复 使用道具 举报
这一看就是大神才能写出来的东西,让我等膜拜
回复 使用道具 举报
我是才上了五天课的小白 这个帖子自己发现好多自己没有学到过的东西,不过我还是看完了 ! 其实也有很多是我们已经接触过的了! 希望等我学完了之后弄个更好的出来 ~哈哈! 不过在那之前还是先求大神的 黑马币吧!
回复 使用道具 举报
写的不错 这个代码很有用 赞一个
回复 使用道具 举报
来看看,O(∩_∩)O
回复 使用道具 举报
1234下一页
您需要登录后才可以回帖 登录 | 加入黑马