# Решение на Игра на живот от Мирослав Милев

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

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

## Резултати

• 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):
new_board = [[0 for x in range(0,BOARD_SIZE + 2)]
for y in range(0,BOARD_SIZE + 2)]
for i, row in enumerate(board[:]):
for j, x in enumerate(row[:]):
if (i in range (1, BOARD_SIZE + 1) and
j in range (1, BOARD_SIZE + 1)):
sum_of_neighbours = 0
neighbours = [[i - 1, j - 1], [i, j - 1], [i + 1, j - 1],
[i - 1, j], [i + 1, j], [i - 1, j + 1],
[i, j + 1], [i + 1, j + 1]]
for neighbour in neighbours:
if (neighbour[0] in range(0, BOARD_SIZE + 2) and
neighbour[1] in range(0, BOARD_SIZE + 2)):
sum_of_neighbours += board[neighbour[0]][neighbour[1]]
if (x == 0 and sum_of_neighbours == 3):
new_board[i][j] = 1
elif (x == 1 and (sum_of_neighbours < 2 or
sum_of_neighbours > 3)):
new_board[i][j] = 0
else:
new_board[i][j] = board[i][j]
for i, row in enumerate(new_board):
board[i] = row
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.634s

OK

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

### Мирослав обнови решението на 27.03.2012 22:37 (преди над 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 step_board(board):
+ new_board = [[0 for x in range(0,BOARD_SIZE + 2)]
+ for y in range(0,BOARD_SIZE + 2)]
+ for i, row in enumerate(board[:]):
+ for j, x in enumerate(row[:]):
+ if (i in range (1, BOARD_SIZE + 1) and
+ j in range (1, BOARD_SIZE + 1)):
+ sum_of_neighbours = 0
+ neighbours = [[i - 1, j - 1], [i, j - 1], [i + 1, j - 1],
+ [i - 1, j], [i + 1, j], [i - 1, j + 1],
+ [i, j + 1], [i + 1, j + 1]]
+ for neighbour in neighbours:
+ if (neighbour[0] in range(0, BOARD_SIZE + 2) and
+ neighbour[1] in range(0, BOARD_SIZE + 2)):
+ sum_of_neighbours += board[neighbour[0]][neighbour[1]]
+ if (x == 0 and sum_of_neighbours == 3):
+ new_board[i][j] = 1
+ elif (x == 1 and (sum_of_neighbours < 2 or
+ sum_of_neighbours > 3)):
+ new_board[i][j] = 0
+ else:
+ new_board[i][j] = board[i][j]
+ for i, row in enumerate(new_board):
+ board[i] = row
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)