# Решение на Игра на живот от Тодор Ганчев

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

Към профила на Тодор Ганчев

## Резултати

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

## Код

#!/usr/bin/env python3
import os
import time
from random import Random
BOARD_SIZE = 48
WHOLE_SIZE = BOARD_SIZE + 2
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 i in range(WHOLE_SIZE)]
for j in range(WHOLE_SIZE)]
for row in range(1, WHOLE_SIZE - 1):
for column in range(1, WHOLE_SIZE - 1):
new_board[row][column] = get_new_state(row, column, board)
for i in range(WHOLE_SIZE):
board[i] = new_board[i]
def alive(cell):
return cell % 2
def get_new_state(row, column, board):
alive_neighbours_count = count_alive_neighbours(row, column, board)
if(alive(board[row][column])):
if(alive_neighbours_count == 2 or alive_neighbours_count == 3):
return 1
else:
return 0
else:
if(alive_neighbours_count == 3):
return 1
else:
return 0
def count_alive_neighbours(row, column, board):
neighbour_rows = [row - 1, row, row + 1]
neighbour_columns = [column - 1, column, column + 1]
neighbour_cells = [board[i][j] for i in neighbour_rows
for j in neighbour_columns if (i, j) != (row, column)]
return sum([value % 2 for value in neighbour_cells])
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.298s

OK

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

### Тодор обнови решението на 31.03.2012 06:33 (преди над 12 години)

+#!/usr/bin/env python3
+import os
+import time
+from random import Random
+
+BOARD_SIZE = 48
+WHOLE_SIZE = BOARD_SIZE + 2
+
+
+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 i in range(WHOLE_SIZE)]
+ for j in range(WHOLE_SIZE)]
+
+ for row in range(1, WHOLE_SIZE - 1):
+ for column in range(1, WHOLE_SIZE - 1):
+ new_board[row][column] = get_new_state(row, column, board)
+
+ for i in range(WHOLE_SIZE):
+ board[i] = new_board[i]
+
+
+def alive(cell):
+ return cell % 2
+
+
+def get_new_state(row, column, board):
+ alive_neighbours_count = count_alive_neighbours(row, column, board)
+
+ if(alive(board[row][column])):
+ if(alive_neighbours_count == 2 or alive_neighbours_count == 3):
+ return 1
+ else:
+ return 0
+ else:
+ if(alive_neighbours_count == 3):
+ return 1
+ else:
+ return 0
+
+
+def count_alive_neighbours(row, column, board):
+ neighbour_rows = [row - 1, row, row + 1]
+ neighbour_columns = [column - 1, column, column + 1]
+ neighbour_cells = [board[i][j] for i in neighbour_rows
+ for j in neighbour_columns if (i, j) != (row, column)]
+ return sum([value % 2 for value in neighbour_cells])
+
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)