Repository: bsatrom/py-conway

Issue:
Add the ability to generate a random beacon

Discussion:

Patch:
diff --git a/README.md b/README.md
index 83fa945..add6cf1 100644
--- a/README.md
+++ b/README.md
@@ -28,6 +28,12 @@ seed = [[0, 1, 0],
 new_game = Game(3, 3, seed)
 ```
 
+If no seed is provided, the game can either generate a seed of zeroes, or a random starter seed. use the `random=True` positional argument to generate a random seed.
+
+```python
+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:
 
 ```python
diff --git a/conway/game.py b/conway/game.py
index 6d2b56e..996c75e 100644
--- a/conway/game.py
+++ b/conway/game.py
@@ -1,6 +1,7 @@
 from copy import deepcopy
 from enum import Enum
 from threading import Thread
+from random import randint
 
 
 class InitError(Exception):
@@ -20,7 +21,8 @@ class Game:
     Class for running a game of Conway's Game of Life on a virtual
     two-dimensional board of any size.
     """
-    def __init__(self, width: int = 6, height: int = 6, seed: list = None):
+    def __init__(self, width: int = 6, height: int = 6,
+                 seed: list = None, random: bool = False):
         """
         Intialize the game based on provided board size values and a seed.
 
@@ -28,7 +30,9 @@ class Game:
         width -- the width (in columns) of the game board
         height -- the height (in rows) of the game board
         seed -- A two-dimensional list with 1 and 0 values that
-                  should be set to the initial game state.
+                should be set to the initial game state.
+        random -- Boolean indicating whether a random seed should
+                  be created. Ignored if a seed is provided.
         """
         self.board_size = (width, height)
         self.live_cells = 0
@@ -36,7 +40,11 @@ class Game:
         self._thread_active = False
 
         if seed is None:
-            self.seed = self._create_zeros()
+            if random:
+                self.seed = self._create_random_seed()
+                self.live_cells = self._count_live_cells(self.seed)
+            else:
+                self.seed = self._create_zeros()
         else:
             if len(seed) != height or len(seed[0]) != width:
                 raise InitError("Please make sure that the seed matches \
@@ -67,8 +75,14 @@ class Game:
         provided board_size, returns a two-dimensional list of zeros.
         """
         cols, rows = self.board_size
-        dim_one = [0 for row in range(rows)]
-        return [dim_one[:] for col in range(cols)]
+        return [[0 for row in range(rows)] for col in range(cols)]
+
+    def _create_random_seed(self) -> list:
+        """
+        Initialize the board with random alive (1) and dead (0) cells.
+        """
+        cols, rows = self.board_size
+        return [[randint(0, 1) for row in range(rows)] for col in range(cols)]
 
     def _scan_seed(self, seed: list):
         """


Test patch:
diff --git a/tests/tests.py b/tests/tests.py
index 60bbf02..8b2dbdc 100644
--- a/tests/tests.py
+++ b/tests/tests.py
@@ -280,3 +280,8 @@ class TestConway():
 
         with pytest.raises(InitError):
             Game(4, 4, seed)
+
+    def test_generate_random_seed_generates_random_seed(self):
+        test_game = Game(12, 12, random=True)
+
+        assert test_game.live_cells > 0