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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 明羽果果 中级黑马   /  2015-3-20 11:42  /  1055 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package cn.itcast;

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

  6. /*
  7. * 三个人斗地主
  8. * 分析:
  9. * 首先要有牌,牌有花色和面值,另有大小王各一张
  10. * 所以分别用数组定义花色和面值
  11. *
  12. * 有了牌以后要进行储存
  13. *
  14. * 储存以后,打乱牌的顺序,按乱序以后顺序发牌
  15. *
  16. * 分别依次发给三个人,在发到还有三张牌的时候留作底牌
  17. *
  18. * 发牌结束进行看牌
  19. *
  20. * 这个时候发现一个问题,牌的排列很乱,这样不利于我们看
  21. * 于是从更加细致的角度来思考这个问题
  22. *
  23. * 定义牌的大小,每张牌对应一个索引,存入Map集合中
  24. * 在排序和发牌时,只是对索引进行操作
  25. * 在看牌时,显示已按大小顺序排好索引对应的值
  26. *
  27. * 步骤:
  28. * 1.定义HashMap集合,定义ArrayList集合
  29. * 2.定义两个数组,花色和面值
  30. * 3.定义索引,将索引和牌存入Map,索引存入ArrayList
  31. * 4.对ArrayList进行乱序
  32. * 5.按照索引对每一张牌进行发放
  33. * 6.定义功能看牌
  34. *
  35. */
  36. public class PokerDemo2 {

  37.         public static void main(String[] args) {

  38.                 // 定义集合
  39.                 HashMap<Integer, String> hm = new HashMap<Integer, String>();

  40.                 ArrayList<Integer> al = new ArrayList<Integer>();

  41.                 // 定义数组
  42.                 String[] huase = { "♥", "♠", "♧", "♢" };
  43.                 String[] mianzhi = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",
  44.                                 "K", "A", "2" };

  45.                 // 定义索引存入集合
  46.                 int index = 0;

  47.                 // 外循环应该是数值,内循环是花色,这个是牌的大小决定的
  48.                 for (String num : mianzhi) {
  49.                         for (String color : huase) {
  50.                                 hm.put(index, color.concat(num));
  51.                                 al.add(index);
  52.                                 index++;

  53.                         }
  54.                 }
  55.                 // 两张特殊的牌
  56.                 hm.put(index, "小王");
  57.                 al.add(index);
  58.                 index++;
  59.                 hm.put(index, "大王");
  60.                 al.add(index);

  61.                 // 牌已经装好,现在需要打乱顺序,只需要对ArrayList操作
  62.                 Collections.shuffle(al);

  63.                 // 发牌,对每一张牌的索引进行处理,希望可以自动排序,就使用TreeSet进行存储
  64.                 TreeSet<Integer> player1 = new TreeSet<Integer>();
  65.                 TreeSet<Integer> player2 = new TreeSet<Integer>();
  66.                 TreeSet<Integer> player3 = new TreeSet<Integer>();
  67.                 TreeSet<Integer> retain = new TreeSet<Integer>();

  68.                 for (int x = 0; x < al.size(); x++) {

  69.                         // 需要留三张底牌
  70.                         if (x >= al.size()-3) {
  71.                                 retain.add(al.get(x));
  72.                         } else if (x % 3 == 0) {
  73.                                 player1.add(al.get(x));
  74.                         } else if (x % 3 == 1) {
  75.                                 player2.add(al.get(x));
  76.                         } else if (x % 3 == 2) {
  77.                                 player3.add(al.get(x));
  78.                         }
  79.                 }

  80.                 // 发牌结束,想要看看玩家的牌,可以定义功能实现

  81.                 // 想看谁的牌就可以看了
  82.                 lookPocker("玩家1", player1, hm);
  83.                 lookPocker("玩家2", player2, hm);
  84.                 lookPocker("玩家3", player3, hm);
  85.                 lookPocker("留底", retain, hm);

  86.         }

  87.         /*
  88.          * 两个明确:应该是没有具体返回值类型的,因为是看牌,直接输出结果即可。
  89.          * 参数列表的思考:看谁的牌,需要调用TreeSet和HashMap两个集合,因为看牌需要输出牌而不是索引
  90.          */

  91.         public static void lookPocker(String name, TreeSet<Integer> ts,
  92.                         HashMap<Integer, String> hm) {

  93.                 System.out.print(name + "的牌:");

  94.                 for (Integer key : ts) {
  95.                         String value = hm.get(key);
  96.                         System.out.print(value + " ");
  97.                 }
  98.                 System.out.println();
  99.         }
  100. }
复制代码


4 个回复

倒序浏览
你是真不怕出乱码啊!
回复 使用道具 举报

用eclipse写编码表转utf-8就可以了呀
回复 使用道具 举报
明羽果果 发表于 2015-3-20 19:51
用eclipse写编码表转utf-8就可以了呀

哈哈 我知道 我就看那四个花色就是乱码的苗头哈
回复 使用道具 举报
女神从来不加班 发表于 2015-3-20 20:00
哈哈 我知道 我就看那四个花色就是乱码的苗头哈

:lol是的,我用输入法随便弄的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马