Решение на Игра на живот от Лазар Лазаров

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

Към профила на Лазар Лазаров

Резултати

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

Код

#!/usr/bin/env python3
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 is_alive(board, i, j):
return board[i][j]
def concider_cell(board, i, j):
live_neighbors = 0
for neighbor in [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1],
[1, 0], [1, 1]]:
if is_alive(board, i + neighbor[0], j + neighbor[1]):
live_neighbors = live_neighbors + 1
if live_neighbors < 2 or live_neighbors > 3:
return 'die'
if live_neighbors == 2:
return 'live'
if live_neighbors == 3:
return 'new life'
def step_board(board):
the_old_board = copy.deepcopy(board)
for i in range(1, BOARD_SIZE + 1):
for j in range(1, BOARD_SIZE + 1):
the_fate_of_the_cell = concider_cell(the_old_board, i, j)
if the_fate_of_the_cell == 'die':
board[i][j] = 0
if the_fate_of_the_cell == 'new life':
board[i][j] = 1
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.233s

OK

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

Лазар обнови решението на 25.03.2012 22:40 (преди над 12 години)

+#!/usr/bin/env python3
+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 is_alive(board, i, j):
+ return board[i][j]
+
+def concider_cell(board, i, j):
+ live_neighbors = 0
+ for neighbor in [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1],
+ [1, 0], [1, 1]]:
+ if is_alive(board, i + neighbor[0], j + neighbor[1]):
+ live_neighbors = live_neighbors + 1
+ if live_neighbors < 2 or live_neighbors > 3:
+ return 'die'
+ if live_neighbors == 2:
+ return 'live'
+ if live_neighbors == 3:
+ return 'new life'
+
+def step_board(board):
+ the_old_board = copy.deepcopy(board)
+ for i in range(1, BOARD_SIZE + 1):
+ for j in range(1, BOARD_SIZE + 1):
+ the_fate_of_the_cell = concider_cell(the_old_board, i, j)
+ if the_fate_of_the_cell == 'die':
+ board[i][j] = 0
+ if the_fate_of_the_cell == 'new life':
+ board[i][j] = 1
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)