Решение на Игра на живот от Станислав Станчев

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

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

Резултати

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

Код

#!/usr/bin/env python3
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])
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 count_neightbours(board, row, column):
if row in [0, len(board[0])] or column in [0, len(board[0])]:
return 0
directions = [[-1, 0, 1, 0, -1, -1, 1, 1],
[0, -1, 0, 1, -1, 1, -1, 1]]
count = 0
for i in range(0, 8):
cell = board[row+directions[0][i]][column+directions[1][i]]
count += 1 if cell in [-1, 1] else 0
# -1 are still alive in the current iteration
return count
def step_board(board):
"""
New markings are introduced in order to use
a single matrix.
0 - dead cell, 1 - living cell
-1 - a living cell which is about to die
2 - a dead cell becoming alive
"""
for row in range(1, len(board[0])-1):
for column in range(1, len(board[0])-1):
neighbours = count_neightbours(board, row, column)
if board[row][column] == 0 and neighbours == 3:
board[row][column] = 2
elif board[row][column] == 1 and not neighbours in [2, 3]:
board[row][column] = -1
for row in range(1, len(board[0])-1):
for column in range(1, len(board[0])-1):
if board[row][column] == -1:
board[row][column] = 0
elif board[row][column] == 2:
board[row][column] = 1
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.271s

OK

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

Станислав обнови решението на 26.03.2012 12:15 (преди почти 13 години)

+#!/usr/bin/env python3
+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])
+ 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 count_neightbours(board, row, column):
+ if row in [0, len(board[0])] or column in [0, len(board[0])]:
+ return 0
+ directions = [[-1, 0, 1, 0, -1, -1, 1, 1],
+ [0, -1, 0, 1, -1, 1, -1, 1]]
+ count = 0
+ for i in range(0, 8):
+ cell = board[row+directions[0][i]][column+directions[1][i]]
+ count += 1 if cell in [-1, 1] else 0
+ # -1 are still alive in the current iteration
+ return count
+
+def step_board(board):
+ """
+ New markings are introduced in order to use
+ a single matrix.
+ 0 - dead cell, 1 - living cell
+ -1 - a living cell which is about to die
+ 2 - a dead cell becoming alive
+ """
+ for row in range(1, len(board[0])-1):
+ for column in range(1, len(board[0])-1):
+ neighbours = count_neightbours(board, row, column)
+ if board[row][column] == 0 and neighbours == 3:
+ board[row][column] = 2
+ elif board[row][column] == 1 and not neighbours in [2, 3]:
+ board[row][column] = -1
+ for row in range(1, len(board[0])-1):
+ for column in range(1, len(board[0])-1):
+ if board[row][column] == -1:
+ board[row][column] = 0
+ elif board[row][column] == 2:
+ board[row][column] = 1
+
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)