简体   繁体   English

Java中纸牌的随机播放方法

[英]Shuffle method for deck of cards in Java

I'm making a PlayingCard class and a Dealer class that shuffles a deck of cards using the Fisher-Yates shuffle. 我正在制作一个PlayingCard类和一个Dealer类,该类使用Fisher-Yates混洗来洗牌。 I have ran into a snag, though, and can't figure out where I went wrong. 但是,我遇到了障碍,无法弄清楚我哪里出了问题。 I get the compiler error "cannot find symbol: variable newDeck" in my Dealer class. 我在Dealer类中收到编译器错误“找不到符号:变量newDeck”。 Here's my PlayingCard class, first: 这是我的PlayingCard类,首先:

    public class PlayingCard
{
  public enum Suit
  {
    Hearts, Clubs, Diamonds, Spades
  }

  public enum Rank
  {
    Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace
  }

  public final Suit suit;
  public final Rank rank;

  public PlayingCard(Rank rank, Suit suit)
  {
    this.rank = rank;
    this.suit = suit;
  }

  public String toString()
  {
    return this.rank.toString() + " of " + this.suit.toString();
  }
}

And here is my Dealer class, where I'm getting the error. 这是我的Dealer类,在这里我得到了错误。 I'm also trying to have my variable i accept number between 1-10, corresponding to how many thousand times the repetition loop will occur, but I don't think I've done that correctly, either. 我也试图让我的变量接受1-10之间的数字,这对应于重复循环将发生多少次,但是我也不认为我做得正确。

import java.util.Random;

public class Dealer
{
  private PlayingCard[] deck;

  public Dealer()
  {
    deck = openNewDeck();
  }

  public PlayingCard[] openNewDeck()
  {
    PlayingCard[] newDeck = new PlayingCard[52];

    int i = 0;

    for (PlayingCard.Suit s : PlayingCard.Suit.values())
    {

      for (PlayingCard.Rank r : PlayingCard.Rank.values())
      {

        newDeck[i] = new PlayingCard(r, s);

        i++;
      }
    }

    return newDeck;

  }

  public void shuffle(int i)
  {
    for (i = 0; i <= 10; i++)
    {
      int j = (int)(Math.random() * newDeck.length);
      int temp = newDeck[i];
      newDeck[i] = newDeck[j];
      newDeck[j] = temp;

      for (String p : newDeck)
      {
        System.out.println(p);
      }
    }
  }

  public String toString()
  {

  }

}

Inside shuffle their is no variable called newDeck . shuffle它们没有名为newDeck变量。 You want to refer to this.deck or just deck to use the field associated with the Dealer instance. 你想参考this.deck或只是deck使用与相关领域的Dealer实例。

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

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