Dynamic if-statement?

I am working on a clone of Game of Life (I cannot link a wiki like it). The basic functionality is complete, but I want to give the user the opportunity to also define their own rules. Standard rules of the game:

A cell with 2 or 3 neighbors continues to live.

A cell with 0-1 and 4-8 neighbors dies.

A dead cell with 3 neighbors becomes live.

This is easy to do with 2 if statements. But the user can also define something like:

A cell with 1-3 or 5-7 neighbors continues to live.

A cell with a different number is dying.

A dead cell with 2 or 4 neighbors becomes alive.

How can I implement this in an if-statement? Can I use some list to check?

EDIT: I see some great solutions here. You will need to determine what works best for my code. For those who are still responding: I can handle user input correctly, don't worry about that;)

I will probably have a window where I ask How many neighbors to stay alive? , How many neighbours to become alive when dead? , with some text fields. They will also validate the input.

(This is C # in Unity3D)

+4
source share
7 answers

I would only check the conditions that would make the cell dead. I would structure my function along the line:

 bool CalculateIfDead(Cell c, int[] deadlyCountRules) { foreach(int n in deadlyCountRules) { if(c.NeighbourCount == n) { return true; } } return false; } 
+1
source

I would define an enumeration with the values ​​Propogate, Die, NoChange.

Then create a list / array with an element of this type for each number of possible neighbors (from 0 to 8). The user can then set this list somehow (perhaps a comma-separated list of enumerations in the form of strings, which is split and then pounced on the type of enumeration).

Then all you have to do is count the neighbors and search and act accordingly.

 var UserActionList = "Die, Die, NoChange, NoChange, Propogate, Propogate, Die, Die, Die" .Split(',').Select(s => Enum.Parse(typeof(CellAction), s).ToArray(); // Count neighbours swtich(UserActionList[NeighboursCount]) { case Die: ///// case Propogate: ///// } 
+1
source

Try matix solution.

 bool life[] = new bool[] {false, false, true, true, false, false, false, false, false}; bool die[] = new bool[] {true, true, false, false, true, true, true, true, true}; bool alive[] = new bool[] {false, false, false, true, false, false, false, false, false}; bool shouldLife=live[count]; bool shouldDie=die[count]; bool getAlive=alive[count]; 
+1
source

What you should probably have seems like a class

 class DyingRule { enum RuleResult{ Neutral , Death , Lives}; //evaluates if according to this rule //death occurs.. pass parameter as needed RuleResult Evaluate(); } 

And somewhere in your code you save a vector of them. Users can add a vector and add their own rules.

You rate them as

 foreach ( currentRule in Rules ) { RuleResult r = currentRule.Evaluate(); if ( r == Death ) { ProcessDeath(); break; } else if ( r == Lives ) { ProcessLiving(); break; } } 

Optionally, you can take precedence for rules and sort them according to if the rule replaces another

0
source

Well, the main problem is the sentence "Cell with another number _". There are two options for this.

-Just deny this offer and have the user accurately indicate which numbers do what.

-Set this sentence as a special case last and assign it any current unassigned values.

The way I will implement it is the #neighbours => IAction array (IAction is one of Same, Kill, or Grow). Since you only have a finite number of possible neighbor counts, you can easily populate the map from the data, and then you don’t even need any if statements, just a direct call to IAction in the index if the array with the current number of neighbors is associated with it.

0
source

these are more OO and design issues. Here you need something similar to a strategy template.

http://www.oodesign.com/strategy-pattern.html

0
source

Well, if the user is allowed to define an unlimited number of rules, you may need to translate, and also check user definitions and create a C # code file, including a class, and run it through the CodeDom provider for C #, compile into the assembly, and execute. this file should return a resulkt of all if the statetments that are generated from the user definition.

If the number of options is limited, you can save a file in which you save a vector that contains a binary array of all the combinations that cause the dead.

0
source

Source: https://habr.com/ru/post/1391648/


All Articles