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 个回复

倒序浏览
最喜欢这种贴了,尽管你的代码我还是看不太懂~~~
回复 使用道具 举报

回帖奖励 +2

来看看,O(∩_∩)O
回复 使用道具 举报
不明觉厉!
回复 使用道具 举报
运行了一下,貌似是发牌的游戏,回去好好看看。
回复 使用道具 举报
路过  打个酱油!!!
回复 使用道具 举报
我只能说你的照片和昵称为你的代码加分了?要是送技术分,那我旧就是最爱的!
回复 使用道具 举报
真有黑马币吗
回复 使用道具 举报
原来是发牌程序。
回复 使用道具 举报
支持一下 回去研究研究
回复 使用道具 举报
先复制一下再看看
回复 使用道具 举报
有奖励的贴子我最喜欢了
回复 使用道具 举报
楼主真是有心了,感谢技术分享!
回复 使用道具 举报
大爱这种贴!! 新手可以好好看看~学习学习~
回复 使用道具 举报
huangchunwei 来自手机 中级黑马 2015-2-9 23:08:05
15#
楼主棒棒哒!!!但是代码看不懂。。。
回复 使用道具 举报
{:3_53:}感觉很吊的样子
回复 使用道具 举报
感谢分享!!!
回复 使用道具 举报
Aily 中级黑马 2015-2-21 23:21:03
18#
我只会做出来发牌。。
回复 使用道具 举报
好贴  技术的整合    研究研究
回复 使用道具 举报
ZZGFF 中级黑马 2015-2-22 04:09:02
20#
酱油来。
回复 使用道具 举报
1234下一页
您需要登录后才可以回帖 登录 | 加入黑马