Repository: bsatrom/py-conway

Issue:
Detect a still-life or oscillator state and update the Game state accordingly

Discussion:

Patch:
diff --git a/py_conway/game.py b/py_conway/game.py
index 339c693..1b007a3 100644
--- a/py_conway/game.py
+++ b/py_conway/game.py
@@ -7,8 +7,22 @@ InitError exception object.
 from random import randint
 from .helpers import PseudoEnum
 
-
-GameState = PseudoEnum(["READY", "RUNNING", "FINISHED"])
+"""Enum for managing the state of a game.
+
+Uses a custom PseudoEnum class because Enum support is not available
+on CircuitPython/MicroPython.
+
+States:
+    READY: The game is initialized and ready to start.
+    RUNNING: The game is currently running.
+    STASIS: The game has reached a point where the board is empty, or the
+            only remaining cells are either still-life or oscillator objects.
+            A game in stasis can still be run, but this state is indicated
+            as a flag so the game consumer can catch stasis conditions
+            and automatically restart or stop the game.
+    FINISHED: The game has stopped and is ready for a new game to begin.
+"""
+GameState = PseudoEnum(["READY", "RUNNING", "STASIS", "FINISHED"])
 
 
 class InitError(Exception):
@@ -53,6 +67,7 @@ class Game:
         """
         self.board_size = (columns, rows)
         self._enforce_boundary = enforce_boundary
+        self._previous_board = None
 
         if seed is None:
             if columns == 0 or rows == 0:
@@ -211,7 +226,7 @@ class Game:
         Once every cell has been checked against Conway's three rules,
         the entire state grid is updated at once.
         """
-        if (self.state != GameState.RUNNING):
+        if self.state != GameState.RUNNING and self.state != GameState.STASIS:
             return
 
         # Get a deep copy of the state to track cells that will need
@@ -233,6 +248,19 @@ class Game:
                         upcoming_live_cells += 1
                     intermediate_state[row_index][col_index] = 1
 
-        self.current_board = [row[:] for row in intermediate_state]
         self.generations += 1
         self.live_cells = upcoming_live_cells
+
+        # When a board only contains still-live cells, there will
+        # be no change in state
+        if self.current_board == intermediate_state:
+            self.state = GameState.STASIS
+        # Alternatively, when the state is the same every other generation
+        # we have an oscillator on our hands and should also change
+        # the state to STASIS
+        elif self._previous_board == intermediate_state:
+            self.state = GameState.STASIS
+        # Otherwise, the game is still running normally
+        else:
+            self._previous_board = self.current_board
+            self.current_board = [row[:] for row in intermediate_state]


Test patch:
diff --git a/tests/test_conway.py b/tests/test_conway.py
index ed722df..d00645a 100644
--- a/tests/test_conway.py
+++ b/tests/test_conway.py
@@ -282,7 +282,8 @@ def test_threaded_game_still_life_game_will_continue_to_run():
     test_game = ThreadedGame(4, 4, seed)
     test_game.start_thread()
 
-    assert test_game.state == GameState.RUNNING
+    assert test_game.state == GameState.STASIS
+    assert test_game.generations > 1
 
 
 def test_still_life_game_can_be_stopped():
@@ -294,11 +295,11 @@ def test_still_life_game_can_be_stopped():
     test_game = ThreadedGame(4, 4, seed)
     test_game.start_thread()
 
-    assert test_game.state == GameState.RUNNING
+    assert test_game.state == GameState.STASIS
 
     test_game.stop_thread()
 
-    time.sleep(.01)
+    time.sleep(.10)
 
     assert test_game.state == GameState.FINISHED
 
@@ -451,3 +452,35 @@ def test_game_can_be_stopped_while_running():
     my_game.stop()
 
     assert my_game.state == GameState.FINISHED
+
+
+def test_game_still_life_changes_status_to_stasis():
+    seed = [[0, 0, 0, 0],
+            [0, 1, 1, 0],
+            [0, 1, 1, 0],
+            [0, 0, 0, 0]]
+
+    my_game = Game(seed=seed)
+
+    my_game.start()
+    my_game.run_generation()
+
+    assert my_game.state == GameState.STASIS
+
+
+def test_game_oscillator_changes_status_to_stasis():
+    seed = [[0, 0, 1, 0],
+            [0, 0, 1, 0],
+            [0, 0, 1, 0],
+            [0, 0, 0, 0]]
+
+    my_game = Game(seed=seed)
+
+    my_game.start()
+    my_game.run_generation()
+
+    assert my_game.state == GameState.RUNNING
+
+    my_game.run_generation()
+
+    assert my_game.state == GameState.STASIS