Решение на Игра на живот от Орлин Христов

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

Към профила на Орлин Христов

Резултати

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

Код

#!/usr/bin/env python3
import os
import time
from random import Random
from itertools import product, tee, chain
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 calculate_state(cell, factor):
return 1 if factor == 3 or factor - cell == 3 else 0
def calculate_factor(row, col, board):
return sum([board[x][y] for x, y
in product(range(row - 1, row + 2), range(col - 1, col + 2))])
def cell_step(row, col, board):
return calculate_state(board[row][col], calculate_factor(row, col, board))
def core_range(list_):
return range(1, len(list_) - 1)
def bordered(list_, border=0):
return [x for x in chain([border], list_, [border])]
def step_board(board):
nextBoard = []
bodyRange = core_range(board)
for row, core in zip(bodyRange, tee(core_range(board[0]), len(bodyRange))):
nextBoard.append(bordered([cell_step(row, column, board)
for column in core]))
board[1:len(board) - 1] = nextBoard
#return bordered(nextBoard, board[0])
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
#board = step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.236s

OK

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

Орлин обнови решението на 26.03.2012 18:56 (преди почти 13 години)

+#!/usr/bin/env python3
+import os
+import time
+from random import Random
+from itertools import product, tee, chain
+
+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 calculate_state(cell, factor):
+ return 1 if factor == 3 or factor - cell == 3 else 0
+
+
+def calculate_factor(row, col, board):
+ return sum([board[x][y] for x, y
+ in product(range(row - 1, row + 2), range(col - 1, col + 2))])
+
+
+def cell_step(row, col, board):
+ return calculate_state(board[row][col], calculate_factor(row, col, board))
+
+
+def core_range(list_):
+ return range(1, len(list_) - 1)
+
+
+def bordered(list_, border=0):
+ return [x for x in chain([border], list_, [border])]
+
+
+def step_board(board):
+ nextBoard = []
+ bodyRange = core_range(board)
+ for row, core in zip(bodyRange, tee(core_range(board[0]), len(bodyRange))):
+ nextBoard.append(bordered([cell_step(row, column, board)
+ for column in core]))
+ board[1:len(board) - 1] = nextBoard
+ #return bordered(nextBoard, board[0])
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ #board = step_board(board)
+ print_board(board)