Fix Cell Propagation through intermediate Step

the next targeted state is first propagated to all Cells, then they all adopt their new states at once.
This commit is contained in:
Marty Oehme 2018-07-06 16:46:30 +02:00
parent c2d46c6e38
commit 6b7e8edb28

View file

@ -2,22 +2,26 @@ import 'package:rules_of_living/Rule.dart';
class Cell { class Cell {
bool state; bool state;
bool nextState = false;
List<Rule> surviveRules = new List<Rule>(); List<Rule> surviveRules = new List<Rule>();
List<Rule> birthRules = new List<Rule>(); List<Rule> birthRules = new List<Rule>();
Cell([bool state = false]) : this.state = state; Cell([bool state = false]) : this.state = state;
void advanceState() {
this.state = this.nextState;
this.nextState = false;
}
void update(int neighbors) { void update(int neighbors) {
bool newState = false;
if (state == true) { if (state == true) {
surviveRules.forEach( (Rule rule) { surviveRules.forEach( (Rule rule) {
if(rule.evaluate(neighbors) == true) newState = true; if(rule.evaluate(neighbors) == true) this.nextState = true;
}); });
} else { } else {
birthRules.forEach((Rule rule) { birthRules.forEach((Rule rule) {
if (rule.evaluate(neighbors) == true) newState = true; if (rule.evaluate(neighbors) == true) this.nextState = true;
}); });
} }
this.state = newState;
} }
} }