-2

I'm making a Player class that has a hand, and I'm trying to figure out how to add cards to my hand. I have a PlayingCard class and Dealer class, and I know that my adding cards method should be similar to the deal method in my Dealer class, but I'm stuck.

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

Dealer class:

import java.util.Random;

public class Dealer
{
  private PlayingCard[] deck;

  private int nextCard;

  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++;
      }
    }

    this.nextCard = 0;

    return newDeck;

  }

  public void shuffle(int i)
  {
    int j = i * 1000;

    for (j = 0; j <= 10000; j++)
    {

    for (i = 0; i <= 10; i++)
    {
      int k = (int)(Math.random() * deck.length);
      PlayingCard temp = deck[i];
      deck[i] = deck[k];
      deck[k] = temp;
    }
    }
  }

  public PlayingCard deal()
  {
    if (nextCard < deck.length)
    {
      return deck[nextCard++];
    }
    else
    { 
      System.out.println("No cards left!");
      return null;
    }
  }

  public String toString()
  {
    String c = "";
    for (int i = 0; i < 52; i++)
    {
      c += deck[i];
      if ((i+1)%1 == 0 || i == 51)
        c += "\n";
    }
    return c;
    }
  }

And where I'm stuck at the Player class:

public class Player
{
  private PlayingCard[] hand;

  public final String name;

  public int nextCard;

  public Player(String name)
  {
    this.name = name;

    this.nextCard = 0;
  }

  public void receive(PlayingCard card)
  {
    if (nextCard < hand.length)
    {
      return hand[nextCard++];
    }
    else
    {
      System.out.println("Cannot add any more cards!");
    }
  }
}
suicuned
  • 37
  • 5
  • `c += deck[i];` don't do this, Use a StringBuilder – SamHoque Dec 10 '18 at 22:38
  • Hi there, welcome to Stack Overflow :) Can you please explain what exactly you're stuck on? I.e. 1.) what should your code be doing, 2.) what it's doing instead and 3.) anything you've tried to fix it :) – MyStackRunnethOver Dec 10 '18 at 22:39
  • @SamzSakerz while your comment may be correct and even potentially useful to the OP, it's not related to the question so it would be more helpful if you explained the reason **why** or provided [a link to relevant info about `String` and `StringBuilder`](https://stackoverflow.com/questions/5234147/why-stringbuilder-when-there-is-string) :) – MyStackRunnethOver Dec 10 '18 at 22:41
  • @MyStackRunnethOver I understand that, Thats why I have provided a comment instead of an answer :) – SamHoque Dec 10 '18 at 22:42

1 Answers1

2

In your receive method you are not adding anything to the hand Array, you are just trying to return an element from hand. (Which is not valid as it's a void method) Instead set the PlayingCard object you pass in to the index:

public void receive(PlayingCard card)
{
    if (nextCard < hand.length)
    {
        hand[nextCard++] = card;
    }
    else
    {
        System.out.println("Cannot add any more cards!");
    }
}
GBlodgett
  • 12,704
  • 4
  • 31
  • 45