cellular-automata/lib/src/Cell.dart

37 lines
924 B
Dart

import 'package:rules_of_living/src/Rule.dart';
class Cell {
bool state;
bool nextState = false;
List<Rule> surviveRules = new List<Rule>();
List<Rule> birthRules = new List<Rule>();
/// For determining if render updates are necessary in [Grid].render() function
bool dirty = false;
Cell([bool state = false]) : this.state = state;
// Sets the actual state to what it should be next update
// Returns the newly assumed actual state of the cell.
bool advanceState() {
this.state = this.nextState;
this.nextState = false;
this.dirty = true;
return this.state;
}
void update(int neighbors) {
if (state == true) {
surviveRules.forEach((Rule rule) {
if (rule.evaluate(neighbors) == true) this.nextState = true;
});
} else {
birthRules.forEach((Rule rule) {
if (rule.evaluate(neighbors) == true) this.nextState = true;
});
}
}
}