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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 林晓泉 中级黑马   /  2012-11-3 02:43  /  1844 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. <p>/*
  2. 需求: 实现扑克牌的洗牌 发牌 效果

  3. 首先定义一个扑克牌的类,定义了扑克牌的花色,点数,能容纳不定牌数的数组,以及查看扑克牌的方法

  4. 然后定义一个牌组类,继承于扑克牌类,用单例设计模式,内存中只能有1副牌,并且初始化为52张4花色13点数的牌组
  5. 这个类有洗牌和发牌的方法

  6. 最后定义一个手牌类,继承于扑克牌类,牌组类对象发牌的时候该类被创建,并得到所发的牌,该类还应该有一个理牌的方法

  7. */

  8. import java.util.*;

  9. class Card  
  10. {
  11.         private String[] point = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
  12.         private String[] suit = {"黑桃","红桃","梅花","方片"};
  13.         int[][] cards; //一维数组中,第一个为花色,第二个为点数,第三个为位置标示符
  14.         public void show()
  15.         {
  16.                 for (int i=0;i<cards.length ;i++ )
  17.                 {
  18.                         System.out.println(suit[cards[i][0]]+"_"+point[cards[i][1]]+"_"+cards[i][2]);
  19.                 }
  20.         }
  21. }

  22. class Deck extends Card     
  23. {
  24.         private static int x=52;
  25.         private Deck()
  26.         {
  27.                 super.cards = new int[x][3];
  28.                 for (int i=0 ; i<4 ;i++ )
  29.                 {
  30.                         for (int j=0; j<13 ; j++)
  31.                         {
  32.                                 super.cards[j+i*13][0] = i;
  33.                                 super.cards[j+i*13][1] = j;
  34.                                 super.cards[j+i*13][2] = j+i*13;
  35.                         }
  36.                 }
  37.         }
  38.         private static Deck d = new Deck();

  39.         public static Deck getDeck() //单例设计模式,内存中只能有一副牌
  40.         {
  41.                 return d;
  42.         }
  43.         public int getNum()  //可以得到牌组剩余的张数
  44.         {
  45.                 return x;
  46.         }
  47.         public void shuffle()  //洗牌
  48.         {
  49.                 x=52;
  50.                 Random ran = new Random();
  51.                 for (int i=0;i<x ;i++ )
  52.                 {
  53.                         cards[i][2] = ran.nextInt() % super.cards.length; //产生一个随机数,并把它赋给扑克牌的位置标识符
  54.                 }
  55.                 sort();  //根据位置标识符从小到大排列牌组
  56.         }

  57.         public Hands[] deal(int pnum,int cnum)  //返回一个手牌类数组,参数pnum是参与人数,cnum是没人发的牌数
  58.         {
  59.                 Hands[] hands;
  60.                 if (x<pnum*cnum)  //如果牌组中剩余的牌不够发则提示并跳出
  61.                 {
  62.                         System.out.println("牌组的牌不够,请重新洗牌");
  63.                         return null;
  64.                 }
  65.                 /*
  66.                 这里我不知道用什么办法能定义带参数的类的数组,只能想到这样写,不知道大家有什么更好的办法?
  67.                 */
  68.                 hands = new Hands[pnum];  //定义手牌类数组
  69.                 for (int i=0;i<pnum ;i++ )
  70.                 {
  71.                         hands[i]=new Hands(cnum);  //初始化这个数组的元素
  72.                 }
  73.                 for (int i=0; i<cnum ; i++ )
  74.                 {
  75.                         for (int j=0;j<pnum ;j++)
  76.                         {
  77.                                 hands[j].cards[i][0]=super.cards[x-(i*cnum+j)-1][0]; //牌从数组的最后开始发,这样下标,0到(x-pnum*cnum),就是剩余的牌
  78.                                 hands[j].cards[i][1]=super.cards[x-(i*cnum+j)-1][1];
  79.                                 hands[j].cards[i][2]=super.cards[x-(i*cnum+j)-1][2];
  80.                         }
  81.                 }
  82.                 x=x-pnum*cnum;
  83.                 return hands;
  84.         }

  85.         private void sort()  //根据位置标识符,重新排序牌组
  86.         {
  87.                 for (int i=0;i<x-1 ;i++ )
  88.                 {
  89.                         int n=i;
  90.                         for (int j=i+1;j<x ;j++ )
  91.                         {
  92.                                 if (super.cards[n][2]>super.cards[j][2])
  93.                                 {
  94.                                         n = j;
  95.                                 }
  96.                         }
  97.                         if (n!=i)
  98.                         {
  99.                                 int[] temp = super.cards[i];
  100.                                 super.cards[i] = super.cards[n];
  101.                                 super.cards[n] = temp;
  102.                         }
  103.                 }
  104.         }
  105. }

  106. class Hands extends Card
  107. {
  108.         int x;
  109.         Hands(int n)
  110.         {
  111.                 x=n;
  112.                 super.cards = new int[n][3];
  113.         }
  114.         public void tidy()
  115.         {
  116.                 for (int i=0;i<x-1 ;i++ )
  117.                 {
  118.                         int n=i;
  119.                         for (int j=i+1;j<x ;j++ )
  120.                         {
  121.                                 if (super.cards[n][1]>super.cards[j][1])//根据牌的点数大小排序
  122.                                 {
  123.                                         n = j;
  124.                                 }
  125.                         }
  126.                         if (n!=i)
  127.                         {
  128.                                 int[] temp = super.cards[i];
  129.                                 super.cards[i] = super.cards[n];
  130.                                 super.cards[n] = temp;
  131.                         }
  132.                 }
  133.         }
  134. }
  135. class  Demo
  136. {
  137.         public static void main(String[] args) //测试方法可以无视
  138.         {
  139.                 Deck d = Deck.getDeck();
  140.                 d.shuffle();
  141.                 d.show();
  142.                 Hands[] h = d.deal(5,6);
  143.                 for (int i=0;i<5 ;i++ )
  144.                 {
  145.                         System.out.println("---------------------------------------------------------------------------------------------");
  146.                         h[i].show();
  147.                         h[i].tidy();
  148.                         System.out.println("---------------------------------------------------------------------------------------------");
  149.                         h[i].show();
  150.                 }
  151.                 d.deal(5,6);
  152.                 System.out.println(d.getNum());
  153.         }
  154. }
  155. </p>
复制代码
在论坛上看到的这个题,用自己的方法写的。
浪费了一个晚上的脑细胞,感觉对面向对象的思想理解还不到位,中间反复修改了好几次,无法很好的将事物的描述转成代码
希望大家对以上的代码给点建议
这个注释大家将就着看

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马