public static WaitingArea EvolveWaitingAreaUntilSteadyState( WaitingArea initialWaitingArea, EvolutionRules evolutionRules) { WaitingArea previousState = null; var nextState = initialWaitingArea; while (!nextState.Equals(previousState)) { previousState = nextState; nextState = GetNextWaitingAreaState(previousState, evolutionRules); } return(previousState); }
public static WaitingArea GetNextWaitingAreaState( WaitingArea waitingArea, EvolutionRules evolutionRules) { // For each cell: // If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied. // If a seat is occupied(#) and four or more seats adjacent to it are also occupied, the seat becomes empty. // Otherwise, the seat's state does not change. // Floor(.) never changes; seats don't move, and nobody sits on the floor. var updatedGridCellTypes = new Dictionary <GridPoint, CellType>(); for (int row = 0; row < waitingArea.Height; row++) { for (int column = 0; column < waitingArea.Width; column++) { var point = new GridPoint(column, row); var cellType = CellType.Floor; if (waitingArea.GridCellTypes.ContainsKey(point)) { cellType = waitingArea.GridCellTypes[point]; } if (CellType.Floor.Equals(cellType)) { updatedGridCellTypes.Add(point, cellType); continue; } int numberOfAdjacentOccupiedSeats = GetNumberOfSeenOccupiedSeats(waitingArea, point, evolutionRules.OnlyConsiderAdjacentCells); if (CellType.ChairOpen.Equals(cellType) && numberOfAdjacentOccupiedSeats == 0) { cellType = CellType.ChairOccupied; } else if (CellType.ChairOccupied.Equals(cellType) && numberOfAdjacentOccupiedSeats >= evolutionRules.MinimumNumberOfSeenOccupiedSeatsToFlipToEmpty) { cellType = CellType.ChairOpen; } updatedGridCellTypes.Add(point, cellType); } } var result = new WaitingArea(waitingArea.Width, waitingArea.Width, updatedGridCellTypes); return(result); }