Is there a better way to do this?

I’m sure there is a better way to do it but I’m not sure how to:

   void SaveCards(int[] cards, int player)
    {
        if (dummy == 0)
        {
            if (player == 1)
            {
                player1first4cards = cards;
            }
            else if (player == 2)
            {
                player2first4cards = cards;
            }
            else if (player == 3)
            {
                player3first4cards = cards;
            }
            else if (player == 4)
            {
                player4first4cards = cards;
            }
            dummy++;
        }else if(dummy == 1)
        {
            if (player == 1)
            {
                player1second4cards = cards;
            }
            else if (player == 2)
            {
                player2second4cards = cards;
            }
            else if (player == 3)
            {
                player3second4cards = cards;
            }
            else if (player == 4)
            {
                player4second4cards = cards;
            }
            dummy++;
        }
    }

yes

Ok serious answer:

Have a class for a player (object orientation, man…)

public class Player
{
    int[] firstCards;
    int[] secondCards;

    public void SetFirstCards(int[] firstCards)
    {
        // you would do validation in here (eg. make sure all cards are >= 0)
        this.firstCards = firstCards;
    }

    public void SetSecondCards(int[] secondCards)
    {
        // more validation
        this.secondCards = secondCards;
    }
}

Then you keep an array of players, and set it that way:

void SaveCards(int[] cards, int player)
{
    if(dummy)
    {
        players[player].SaveFirstCards(cards);
    }else
    {
        players[player].SaveSecondCards(cards);
    }
}

By the way, you could just make the “dummy” variable a boolean from as far as I can see, and it’s not really a descriptive name…

1 Like

Okaz great thanks!