diff --git a/game_of_life/mygamefile.py b/game_of_life/mygamefile.py index 6aec20d..41a0858 100644 --- a/game_of_life/mygamefile.py +++ b/game_of_life/mygamefile.py @@ -21,6 +21,12 @@ def board_to_string(self): s += '\n' return s + def swap_life_state(self, row, column): + if self.is_alive(row, column): + self.board[row][column] = 0 + else: + self.board[row][column] = 1 + def set_cell_alive(self, row, column): self.board[row][column] = 1 @@ -56,3 +62,6 @@ def count_neighbors(self, row_num, col_num): # todo just for fun, try it later as if all cells beyond the boundary are alive return count + def is_alive(self, row, column): + return bool(self.board[row][column]) + diff --git a/game_of_life/test_game.py b/game_of_life/test_game.py index f0f7398..d3e2687 100644 --- a/game_of_life/test_game.py +++ b/game_of_life/test_game.py @@ -33,6 +33,13 @@ def test_set_cell_dead(): game.set_cell_dead(2, 4) assert game.board == resources.EMPTY_BOARD +def test_swap_life_state(): + game = Game() + game.set_cell_alive(2, 4) + game.swap_life_state(2, 4) + assert game.board == resources.EMPTY_BOARD + game.swap_life_state(2, 4) + assert game.board == resources.BOARD_WITH_ROW_2_COLUMN_4_ALIVE def test_count_neighbors(): game = Game()