step method

FutureOr<void> step()

Steps the generation forward by one.

The state of the automaton is updated according to the rules.

Throws UndefinedStateException if no rule matches the current state. Throws LockedStateException if the automaton is locked when called.

Implementation

FutureOr<void> step() async {
  if (_locked) throw LockedStateException();
  final previousStateView = UnmodifiableMapView<Coordinate, Base>(
    Map<Coordinate, Base>.from(_state),
  );

  _locked = true;
  for (final mapEntry in previousStateView.entries) {
    final coordinate = mapEntry.key;
    late UnmodifiableMapView<Coordinate, Base> neighbourhoodView;
    final rule = _rules.firstWhere(
      (rule) {
        final neighbourhood = rule.neighbourhood(
          previousStateView,
          coordinate,
        );
        neighbourhoodView = UnmodifiableMapView(neighbourhood);
        return rule.matches(neighbourhoodView, coordinate);
      },
      orElse: () => throw UndefinedStateException(),
    );
    _state[coordinate] = await rule.apply(neighbourhoodView, coordinate);
  }
  _locked = false;
}