Repository: bsatrom/py-conway

Issue:
refactor start to start_thread and have start just change state.

Discussion:

Patch:
diff --git a/README.md b/README.md
index 7fa69a3..ddf9d87 100644
--- a/README.md
+++ b/README.md
@@ -34,14 +34,22 @@ If no seed is provided, the game can either generate a seed of zeroes, or a rand
 new_game = Game(12, 12, random=True)
 ```
 
-Once the game board is populated, call the `start()` method. The game will start on a background thread and update the full game board as well as a number of informational instance variables:
+Once the game board is populated, call the `start()` method. The game values and state will be initialized so you can interact with the board one step at a time. You can also use this method to re-initialize the game:
 
 ```python
 new_game.start()
+```
+
+You can also instruct the game to self-run with the `start_thread()` method. The game will start on a background thread and update the full game board as well as a number of informational instance variables:
+
+```python
+new_game.start_thread()
 
 new_game.current_board # hold the complete game state after each step
 new_game.live_cells # the count of live cells on the board
 new_game.generations # the number of steps elapsed in the current game.
+
+new_game.stop_thread()
 ```
 
 It's also possible to call the `run_generation()` method and control the game state yourself from one iteration to the next:
diff --git a/py_conway/game.py b/py_conway/game.py
index a1d5b7f..dc7da7c 100644
--- a/py_conway/game.py
+++ b/py_conway/game.py
@@ -38,6 +38,9 @@ class Game:
     two-dimensional board of any size.
     """
 
+    generations = 0
+    live_cells = 0
+
     def __init__(self, width: int = 6, height: int = 6,
                  seed: list = None, random: bool = False,
                  enforce_boundary: bool = True):
@@ -56,8 +59,6 @@ class Game:
                     side.
         """
         self.board_size = (width, height)
-        self.live_cells = 0
-        self.generations = 0
         self._thread_active = False
         self._enforce_boundary = enforce_boundary
 
@@ -193,17 +194,24 @@ class Game:
                     break
                 self.run_generation()
 
-    def start(self):
+    def start_thread(self):
         """Run the game automatically on a background thread."""
         thread = Thread(target=self._run, args=())
         thread.daemon = True
         thread.start()
         self._thread_active = True
 
-    def stop(self):
+    def stop_thread(self):
         """Stop a game currently running on a background thread."""
         self._thread_active = False
 
+    def start(self):
+        """Initialize important game properties."""
+        self.current_board = deepcopy(self.seed)
+        self.state = GameState.RUNNING
+        self.generations = 0
+        self.live_cells = self._count_live_cells(self.current_board)
+
     def run_generation(self):
         """Run a single generation across all cells.
 
@@ -212,6 +220,9 @@ 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):
+            return
+
         # Get a deep copy of the state to track cells that will need
         # to change without affecting the outcome for other cells
         # in-generation


Test patch:
diff --git a/tests/test_conway.py b/tests/test_conway.py
index da4e9a3..76decfc 100644
--- a/tests/test_conway.py
+++ b/tests/test_conway.py
@@ -82,6 +82,8 @@ def test_3_x_3_single_cell_single_run():
             [0, 0, 0]]
 
     test_game = Game(3, 3, seed)
+    test_game.start()
+
     test_game.run_generation()
     assert test_game.current_board == create_zeros(3, 3)
 
@@ -97,6 +99,8 @@ def test_3_x_3_three_neighbors_single_run():
                       [0, 0, 0]]
 
     test_game = Game(3, 3, seed)
+    test_game.start()
+
     test_game.run_generation()
 
     assert test_game.current_board == expected_state
@@ -113,6 +117,8 @@ def test_3_x_3_four_neighbors_single_run():
                       [1, 1, 1]]
 
     test_game = Game(3, 3, seed)
+    test_game.start()
+
     test_game.run_generation()
 
     assert test_game.current_board == expected_state
@@ -129,6 +135,8 @@ def test_4_x_3_three_neighbors_single_run():
                       [0, 1, 1, 0]]
 
     test_game = Game(4, 3, seed)
+    test_game.start()
+
     test_game.run_generation()
 
     assert test_game.current_board == expected_state
@@ -158,6 +166,8 @@ def test_increment_live_cells_after_update():
             [0, 1, 0]]
 
     test_game = Game(3, 3, seed)
+    test_game.start()
+
     test_game.run_generation()
 
     assert test_game.live_cells == 8
@@ -165,6 +175,7 @@ def test_increment_live_cells_after_update():
 
 def test_update_generations_count_after_each_generation():
     test_game = Game(2, 2, [[1, 0], [0, 1]])
+    test_game.start()
 
     test_game.run_generation()
     test_game.run_generation()
@@ -183,6 +194,8 @@ def test_3_x_3_four_neighbors_two_runs():
                       [1, 0, 1]]
 
     test_game = Game(3, 3, seed)
+    test_game.start()
+
     test_game.run_generation()
     test_game.run_generation()
 
@@ -234,6 +247,8 @@ def test_ensure_that_live_cells_count_is_accurate_before_run():
             [0, 0, 0, 0]]
 
     test_game = Game(4, 4, seed)
+    test_game.start()
+
     test_game.current_board[0][0] = 1
 
     test_game.run_generation()
@@ -243,6 +258,8 @@ def test_ensure_that_live_cells_count_is_accurate_before_run():
 
 def test_no_seed_ensure_live_cells_count_is_accurate_before_run():
     test_game = Game(4, 4)
+    test_game.start()
+
     test_game.current_board[0][0] = 1
     test_game.current_board[0][1] = 1
     test_game.current_board[0][2] = 1
@@ -259,7 +276,7 @@ def test_still_life_game_will_continue_to_run():
             [0, 0, 0, 0]]
 
     test_game = Game(4, 4, seed)
-    test_game.start()
+    test_game.start_thread()
 
     assert test_game.state, GameState.RUNNING
 
@@ -271,11 +288,11 @@ def test_still_life_game_can_be_stopped():
             [0, 0, 0, 0]]
 
     test_game = Game(4, 4, seed)
-    test_game.start()
+    test_game.start_thread()
 
     assert test_game.state, GameState.RUNNING
 
-    test_game.stop()
+    test_game.stop_thread()
 
     assert test_game.state, GameState.FINISHED
 
@@ -325,6 +342,31 @@ def test_enable_boundary_wrapping_on_board():
                       [0, 0, 0, 0],
                       [0, 0, 0, 0]]
 
+    test_game.start()
     test_game.run_generation()
 
     assert test_game.current_board == expected_board
+
+
+def test_dont_run_generation_when_game_not_started():
+    test_game = Game(random=True)
+
+    test_game.run_generation()
+
+    assert test_game.generations == 0
+
+
+def test_start_can_be_used_to_restart_the_game():
+    test_game = Game(random=True)
+
+    random_seed = test_game.current_board
+
+    test_game.start()
+    test_game.run_generation()
+    test_game.run_generation()
+
+    assert test_game.current_board != random_seed
+
+    test_game.start()
+
+    assert test_game.current_board == random_seed