From f9607726dfc5afaa133a9bab10eb192846ac3181 Mon Sep 17 00:00:00 2001 From: Pavel Marychev Date: Mon, 2 Aug 2021 23:00:20 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=20=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D1=82=D1=8C=20=D0=B3=D0=BB=D0=B0=D0=B2=D1=83=2011?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch11/model.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 ch11/model.py diff --git a/ch11/model.py b/ch11/model.py new file mode 100644 index 0000000..ad5a30b --- /dev/null +++ b/ch11/model.py @@ -0,0 +1,49 @@ +height = 100 +width = 100 + +grid_model = [0] * height + +for i in range(height): + grid_model[i] = [0] * width + + +def next_gen(): + global grid_model + + for i in range(0, height): + for j in range(0, width): + cell = 0 + print('Проверяем клетку', i, j) + count = count_neighbors(grid_model, i, j) + + if grid_model[i][j] == 0: + if count == 3: + cell = 1 + elif grid_model[i][j] == 1: + if count == 2 or count == 3: + cell = 1 + + +def count_neighbors(grid, row, col): + count = 0 + if row - 1 >= 0: + count = count + grid[row - 1][col] + if (row - 1 >= 0) and (col - 1 >= 0): + count = count + grid[row - 1][col - 1] + if (row - 1 >= 0) and (col + 1 < width): + count = count + grid[row - 1][col + 1] + if col - 1 >= 0: + count = count + grid[row][col - 1] + if col + 1 < width: + count = count + grid[row][col + 1] + if row + 1 < height: + count = count + grid[row + 1][col] + if (row + 1 < height) and (col - 1 >= 0): + count = count + grid[row + 1][col - 1] + if (row + 1 < height) and (col + 1 < width): + count = count + grid[row + 1][col + 1] + return count + + +if __name__ == '__main__': + next_gen()