简体   繁体   English

纸牌洗牌

[英]Deck of Cards Shuffle

I'm trying to test out my two functions in my Pack class to simulate a deck of cards. 我正在尝试在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

I want it to look similar to how I've done my PlayingCard class, to keep it more formal. 我希望它看起来类似于我完成我的PlayingCard类的方式,以使其更加正式。

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();
    } 

    }

What I want to do is put the shuffle method into a toString method, like the PlayingCard class so I can then test it in my PackTester. 我想做的就是将shuffle方法放到toString方法中,例如PlayingCard类,这样我就可以在PackTester中对其进行测试。 I understand I can't return a void statement, and that's where I'm stuck. 我知道我无法返回void语句,这就是我遇到的问题。 If someone could explain what to do, I'd really appreciate it. 如果有人可以解释该怎么做,我将非常感激。

Can you guess what I'm doing here? 你能猜出我在做什么吗? :)

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();
    }
}

Note: If you want, you can call the shuffle method here to shuffle up the cards but this is bad design, as every time you want to print the deck, you end up shuffling it. 注意:如果需要,可以在此处调用shuffle方法对卡进行随机化处理,但这是不好的设计,因为每次要打印卡座时,都需要对其进行随机排列。 The toString method should only tell you about the current state, not change the state. toString方法应仅告诉您当前状态,而不更改状态。

Ideally you should do as following: 理想情况下,您应该执行以下操作:

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

Take note that void return type means there's no object returned - the method just does its thing when called. 请注意, void返回类型意味着没有对象返回-该方法在被调用时会执行其操作。

This is the best practice shuffling method around http://datagenetics.com/blog/november42014/index.html 这是围绕http://datagenetics.com/blog/november42014/index.html的最佳做法改组方法

Here's a very rough snippit from my program 这是我程序中非常粗糙的片段

     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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM