# Решение на Игра на живот от Стоян Стоянов

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

Към профила на Стоян Стоянов

## Резултати

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

## Код

import os
import time
import copy
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):
copy_board = copy.deepcopy(board)
def count_cells(row, column):
nearby_cells_count = 0
if copy_board[row - 1][column - 1] == 1:
nearby_cells_count += 1
if copy_board[row - 1][column] == 1:
nearby_cells_count += 1
if copy_board[row - 1][column + 1] == 1:
nearby_cells_count += 1
if copy_board[row][column - 1] == 1:
nearby_cells_count += 1
if copy_board[row][column + 1] == 1:
nearby_cells_count += 1
if copy_board[row + 1][column - 1] == 1:
nearby_cells_count += 1
if copy_board[row + 1][column] == 1:
nearby_cells_count += 1
if copy_board[row + 1][column + 1] == 1:
nearby_cells_count += 1
return nearby_cells_count
def game_conditions(row, column):
nearby_cells_count = count_cells(row, column)
if copy_board[row][column] == 0 and nearby_cells_count == 3:
board[row][column] = 1
elif copy_board[row][column] == 1 and (nearby_cells_count == 2 or nearby_cells_count == 3):
board[row][column] = 1
else:
board[row][column] = 0
for row in range(1,49):
for column in range(1,49):
game_conditions(row, column)
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.170s

OK

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

### Стоян обнови решението на 02.04.2012 19:06 (преди над 12 години)

+import os
+import time
+import copy
+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):
+
+ copy_board = copy.deepcopy(board)
+
+ def count_cells(row, column):
+
+ nearby_cells_count = 0
+
+ if copy_board[row - 1][column - 1] == 1:
+ nearby_cells_count += 1
+ if copy_board[row - 1][column] == 1:
+ nearby_cells_count += 1
+ if copy_board[row - 1][column + 1] == 1:
+ nearby_cells_count += 1
+ if copy_board[row][column - 1] == 1:
+ nearby_cells_count += 1
+ if copy_board[row][column + 1] == 1:
+ nearby_cells_count += 1
+ if copy_board[row + 1][column - 1] == 1:
+ nearby_cells_count += 1
+ if copy_board[row + 1][column] == 1:
+ nearby_cells_count += 1
+ if copy_board[row + 1][column + 1] == 1:
+ nearby_cells_count += 1
+
+ return nearby_cells_count
+
+
+ def game_conditions(row, column):
+
+ nearby_cells_count = count_cells(row, column)
+
+ if copy_board[row][column] == 0 and nearby_cells_count == 3:
+ board[row][column] = 1
+
+ elif copy_board[row][column] == 1 and (nearby_cells_count == 2 or nearby_cells_count == 3):
+ board[row][column] = 1
+
+ else:
+ board[row][column] = 0
+
+ for row in range(1,49):
+ for column in range(1,49):
+ game_conditions(row, column)
+
+
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)
+