public AsyncGameOfLifeState Apply(AsyncGameOfLifeState currentState, AsyncGameOfLifeState[] neigboursStates) { byte neighboursAlive = 0; foreach (var state in neigboursStates) { if (state.alive) { neighboursAlive++; } } if (IsReady(currentState.tense, neigboursStates)) { if (currentState.tense == AsyncGameOfLifeState.Tense.Present) { var syncGameOfLifeStates = ToSyncNeighbours(neigboursStates); var syncGameOfLifeState = new SyncGameOfLifeState() { alive = currentState.alive }; var newSyncState = base.Apply(syncGameOfLifeState, syncGameOfLifeStates); var newAsyncState = new AsyncGameOfLifeState() { alive = newSyncState.alive, previousAlive = currentState.alive, tense = AsyncGameOfLifeState.Tense.Future }; return(newAsyncState); } else { var nextTense = (AsyncGameOfLifeState.Tense)((((int)currentState.tense) + 1) % 3); return(new AsyncGameOfLifeState() { alive = currentState.alive, previousAlive = currentState.previousAlive, tense = nextTense }); } } else { return(currentState); } }
SyncGameOfLifeState[] ToSyncNeighbours(AsyncGameOfLifeState[] neigboursStates) { SyncGameOfLifeState[] syncGameOfLifeStates = new SyncGameOfLifeState[neigboursStates.Length]; for (int i = 0; i < neigboursStates.Length; ++i) { if (neigboursStates[i].tense == AsyncGameOfLifeState.Tense.Present) { syncGameOfLifeStates[i] = new SyncGameOfLifeState() { alive = neigboursStates[i].alive }; } else if (neigboursStates[i].tense == AsyncGameOfLifeState.Tense.Future) { syncGameOfLifeStates[i] = new SyncGameOfLifeState() { alive = neigboursStates[i].previousAlive }; } } return(syncGameOfLifeStates); }