卡片洗牌

时间:2015-02-23 01:48:31

标签: java shuffle

我正在尝试在我的Pack类中测试我的两个函数来模拟一副牌。

public class Pack {

    private PlayingCard[] deck;   // An array of 52 cards, representing the deck.
    private int cardsUsed; // How many cards have been dealt from the deck.

    /**
    * Creating an unshuffled deck of cards
    */
    public Pack() {
       deck = new PlayingCard[51]; //Creates an array of 52 playing cards
       int cardCt = 0; // How many cards have been created so far.
       for ( int suit = 0; suit <= 3; suit++ ) { //If a suit is complete, move to the next suit
          for ( int value = 1; value <= 14; value++ ) { //Builds a complete suit
             deck[51] = new PlayingCard(value,suit);
             cardCt++; //Adds one to the card count
          }
       }
       cardCt = 0;
    }

    /**
    * Shuffling a deck of cards
    */
    public void shuffle() {
          // Put all the used cards back into the deck, and shuffle it into
          // a random order.
        for ( int i = 51; i > 0; i-- ) { 
            int rand = (int)(Math.random()*(i+1));
            PlayingCard temp = deck[i];
            deck[i] = deck[rand];
            deck[rand] = temp;
        }
        cardsUsed = 0;
    }

    public @Override String toString()
    {
        return Pack();
    }

    } // end class Pack

我希望它看起来与我完成PlayCard课程的方式类似,以使其更加正式。

public class PlayingCard {

    /**
    * Declaring Variables
    */
    private int rank;
    private int suit;

    /**
    * Declaring Non-Numerical Cards
    */
    public static final int jack = 11;
    public static final int queen = 12;
    public static final int king = 13;
    public static final int ace = 14;

    /**
    * Declaring Suits
    */
    public static final int clubs = 0;
    public static final int diamonds = 1;
    public static final int hearts = 2;
    public static final int spades = 3;

    /**
    * Constructs a card with specified initial rank and suit
    */
    public PlayingCard(int rank, int suit)
    {
        this.rank=rank;
        this.suit=suit;
    }

    public int getSuit()
    {
        return suit;
    }

    public int getRank()
    {
        return rank;
    }

    /**
    * Switch Statements are use because there are many execution paths
    */
    public String getSuitAsString()
    {
        switch(suit)
        {
            case clubs: return "Clubs";
            case diamonds: return "Diamonds";
            case hearts: return "Hearts";
            case spades: return "Spades";
            default: return "ERR";
        }
    }

    /**
    * Switch Statements are use because there are many execution paths
    */
    public String getRankAsString()
    {
        switch(rank)
        {
            case 2: return "2";
            case 3: return "3";
            case 4: return "4";
            case 5: return "5";
            case 6: return "6";
            case 7: return "7";
            case 8: return "8";
            case 9: return "9";
            case 10: return "10";
            case 11: return "Jack";
            case 12: return "Queen";
            case 13: return "King";
            case 14: return "Ace";
            default: return "ERR";
        }
    }

    /**
    * toString Method
    */
    public @Override String toString()
    {
        return getRankAsString() + " of " + getSuitAsString();
    } 

    }

我想要做的是将shuffle方法放入toString方法,就像PlayingCard类一样,然后我可以在PackTester中测试它。我知道我不能回复无效声明,那就是我被困住的地方。如果有人能解释该怎么做,我真的很感激。

2 个答案:

答案 0 :(得分:1)

你能猜出我在这里做什么吗? :)

public class Pack {

    // .. other code ..

    public @Override String toString() {

        // see note below
        // shuffle();

        StringBuilder sb = new StringBuilder();
        sb.append("The cards are: ");
        for (int i = 0; i < deck.length; i++) {
            if (i > 0) sb.append(", ");
            sb.append(deck.toString());
        }
        sb.append(".");
        return sb.toString();
    }
}

注意:如果你愿意,你可以在这里调用shuffle方法来洗牌,但这是糟糕的设计,因为每次你想打印套牌时,你都会结束洗牌吧。 toString方法应该只告诉您当前的状态,而不是改变状态。

理想情况下,您应该执行以下操作:

Pack myPack = new Pack();
myPack.shuffle();
System.out.println(myPack); // this automatically calls toString()

请注意void返回类型表示没有返回对象 - 该方法在调用时才会执行其操作。

答案 1 :(得分:1)

这是最好的洗牌方法 http://datagenetics.com/blog/november42014/index.html

这是我的程序中非常粗略的信息

     ArrayList cardsArray = new ArrayList();
      cardsArray.Add(0);
     //generate deck
      for (int i = 1; i <= 52; i++)
      {
          cardsArray.Add(i);
     }

      // shuffle
      object t = 0;
      int s = 0;
      Random r = new Random();
      for(int i=52; i>=1; i=i-1){
      s= (r.Next(i)) +1;
          //swap
          t = cardsArray[s];
          cardsArray[s] = cardsArray[i];
          cardsArray[i] = t;
          }

    cardsArray.RemoveAt(0);