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

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

Към профила на Светослав Атанасов

## Резултати

• 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('=' * (BOARD_SIZE + 2), end = '|\n')
#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 check_neighbours(x,y,board):
sum = 0
for horizon in range(x-1,x+2):
for vertical in range(y-1,y+2):
sum += board[horizon][vertical]
return sum - board[x][y]
def check_rules(x,y,board):
if board[x][y] == 1 and check_neighbours(x,y,board) < 2:
return 0
if board[x][y] == 1 and check_neighbours(x,y,board) > 3:
return 0
if board[x][y] == 0 and check_neighbours(x,y,board) == 3:
return 1
return board[x][y]
def step_board(board):
return [[int(x and x != BOARD_SIZE + 1 and y and y != BOARD_SIZE + 1
and check_rules(x,y,board) == 1) for y in range(0,BOARD_SIZE + 2)]
for x in range(0,BOARD_SIZE + 2)]
if __name__ == '__main__':
board = create_board()
while True:
print_board(board)
board = step_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.234s

OK

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

### Светослав обнови решението на 25.03.2012 21:08 (преди над 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('=' * (BOARD_SIZE + 2), end = '|\n')
+ #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 check_neighbours(x,y,board):
+ sum = 0
+ for horizon in range(x-1,x+2):
+ for vertical in range(y-1,y+2):
+ sum += board[horizon][vertical]
+ return sum - board[x][y]
+
+def check_rules(x,y,board):
+ if board[x][y] == 1 and check_neighbours(x,y,board) < 2:
+ return 0
+ if board[x][y] == 1 and check_neighbours(x,y,board) > 3:
+ return 0
+ if board[x][y] == 0 and check_neighbours(x,y,board) == 3:
+ return 1
+ return board[x][y]
+
+def step_board(board):
+ return [[int(x and x != BOARD_SIZE + 1 and y and y != BOARD_SIZE + 1
+ and check_rules(x,y,board) == 1) for y in range(0,BOARD_SIZE + 2)]
+ for x in range(0,BOARD_SIZE + 2)]
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ print_board(board)
+ board = step_board(board)