李敏  
          日歷
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789
          統計
          • 隨筆 - 1
          • 文章 - 40
          • 評論 - 4
          • 引用 - 0

          導航

          常用鏈接

          留言簿(1)

          文章分類

          文章檔案

          相冊

          收藏夾

          它山之石

          聚賢莊

          搜索

          •  

          最新評論

           

             題目就不用介紹了,網上都有。(Random我在這里使用的是我自己寫的一個隨機類,如果想要更好的效果可以使用java.util.Random這個類,具體方法一樣)


          標記(修改),當前的錯誤:

          1 一盒撲克牌使用了組成關系,是由許多張撲克牌構成的(部分),同時它也包含【行為】取出所有的撲克牌。----不要盲目考慮static的事情和性能,先把問題描述清楚。

          2 人取來的永遠是一【盒】牌,而不是散裝的牌。切牌和亮牌則一定是散裝的牌。


          項目:
          模擬現實當中的洗牌

          需求:
          1. 一副牌
          2. 玩家進行洗牌


          功能列表:
          洗牌

          用例列表:

          參與者

          用例

          玩家 洗牌

          用例:

          洗牌

          1. 玩家拿到一副牌
          2. 玩家開始洗牌
          3. 玩家把牌拿出來
          場景:
          1、2、3
          Poker
          point:Point(點數)
          color:Color(花色)
          Point
          One,Two 
          Color
          Tao,Xin,Mei,Fang
          PokerBox
          allpokers:List
          changePoker(firstIndex:int,lastIndex:int)
          length():int
          Player
          cutPoker(pokerBox:PokerBox)

           



          public class Test {

              
          /**
               * 業務入口
               
          */

              
          private void service() {
                  PokerBox pokerBox 
          = new PokerBox();

                  Player player 
          = new Player();

                  player.cutPoker(pokerBox);

                  pokerBox.testPrint();
              }


              
          public static void main(String[] args) {
                  
          new Test().service();
              }

          }


          花色:
          /**
           * 撲克牌花色
           * 
           * 
          @author MinLi
           * 
           
          */

          public enum Color {
              Tao, Xin, Mei, Fang
          }


          點數:
          /**
           * 撲克牌點數
           * 
           * 
          @author MinLi
           * 
           
          */

          public enum Point {
              One, Two, Three, Four, Five, Six, Seven
          }


          撲克牌:
          public class Poker {
              
          /** 花色 */
              
          private Color color;

              
          /** 點數 */
              
          private Point point;

              
          /**
               * 
               * 
          @param color顏色
               * 
          @param point點數
               
          */

              
          public Poker(Color color, Point point) {
                  
          this.color = color;

                  
          this.point = point;
              }


              
          public Color getColor() {
                  
          return color;
              }


              
          public void setColor(Color color) {
                  
          this.color = color;
              }


              
          public Point getPoint() {
                  
          return point;
              }


              
          public void setPoint(Point point) {
                  
          this.point = point;
              }


              
          public String toString() {
                  
          return color + " " + point;
              }

          }


          一副撲克牌:
          import java.util.ArrayList;
          import java.util.List;

          /**
           * 一副牌
           * 
           * 
          @author MinLi
           * 
           
          */

          public class PokerBox {
              
          /** 52張牌 */
              
          private List<Poker> allPoker = new ArrayList<Poker>();

              
          /** 每張牌的索引 */
              
          private int[] pokerIndexs;

              
          public PokerBox() {
                  initPoker();

                  initPokerIndex();
              }


              
          /**
               * 初始化所有牌
               
          */

              
          private void initPoker() {
                  
          for (Color color : Color.values()) {

                      
          for (Point point : Point.values()) {
                          allPoker.add(
          new Poker(color, point));
                      }

                  }

              }


              
          /**
               * 初始化所有牌的索引
               
          */

              
          private void initPokerIndex() {
                  
          int total = allPoker.size();

                  pokerIndexs 
          = new int[total];

                  
          int value = 0;

                  
          for (int count = 0; count < total; count++{
                      pokerIndexs[count] 
          = value;

                      value
          ++;
                  }

              }


              
          /**
               * 兩張牌的交互
               
          */

              
          public void changePoker(int firstIndex, int lastIndex) {
                  
          int firstValue = pokerIndexs[firstIndex];

                  
          int lastValue = pokerIndexs[lastIndex];

                  pokerIndexs[firstIndex] 
          = lastValue;
                  pokerIndexs[lastIndex] 
          = firstValue;
              }


              
          /**
               * 取出牌數
               * 
               * 
          @return
               
          */

              
          public int length() {
                  
          return allPoker.size();
              }


              
          /**
               * 打印所有撲克牌
               
          */

              
          public void testPrint() {
                  
          for (int index : pokerIndexs) {
                      Poker poker 
          = allPoker.get(index);

                      Point point 
          = poker.getPoint();

                      Color color 
          = poker.getColor();

                      System.out.println(color 
          + "  " + point);
                  }

              }

          }


          發牌人:
          import java.util.Random;

          /**
           * 玩家
           * 
           * 
          @author MinLi
           * 
           
          */

          public class Player {
              
          /**
               * 切牌
               * 
               * 
          @param pokerBox
               
          */

              
          public void cutPoker(PokerBox pokerBox) {
                  Random rand 
          = new Random();

                  
          // 對所有的撲克牌進行前后調換

                  
          int total = pokerBox.length() - 1;

                  
          int currentTotal = pokerBox.length();

                  
          for (int count = 0; count < total; count++{

                      
          int firstIndex = rand.nextInt(currentTotal);

                      
          int lastIndex = currentTotal - 1;

                      pokerBox.changePoker(firstIndex, lastIndex);

                      currentTotal
          --;
                  }

              }

          }

           

          posted on 2009-03-17 23:33 李敏 閱讀(167) 評論(0)  編輯  收藏 所屬分類: 算法
           
          Copyright © 李敏 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 澎湖县| 石城县| 丹棱县| 南京市| 遂平县| 巴林左旗| 张家川| 驻马店市| 黄龙县| 福清市| 望江县| 衡山县| 溆浦县| 临高县| 布尔津县| 湘乡市| 大姚县| 和硕县| 泾源县| 长子县| 邢台市| 马关县| 海原县| 汉寿县| 金乡县| 洮南市| 西畴县| 固始县| 藁城市| 昆明市| 徐州市| 通渭县| 彭州市| 陈巴尔虎旗| 自治县| 门源| 饶阳县| 抚州市| 武陟县| 姚安县| 三原县|