I'm trying to create a card game. I created two classes, player and card. a player can have a hand of cards (which is an array of card objects). a player can also play a card from their hand. This is where my problem starts when I try to get the length of the hand array, it throws an error "An object reference is required for the nonstatic field, method, or property 'Player.hand'"
So what is the best way to go about creating this? I don't want to create an instance of the object because I don't know what the cards will be until the cards are dealt to each player when the game starts. Beginner coder btw so excuse me.
public class Player
{
private int id;
private string name;
private Card[] hand;
public Player(int playerId, string playerName, Card[] handOfCards)
{
id = playerId;
name = playerName;
hand = handOfCards;
}
static void playCard(Card aCard)
{
for (int i = 0; i < hand.Length; i++) //error CS0120
{
}
}
}
public class Card
{
private int id;
private string name;
private int value;
public Card()
{
}
public Card(int cardId, string cardName, int cardValue)
{
id = cardId;
name = cardName;
value = cardValue;
}