Skip to content

Commit

Permalink
add functionality to swap life state of cell
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkristoferanderson committed Aug 1, 2021
1 parent 6f0696f commit 7569695
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions game_of_life/mygamefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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])

7 changes: 7 additions & 0 deletions game_of_life/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7569695

Please sign in to comment.