Решение на Игра на живот от Петър Костов

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

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

Резултати

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

Код

#!/usr/bin/env python3
import os
import time
from copy import copy, deepcopy
from random import Random
BOARD_SIZE = 48
ALIVE = 1
DEAD = 0
BORDER = (0, BOARD_SIZE + 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 get_alive_neighbours(board, cell_coordinates):
x, y = cell_coordinates
neighbours = [cell for cell in [board[x][y - 1], board[x][y + 1]]]
neighbours += [cell for cell in board[x + 1][y - 1: y + 2]]
neighbours += [cell for cell in board[x - 1][y - 1: y + 2]]
return list(filter(lambda cell: cell is ALIVE, neighbours))
def cells(board):
for cell_x, row in enumerate(board):
for cell_y, cell_value in enumerate(row):
if cell_x not in BORDER and cell_y not in BORDER:
yield (cell_x, cell_y, cell_value)
def step_board(board):
old_board = deepcopy(board)
for cell in cells(old_board):
cell_x, cell_y, cell_value = cell
alive_neighbours = get_alive_neighbours(old_board, (cell_x, cell_y))
if cell_value is ALIVE and len(alive_neighbours) < 2:
board[cell_x][cell_y] = DEAD
elif cell_value is ALIVE and len(alive_neighbours) > 3:
board[cell_x][cell_y] = DEAD
elif cell_value is DEAD and len(alive_neighbours) is 3:
board[cell_x][cell_y] = ALIVE
if __name__ == '__main__':
board = create_board()
print_board(board)
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.343s

OK

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

Петър обнови решението на 28.03.2012 22:45 (преди над 12 години)

+#!/usr/bin/env python3
+import os
+import time
+from copy import copy, deepcopy
+from random import Random
+
+BOARD_SIZE = 48
+ALIVE = 1
+DEAD = 0
+BORDER = (0, BOARD_SIZE + 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 get_alive_neighbours(board, cell_coordinates):
+ x, y = cell_coordinates
+ neighbours = [cell for cell in [board[x][y - 1], board[x][y + 1]]]
+ neighbours += [cell for cell in board[x + 1][y - 1: y + 2]]
+ neighbours += [cell for cell in board[x - 1][y - 1: y + 2]]
+
+ return list(filter(lambda cell: cell is ALIVE, neighbours))
+
+
+def cells(board):
+ for cell_x, row in enumerate(board):
+ for cell_y, cell_value in enumerate(row):
+ if cell_x not in BORDER and cell_y not in BORDER:
+ yield (cell_x, cell_y, cell_value)
+
+
+def step_board(board):
+ old_board = deepcopy(board)
+ for cell in cells(old_board):
+ cell_x, cell_y, cell_value = cell
+ alive_neighbours = get_alive_neighbours(old_board, (cell_x, cell_y))
+
+ if cell_value is ALIVE and len(alive_neighbours) < 2:
+ board[cell_x][cell_y] = DEAD
+ elif cell_value is ALIVE and len(alive_neighbours) > 3:
+ board[cell_x][cell_y] = DEAD
+ elif cell_value is DEAD and len(alive_neighbours) is 3:
+ board[cell_x][cell_y] = ALIVE
+
+
+if __name__ == '__main__':
+ board = create_board()
+ print_board(board)
+ while True:
+ step_board(board)
+ print_board(board)