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

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

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

Резултати

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

Код

import os
import time
from random import Random
from itertools import *
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 map_cell_coords(cell_coords, board):
x, y = cell_coords
return board[x][y]
def neighbours_of(cell_coords):
x, y = cell_coords
return [(x - 1, y + 1), (x, y + 1), (x + 1, y + 1),
(x - 1, y), (x + 1, y), (x - 1, y - 1), (x, y - 1), (x + 1, y - 1)]
def live_neighbours_count(cell_coords, board):
neighbours_coords = neighbours_of(cell_coords)
neighbour_cells = []
for neighbour_coords in neighbours_coords:
neighbour_cells.append(map_cell_coords(neighbour_coords, board))
return neighbour_cells.count(1)
def cell_lives(cell_coords, board):
if map_cell_coords(cell_coords, board) == 0:
if live_neighbours_count(cell_coords, board) == 3:
return True
else:
if live_neighbours_count(cell_coords, board) in [2,3]:
return True
return False
def clear_board():
board_row = [0]*50
return [board_row[:] for x in range(0,50)]
def step_board(board):
cells_coords = list(product(range(1,49),repeat = 2))
new_board = clear_board()
for cell_coords in cells_coords:
if cell_lives(cell_coords, board):
new_board[cell_coords[0]][cell_coords[1]] = 1
board.__init__(new_board)
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.225s

OK

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

Лъчезар обнови решението на 02.04.2012 18:50 (преди над 8 години)

+import os
+import time
+from random import Random
+from itertools import *
+
+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 map_cell_coords(cell_coords, board):
+ x, y = cell_coords
+ return board[x][y]
+
+def neighbours_of(cell_coords):
+ x, y = cell_coords
+ return [(x - 1, y + 1), (x, y + 1), (x + 1, y + 1),
+ (x - 1, y), (x + 1, y), (x - 1, y - 1), (x, y - 1), (x + 1, y - 1)]
+
+def live_neighbours_count(cell_coords, board):
+ neighbours_coords = neighbours_of(cell_coords)
+ neighbour_cells = []
+ for neighbour_coords in neighbours_coords:
+ neighbour_cells.append(map_cell_coords(neighbour_coords, board))
+ return neighbour_cells.count(1)
+
+def cell_lives(cell_coords, board):
+ if map_cell_coords(cell_coords, board) == 0:
+ if live_neighbours_count(cell_coords, board) == 3:
+ return True
+ else:
+ if live_neighbours_count(cell_coords, board) in [2,3]:
+ return True
+ return False
+
+def clear_board():
+ board_row = [0]*50
+ return [board_row[:] for x in range(0,50)]
+
+def step_board(board):
+ cells_coords = list(product(range(1,49),repeat = 2))
+ new_board = clear_board()
+ for cell_coords in cells_coords:
+ if cell_lives(cell_coords, board):
+ new_board[cell_coords[0]][cell_coords[1]] = 1
+ board.__init__(new_board)
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)