Решение на Игра на живот от Николай Стоицев

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

Към профила на Николай Стоицев

Резултати

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

Код

import os
import time
import itertools
from random import Random
from copy import deepcopy
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 step_board(new_board):
old_board = deepcopy(new_board)
for row_index in range(1, BOARD_SIZE + 1):
for cell_index in range(1, BOARD_SIZE + 1):
neighbours = get_neighbours(cell_index, row_index, old_board)
live_neighbours = get_live_neighbours(neighbours)
if live_neighbours >= 2 and live_neighbours <= 3 and old_board[cell_index][row_index] == 1:
new_board[cell_index][row_index] = 1
elif live_neighbours == 3 and old_board[cell_index][row_index] == 0:
new_board[cell_index][row_index] = 1
else:
new_board[cell_index][row_index] = 0
def get_live_neighbours(neighbours):
result = 0
for cell in neighbours:
if cell:
result += 1
return result
def get_neighbours(x, y, board):
neighbours = []
neighbours.append(board[x - 1][y + 1])
neighbours.append(board[x - 1][y])
neighbours.append(board[x][y + 1])
neighbours.append(board[x - 1][y - 1])
neighbours.append(board[x + 1][y + 1])
neighbours.append(board[x][y - 1])
neighbours.append(board[x + 1][y - 1])
neighbours.append(board[x + 1][y])
return neighbours
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.179s

OK

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

Николай обнови решението на 27.03.2012 01:50 (преди над 8 години)

+import os
+import time
+import itertools
+from random import Random
+from copy import deepcopy
+
+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 step_board(new_board):
+ old_board = deepcopy(new_board)
+ for row_index in range(1, BOARD_SIZE + 1):
+ for cell_index in range(1, BOARD_SIZE + 1):
+ neighbours = get_neighbours(cell_index, row_index, old_board)
+ live_neighbours = get_live_neighbours(neighbours)
+ if live_neighbours >= 2 and live_neighbours <= 3 and old_board[cell_index][row_index] == 1:
+ new_board[cell_index][row_index] = 1
+ elif live_neighbours == 3 and old_board[cell_index][row_index] == 0:
+ new_board[cell_index][row_index] = 1
+ else:
+ new_board[cell_index][row_index] = 0
+
+def get_live_neighbours(neighbours):
+ result = 0
+ for cell in neighbours:
+ if cell:
+ result += 1
+ return result
+
+def get_neighbours(x, y, board):
+ neighbours = []
+ neighbours.append(board[x - 1][y + 1])
+ neighbours.append(board[x - 1][y])
+ neighbours.append(board[x][y + 1])
+ neighbours.append(board[x - 1][y - 1])
+ neighbours.append(board[x + 1][y + 1])
+ neighbours.append(board[x][y - 1])
+ neighbours.append(board[x + 1][y - 1])
+ neighbours.append(board[x + 1][y])
+ return neighbours
+
+if __name__ == '__main__':
+ board = create_board()
+
+ while True:
+ step_board(board)
+ print_board(board)