[C#] if int is equal to multiple variables

Hello, I’m currently working on a poker game in Unity and I’ve starting working on a script to randomly organise the cards into a deck. The first step of this is done by assigning a number from 1 to 52 to each card within a single script. I have a system to assign each number a random value and check if the number is equal to any previous numbers, and if so it will assign another random number until the number is unique. This works, but the code is very untidy.

The code looks something like this, repeated around 52 times:

	if (nineGo == true) {
		if (cardNine == cardOne) {cardNine = Random.Range(startRange, endRange); }
		else if (cardNine == cardTwo) { cardNine = Random.Range(startRange, endRange); }
		else if (cardNine == cardThree) { cardNine = Random.Range(startRange, endRange); }
		else if (cardNine == cardFour) { cardNine = Random.Range(startRange, endRange); }
		else if (cardNine == cardFive) { cardNine = Random.Range(startRange, endRange); }
		else if (cardNine == cardSix) { cardNine = Random.Range(startRange, endRange); }
		else if (cardNine == cardSeven) { cardNine = Random.Range(startRange, endRange); }
		else if (cardNine == cardEight) { cardNine = Random.Range(startRange, endRange); }
		else { tenGo = true; }
	}

I feel that the majority of this could be put into a single if statement, something along the lines of this:

	if (nineGo == true) {
		if (cardNine == cardOne  cardTwo  cardThree  cardFour  cardFive  cardSix  cardSeven  cardEight) { cardNine = Random.Range(startRange, endRange); }
		else { tenGo = true; }
	}

As I don’t know how to do such a thing, this code is obviously wrong, so I was wondering if anyone could show me a way to do this.

Thanks.

That method is extremely inefficient and should not be used; look up arrays and shuffling algorithms instead. Your code will be 100X shorter, and far easier to understand and modify, not to mention saving yourself a lot of time and aggravation.

–Eric

Eric’s right, look into shuffling algorithms. The Fisher Yates shuffle is a personal favorite and simple to implement.

I would do something like this

public int[] deckOfCards = new int[52];

void Awake(){
    for (i = 0; i < 52; i++) deckOfCards[i] = i;
    Shuffle(deckOfCards);
}

public void Shuffle(int[] obj)
{
    for (int i = 0; i < obj.Length; i++)
    {
        int temp = obj[i];
        int obj_index = Random.Range(0, obj.Length);
        obj[i] = obj[obj_index];
        obj[obj_index] = temp;
    }
}

Then you simply deal the cards from 1 to 52

void DealCards(){
    for (i = 0; i < 2) DealThisCard(i);
}

Not the best example, but as i can’t see the rest of your code, i would not know how to implement it. But it should give you the basic idea.

Thanks for the replies. Being quite new to programming I’ve found myself using if statements and integer/boolean variables for pretty much everything, so it’ll be nice to add a bit more variety and efficiency to my code after researching this. Thanks for the example too, EliteMossy, I’ll certainly use it as a learning opportunity.

A switch would neaten things up a little

if (nineGo)
{
    switch (cardNine)
    {
         case cardOne:
         case cardTwo:
         case cardThree:
            cardNine = Random.Range(startRange, endRange);
             break;

          default:
              tenGo = true;
              break;
      }
}

That would mean 10 switches, not very good practice.

Indeed, but it was more aimed at an answer for the general question, “What can I use instead of ‘if’ throughout my code”. To take with a pinch of salt.

1 Like

Interesting.
By the way, is there a shorter way of evaluating whether the same integer has several possible values? I can’t find that anywhere. I mean something instead of this:
if ((camAnim == 1) || (if ((camAnim == 2))
{
something…
}
Thank you.

Something like this.

var set = new HashSet<int>() { 1, 2, 3, 4 };
var valToCheck = 3;
if (set.Contains(valToCheck))
{
  // blah
}

PS. Are you aware this is a nearly 6 year old thread…?

1 Like

That’s quite an old thread you have revived.

Several approaches.

If you want to check if one variable is between two values, if (15 <= x && x <= 32).

If you want to check if one variable matches two or three spread out values, nothing wrong with your if (x == 15 || x == 32) approach.

If you have a small numerical range of possible choices you won’t change often, consider a small array of bools which have been set up before, bool flags[4] = new bool[ ] { true, false, false, true } ... if (flags[×]). A wider range of choices (say, 12 or 32 or 200) would be better served with a non-linear lookup like a dictionary or hashset.

The Unity way for larger or more adjustable design would be to set up an array or list that can be adjusted in the Inspector.

(Shinyclef just posted something that is a mix of the latter two options.)

1 Like