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

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

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

Резултати

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

Код

import os
import time
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]) # fixme add
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_neighbour_count(board, i, j):
coordinates = [(-1, -1), (-1, 0), (-1, 1), (0, -1),
(0, 1), (1, -1), (1, 0), (1, 1)]
return sum(board[i + coordinate[0]][j + coordinate[1]]
for coordinate in coordinates)
class CellInfo:
def __init__(self, is_alive, neighbour_count):
self.is_alive = is_alive
self.neighbour_count = neighbour_count
def step_board(board):
cells = [[CellInfo(board[i][j], get_neighbour_count(board, i, j))
for j in range(1, BOARD_SIZE + 1)] for i in range(1, BOARD_SIZE + 1)]
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
cell = cells[i][j]
board[i + 1][j + 1] = (cell.is_alive and cell.neighbour_count in [2, 3])\
or (not cell.is_alive and cell.neighbour_count == 3)
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.252s

OK

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

Николай обнови решението на 24.03.2012 15:55 (преди почти 13 години)

+import os
+import time
+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]) # fixme add
+ 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_neighbour_count(board, i, j):
+ coordinates = [(-1, -1), (-1, 0), (-1, 1), (0, -1),
+ (0, 1), (1, -1), (1, 0), (1, 1)]
+ return sum(board[i + coordinate[0]][j + coordinate[1]]
+ for coordinate in coordinates)
+
+class CellInfo:
+ def __init__(self, is_alive, neighbour_count):
+ self.is_alive = is_alive
+ self.neighbour_count = neighbour_count
+
+def step_board(board):
+ cells = [[CellInfo(board[i][j], get_neighbour_count(board, i, j))
+ for j in range(1, BOARD_SIZE + 1)] for i in range(1, BOARD_SIZE + 1)]
+ for i in range(BOARD_SIZE):
+ for j in range(BOARD_SIZE):
+ cell = cells[i][j]
+ board[i + 1][j + 1] = (cell.is_alive and cell.neighbour_count in [2, 3])\
+ or (not cell.is_alive and cell.neighbour_count == 3)
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)