Repository: munichpavel/clovek-ne-jezi-se

Issue:
Index error during game play
My best guess is that the called `advance_edges` below is empty. Need some logic to return `None` in this case for `to_space`. Would be good to have play logging for situations like this, e.g. #16 

```
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-34-a3bf77e003f6> in <module>
----> 1 client.play()
      2 client.get_game_state().draw(color_map=color_map)

~/delo/projects/clovek-ne-jezi-se/clovek_ne_jezi_se/client.py in play(self)
     52 
     53         moves = self._game_state.get_player_moves(
---> 54             roll_value, current_player.name
     55         )
     56         print(f'Player {current_player.name} rolls a {roll_value}')

~/delo/projects/clovek-ne-jezi-se/clovek_ne_jezi_se/game_state.py in get_player_moves(self, roll, player_name)
    372         )
    373         primary_moves = self._get_primary_moves(
--> 374             roll, player_occupied_node_names
    375         )
    376 

~/delo/projects/clovek-ne-jezi-se/clovek_ne_jezi_se/game_state.py in _get_primary_moves(self, roll, node_names)
    402             )
    403             primary_move_candidate = self.move_factory(
--> 404                 from_space=board_space, roll=roll
    405             )
    406             if primary_move_candidate.to_space is not None:

~/delo/projects/clovek-ne-jezi-se/clovek_ne_jezi_se/game_state.py in move_factory(self, from_space, roll)
    447         """
    448 
--> 449         to_space = self._get_to_space(from_space, roll)
    450         return MoveContainer(
    451             from_space=from_space,

~/delo/projects/clovek-ne-jezi-se/clovek_ne_jezi_se/game_state.py in _get_to_space(self, from_space, roll)
    487             player_subgraph_view, source=from_node_name, depth_limit=roll+1
    488         ))
--> 489         to_node_name = advance_edges[roll-1][1]
    490         to_node = self._graph.nodes[to_node_name]
    491         to_space = BoardSpace(

IndexError: list index out of range
```

Game state drawing of error:

![Screenshot 2021-01-03 at 13 00 09](https://user-images.githubusercontent.com/21952941/103477958-d99f7680-4dc3-11eb-95e3-9b28de3fbea7.png)


Discussion:

Patch:
diff --git a/clovek_ne_jezi_se/agents.py b/clovek_ne_jezi_se/agents.py
index 33be7c5..8c011d8 100644
--- a/clovek_ne_jezi_se/agents.py
+++ b/clovek_ne_jezi_se/agents.py
@@ -32,5 +32,4 @@ class HumanPlayer(Player):
 
         chosen_move_idx = int(input('Enter chosen move index: '))
         res = allowed_moves[chosen_move_idx]
-        print(f'\nYou selected move {res}')
         return res
diff --git a/clovek_ne_jezi_se/game_state.py b/clovek_ne_jezi_se/game_state.py
index 740270f..7b6bcd7 100644
--- a/clovek_ne_jezi_se/game_state.py
+++ b/clovek_ne_jezi_se/game_state.py
@@ -486,16 +486,19 @@ class GameState:
         advance_edges = list(nx.dfs_edges(
             player_subgraph_view, source=from_node_name, depth_limit=roll+1
         ))
-        to_node_name = advance_edges[roll-1][1]
-        to_node = self._graph.nodes[to_node_name]
-        to_space = BoardSpace(
-            kind=to_node['kind'],
-            idx=to_node['idx'],
-            occupied_by=to_node['occupied_by'],
-            allowed_occupants=to_node['allowed_occupants']
-        )
+        if roll > len(advance_edges):
+            return None
+        else:
+            to_node_name = advance_edges[roll-1][1]
+            to_node = self._graph.nodes[to_node_name]
+            to_space = BoardSpace(
+                kind=to_node['kind'],
+                idx=to_node['idx'],
+                occupied_by=to_node['occupied_by'],
+                allowed_occupants=to_node['allowed_occupants']
+            )
 
-        return to_space
+            return to_space
 
     def _get_player_subgraph_query_paramses(
         self, player_name: str


Test patch:
diff --git a/tests/test_state.py b/tests/test_state.py
index e65ddb0..60257e1 100644
--- a/tests/test_state.py
+++ b/tests/test_state.py
@@ -199,6 +199,7 @@ class TestGameState:
         player_prehome_indices[player_name] = \
             player_subgraph.nodes[player_prehome_node_name]['idx']
 
+    # Move tests
     @pytest.mark.parametrize(
         "roll,from_space,expected_to_space_kwargs",
         [
@@ -209,6 +210,21 @@ class TestGameState:
                 ),
                 None
             ),
+            (
+                1, BoardSpace(
+                    kind='home', idx=3, occupied_by='red',
+                    allowed_occupants=['red', EMPTY_SYMBOL]
+                ),
+                None
+            ),
+            (
+                5, BoardSpace(
+                    kind='main', idx=player_prehome_indices['red'],
+                    occupied_by='red',
+                    allowed_occupants=player_names + [EMPTY_SYMBOL]
+                ),
+                None
+            ),
             (
                 6, BoardSpace(
                     kind='waiting', idx=0, occupied_by='red',
@@ -271,34 +287,6 @@ class TestGameState:
                 from_space=from_space, to_space=expected_to_space
             )
 
-    @pytest.mark.parametrize(
-        'roll,from_space,Error',
-        [
-            (
-                1, BoardSpace(
-                    kind='home', idx=3, occupied_by='red',
-                    allowed_occupants=['red', EMPTY_SYMBOL]
-                ),
-                IndexError
-            ),
-            (
-                5, BoardSpace(
-                    kind='main', idx=player_prehome_indices['red'],
-                    occupied_by='red',
-                    allowed_occupants=player_names + [EMPTY_SYMBOL]
-                ),
-                IndexError
-            ),
-
-        ]
-    )
-    def test_move_factory_exceptions(
-        self, roll, from_space, Error
-    ):
-        with pytest.raises(Error):
-            self.game_state.move_factory(from_space, roll)
-
-    # Move tests
     @pytest.mark.parametrize(
         'roll,from_space,to_space,post_do_from_space,post_do_to_space',
         [