# Решение на Игра на живот от Елина Кънева

Обратно към всички решения

Към профила на Елина Кънева

## Резултати

• 6 точки от тестове
• 0 бонус точки
• 6 точки общо
• 1 успешни тест(а)
• 0 неуспешни тест(а)

## Код

#!/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 step_board(board):
#modified_board = board[:]
modified_board = [[elem for elem in row] for row in board][:]
for i, row in enumerate(board):
if i not in [0,49]:
for j, elem in enumerate(row):
if j not in [0,49]:
neighbours = 0
for x in [i-1,i,i+1]:
for y in [j-1,j,j+1]:
if board[x][y]:
neighbours += 1
if board[i][j]:
neighbours -= 1
if board[i][j]:
if neighbours < 2 or neighbours > 3:
modified_board[i][j] = 0
else:
if neighbours == 3:
modified_board[i][j] = 1
#board = [row for row in modified_bord]
#board = modified_board[:]
for row in range(1,49):
board[row] = modified_board[row][:]
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

## Лог от изпълнението

.
----------------------------------------------------------------------
Ran 1 test in 0.140s

OK

## История (1 версия и 0 коментара)

### Елина обнови решението на 02.04.2012 23:55 (преди над 8 години)

+#!/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 step_board(board):
+
+ #modified_board = board[:]
+ modified_board = [[elem for elem in row] for row in board][:]
+
+ for i, row in enumerate(board):
+ if i not in [0,49]:
+ for j, elem in enumerate(row):
+ if j not in [0,49]:
+
+ neighbours = 0
+ for x in [i-1,i,i+1]:
+ for y in [j-1,j,j+1]:
+ if board[x][y]:
+ neighbours += 1
+
+ if board[i][j]:
+ neighbours -= 1
+
+
+ if board[i][j]:
+ if neighbours < 2 or neighbours > 3:
+ modified_board[i][j] = 0
+
+ else:
+ if neighbours == 3:
+ modified_board[i][j] = 1
+
+ #board = [row for row in modified_bord]
+ #board = modified_board[:]
+ for row in range(1,49):
+ board[row] = modified_board[row][:]
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)