# Решение на Игра на живот от Никола Велинов

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

Към профила на Никола Велинов

## Резултати

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

## Код

#!/usr/bin/env python3
import os
import time
from random import Random
BOARD_SIZE = 48
HAS_DIED = 3
IS_BORN = 2
IS_ALIVE = 1
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 update_cell(board, i, j):
live_cells = 0
live_cells += board[i - 1][j] % 2
live_cells += board[i + 1][j] % 2
live_cells += board[i][j - 1] % 2
live_cells += board[i][j + 1] % 2
live_cells += board[i + 1][j - 1] % 2
live_cells += board[i - 1][j - 1] % 2
live_cells += board[i + 1][j + 1] % 2
live_cells += board[i - 1][j + 1] % 2
if live_cells < 2 or live_cells > 3:
if board[i][j] == IS_ALIVE:
return HAS_DIED
else:
elif live_cells == 3 and board[i][j] == IS_DEAD:
return IS_BORN
else:
return board[i][j]
def step_board(board):
for i in range(1, BOARD_SIZE + 1):
for j in range(1, BOARD_SIZE + 1):
board[i][j] = update_cell(board, i, j)
for i in range(1, BOARD_SIZE + 1):
for j in range(1, BOARD_SIZE + 1):
board[i][j] = board[i][j] != HAS_DIED and board[i][j] != IS_DEAD
return
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.146s

OK

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

### Никола обнови решението на 31.03.2012 20:25 (преди над 8 години)

+#!/usr/bin/env python3
+import os
+import time
+from random import Random
+
+BOARD_SIZE = 48
+HAS_DIED = 3
+IS_BORN = 2
+IS_ALIVE = 1
+
+
+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 update_cell(board, i, j):
+ live_cells = 0
+ live_cells += board[i - 1][j] % 2
+ live_cells += board[i + 1][j] % 2
+ live_cells += board[i][j - 1] % 2
+ live_cells += board[i][j + 1] % 2
+ live_cells += board[i + 1][j - 1] % 2
+ live_cells += board[i - 1][j - 1] % 2
+ live_cells += board[i + 1][j + 1] % 2
+ live_cells += board[i - 1][j + 1] % 2
+ if live_cells < 2 or live_cells > 3:
+ if board[i][j] == IS_ALIVE:
+ return HAS_DIED
+ else:
+ elif live_cells == 3 and board[i][j] == IS_DEAD:
+ return IS_BORN
+ else:
+ return board[i][j]
+
+
+def step_board(board):
+ for i in range(1, BOARD_SIZE + 1):
+ for j in range(1, BOARD_SIZE + 1):
+ board[i][j] = update_cell(board, i, j)
+ for i in range(1, BOARD_SIZE + 1):
+ for j in range(1, BOARD_SIZE + 1):
+ board[i][j] = board[i][j] != HAS_DIED and board[i][j] != IS_DEAD
+ return
+
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)