Антония обнови решението на 02.04.2012 21:01 (преди над 12 години)
+def step_board(board):
+ for i, row in enumerate(get_next_board(board)):
+ for j, col in enumerate(row):
+ board[i][j] = col
+
+def get_next_board(board):
+ next_board = []
+ for i, row in enumerate(board):
+ next_board_row = []
+ if i == 0 or i == BOARD_SIZE + 1:
+ next_board.append(50*[0])
+ else:
+ for j, col in enumerate(row):
+ if j == 0 or j == BOARD_SIZE + 1:
+ next_board_row.append(0)
+ else:
+ next_board_row.append(get_next_cell_state(board, i, j))
+ next_board.append(next_board_row)
+
+ return next_board
+
+def count_alive_neighbours(board, i, j):
+ alive_neighbours_count = 0
+ row_start_index = i - 1
+ row_end_index = i + 2
+ col_start_index = j - 1
+ col_end_index = j + 2
+ if i == 0:
+ row_start_index = i
+ if i == BOARD_SIZE + 1:
+ row_end_index = i + 1
+ if j == 0:
+ col_start_index = j
+ if j == BOARD_SIZE + 1:
+ col_end_index = j + 1
+
+ for row in range(row_start_index, row_end_index):
+ for col in range(col_start_index, col_end_index):
+ if (row != i or col != j):
+ if board[row][col] == 1:
+ alive_neighbours_count += 1
+
+ return alive_neighbours_count
+
+def get_next_cell_state(board, x, y):
+ neighbours = count_alive_neighbours(board, x, y)
+ return int((board[x][y] and neighbours == 2) or (neighbours == 3))