Петър обнови решението на 01.04.2012 21:35 (преди над 12 години)
+#!/usr/bin/env python3
+import os
+import time
+from random import Random
+
+BOARD_SIZE = 48
+
+def print_board(board, wait = False, sleep = False):
+ os.system(['clear','cls'][os.name == 'nt'])
+ print('=' * (BOARD_SIZE + 2), end = '|\n')
+ for row in board:
+ print(*['X' if x else ' ' for x in row], end = ' |\n', sep = '')
+
+ print([[x & 1 for x in row] for row in board])
+ input("Press enter to continue.")
+
+def create_board(fill_rate = 0.3):
+ rand = Random()
+ return [[int(x and x != BOARD_SIZE + 1 and y and y != BOARD_SIZE + 1
+ and rand.normalvariate(0.5,0.2) < fill_rate) for x in range(0,BOARD_SIZE + 2)] for y in range(0,BOARD_SIZE + 2)]
+
+def get_alive_neighbours_number(board,row,column):
+ alive = 0
+ if board[row - 1][column - 1] == 1:
+ alive = alive + 1
+ if board[row - 1][column] == 1:
+ alive = alive + 1
+ if board[row - 1][column + 1] == 1:
+ alive = alive + 1
+ if board[row][column - 1] == 1:
+ alive = alive + 1
+ #we don't count the cell we are at
+ if board[row][column + 1] == 1:
+ alive = alive + 1
+ if board[row + 1][column - 1] == 1:
+ alive = alive + 1
+ if board[row + 1][column] == 1:
+ alive = alive + 1
+ if board[row + 1][column + 1] == 1:
+ alive = alive + 1
+ return alive
+
+def step_board(board):
+ next_level_board = create_board()
+ for row in range(1,49):
+ for column in range(1,49):
+ if get_alive_neighbours_number(board,row,column) == 3:
+ next_level_board[row][column] = 1
+ elif get_alive_neighbours_number(board,row,column) == 2:
+ if board[row][column] == 1:
+ next_level_board[row][column] = 1
+ else:
+ next_level_board[row][column] = 0
+ if get_alive_neighbours_number(board,row,column) < 2 or get_alive_neighbours_number(board,row,column) > 3:
+ next_level_board[row][column] = 0
+ board = next_level_board
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)