suicuned
suicuned

Reputation: 37

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. 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. Here's my PlayingCard class, first:

    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. 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.

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()
  {

  }

}

Upvotes: 1

Views: 351

Answers (1)

ThisIsNoZaku
ThisIsNoZaku

Reputation: 2443

Inside shuffle their is no variable called newDeck. You want to refer to this.deck or just deck to use the field associated with the Dealer instance.

Upvotes: 4

Related Questions