- Home /
an alternative to if with many multiple variable settings
Just something i was trying to think up an alternative, let me explain with some random, probably non-compilable psuedocode:
enum AnimalColor
{
Red,
White,
Blue
}
AnimalColor Pig = AnimalColor.Red; //Set random color
AnimalColor Cow = AnimalColor.White; //Set Random color
int Barn = 0;
string BarnName = "Shed"; //Arbritary name;
void SortBasedOnColors()
{
//depending on colors of BOTH animals sort into different barn
if (Pig == AnimalColor.Red && Cow == AnimalColor.Red){
Barn = 1;
BarnName = "ImWatchingSupergirlWhileCoding";
}
if (Pig == AnimalColor.Red && Cow == AnimalColor.White){
Barn = 2;
BarnName = "NobodyWillEverKnow";
}
if (Pig == AnimalColor.Red && Cow == AnimalColor.Blue){
Barn = 3;
BarnName = "YouCantProveAThing";
}
if (Pig == AnimalColor.White && Cow == AnimalColor.Red){
Barn = 4;
BarnName = "Wednesday";
}
//etc... etc... etc..
}
So as you can see, depending on how many are in your selection base, in this example AnimalColor enum, that is a but load of if/elseif statements.
What would be a better possible alternative, so one wouldn't need to put every possibility in a if?
What would you do if there was one more animal?
The complexity would just increase with each added color or animal leading to a huge set of if's. It would probably just to restriction the whole mess, but I'm curious what others have thought of.
Have fun. =)
Edit: Edited for changes to complexity of the problem, comments on awesome int answer below explain added changes.
Answer by Captain_Pineapple · Sep 21, 2019 at 05:37 PM
Hey there,
how about this:
int count = System.Enum.GetNames(AnimalColor).Length;
Barn = (int)Pig * count + (int)Cow;
you can expand this as far as you want or could even use this in some kind of loop to be independent of fixed amounts of animal types:
AnimalColor[] allAnimals;
int count = System.Enum.GetNames(AnimalColor).Length;
int factor = 1;
int Barn = 0;
for (int i=0;i<allAnimals.Length;i++)
{
Barn += factor * (int)allAnimals[i];
factor *= count;
}
Not tested of cause, but makes sense in my head. Let me know if it worked.
EDIT: As @Bunny83 pointed out correctly as usual, my code had a fault since i added an offset of +1 to each cast animaltype. while it would still work it wouldn't be optimal. I corrected the code as proposed by Bunny.
I see what you did there, increment the barn for each animal, that would work in that situation. But what about if barn isn't a int? but a string, and you want to make it some random thing like if you have a red cow and brown pig you wanted to name the barn Wednesday? In the case of a int or byte you can do marching squares or something like that. And I suppose one could expand that to the infinite. But god what a mess it would be. Like the poor soul that figured out the first marching cubes table that everyone uses forever. Right? Your solution kinda re$$anonymous$$ds me of turning a 2d array into a 1d array. Which may bring something interesting to $$anonymous$$d... possibly a array table the index being an animal, and the value being the answer. $$anonymous$$ight even be visually interesting in a custom inspector...
if Barn is not a int then just use an array of type whateverbarnis (like string or to be generic: object) and use the integer that my codesnippet generates as an index to access the n'th element of the array. It wont get simpler than that.
I think you might be right. Thank you for going through the motions with me. =)
Not really this does not flatten a 2d array into a 1d array. It flattens an n dimensional array into a 1d array. What you just added just makes no sense. You want to specify an arbitrary string with a certain combination but you haven't given any reasonable pattern. So it can not be simplified. Do you even have the slightest idea how the complexity of x^y grows? If you have 4 colors and 4 animals you will have 256 different combinations (==4^4). However if you have 10 colors and 10 animals you will have 10,000,000,000 combinations (10^10). It's completely unclear what you want to do here.
There isn't a pattern. That's kinda my point. The complexity get stupid high stupid quick, so how can i keep it organized in a way better then having a if statement for every combination. If i got a red cow and a purple pig i WANT the answer to be barn named cheese. But what is the best way to keep it all in a organized mess other then a zillion if statements. Does any of that make sense?
Why do you have a "+1" on each animal? That would offset the smallest possible result up the the sum of all factors. For two animals that would be 4 nad for 3 that would be 11. There's a good reason we have everything zero based ^^. If you want to start the Barn number at 1, just offset that by one.
int factor = 1;
int Barn = 1;
for (int i=0;i<allAnimals.Length;i++)
{
Barn += factor * ((int)allAnimals[i]);
factor *= count;
}
yeah you are right, in my head there was something about having a few combinations accumulating on 0 if i don't offset that but yeah, it should be without offset. I even f***ing edited afterwards to add the damn offsets :D
Your answer
![](https://koobas.hobune.stream/wayback/20220612212443im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Object doesn't move 2 Answers
Tips on how to handle multiple if statemens 1 Answer
Alternative to Team Lisence? 0 Answers
Different Explosion for Different Collisions 3 Answers
Problem with Counting Collisions 2 Answers