Решение на Игра на живот от Стоян Стоянов

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

Към профила на Стоян Стоянов

Резултати

  • 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])
print('=' * (BOARD_SIZE + 2), end='|\n')
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 print_custom(board):
os.system(['clear', 'cls'][os.name == 'nt'])
print('=' * (BOARD_SIZE + 2), end='|\n')
for row in board:
print(*[x for x in row], end=' |\n', sep='')
def count_neighbours(board, item_row, item_cow, size=50):
result = 0
if item_cow + 1 < size and board[item_row][item_cow + 1] == 1:
result = result + 1
if item_cow - 1 >= 0 and board[item_row][item_cow - 1] == 1:
result = result + 1
if item_row + 1 < size and board[item_row + 1][item_cow] == 1:
result = result + 1
if item_row - 1 >= 0 and board[item_row - 1][item_cow] == 1:
result = result + 1
if item_row + 1 < size and item_cow + 1 < size and board[item_row + 1][item_cow + 1] == 1:
result = result + 1
if item_row - 1 >= 0 and item_cow - 1 >= 0 and board[item_row - 1][item_cow - 1] == 1:
result = result + 1
if item_row + 1 < size and item_cow - 1 >= 0 and board[item_row + 1][item_cow - 1] == 1:
result = result + 1
if item_row - 1 >= 0 and item_cow + 1 < size and board[item_row - 1][item_cow + 1] == 1:
result = result + 1
return result
def zero_matrix(size=50):
matrix = [[0 for i in range(50)] for j in range(50)]
return matrix
def step_board(board):
index_row = 1
result_board = zero_matrix()
while index_row + 1 < BOARD_SIZE + 2:
index_col = 1
while index_col + 1 < BOARD_SIZE + 2:
number_of_neighbours = count_neighbours(board, index_row, index_col)
if board[index_row][index_col] == 1:
if number_of_neighbours < 2:
result_board[index_row][index_col] = 0
if number_of_neighbours > 3:
result_board[index_row][index_col] = 0
if number_of_neighbours == 2 or number_of_neighbours == 3:
result_board[index_row][index_col] = 1
if board[index_row][index_col] == 0:
if number_of_neighbours == 3:
result_board[index_row][index_col] = 1
index_col = index_col + 1
index_row = index_row + 1
index_row = 0
while index_row < 50:
index_col = 0
while index_col < 50:
board[index_row][index_col] = result_board[index_row][index_col]
index_col = index_col + 1
index_row = index_row + 1
if __name__ == '__main__':
board = create_board()
while True:
print_board(board)
step_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.176s

OK

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

Стоян обнови решението на 31.03.2012 17:19 (преди около 12 години)

+#!/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])
+ print('=' * (BOARD_SIZE + 2), end='|\n')
+ 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 print_custom(board):
+ os.system(['clear', 'cls'][os.name == 'nt'])
+ print('=' * (BOARD_SIZE + 2), end='|\n')
+ for row in board:
+ print(*[x for x in row], end=' |\n', sep='')
+
+
+def count_neighbours(board, item_row, item_cow, size=50):
+ result = 0
+
+ if item_cow + 1 < size and board[item_row][item_cow + 1] == 1:
+ result = result + 1
+
+ if item_cow - 1 >= 0 and board[item_row][item_cow - 1] == 1:
+ result = result + 1
+
+ if item_row + 1 < size and board[item_row + 1][item_cow] == 1:
+ result = result + 1
+
+ if item_row - 1 >= 0 and board[item_row - 1][item_cow] == 1:
+ result = result + 1
+
+ if item_row + 1 < size and item_cow + 1 < size and board[item_row + 1][item_cow + 1] == 1:
+ result = result + 1
+
+ if item_row - 1 >= 0 and item_cow - 1 >= 0 and board[item_row - 1][item_cow - 1] == 1:
+ result = result + 1
+
+ if item_row + 1 < size and item_cow - 1 >= 0 and board[item_row + 1][item_cow - 1] == 1:
+ result = result + 1
+
+ if item_row - 1 >= 0 and item_cow + 1 < size and board[item_row - 1][item_cow + 1] == 1:
+ result = result + 1
+
+ return result
+
+
+def zero_matrix(size=50):
+ matrix = [[0 for i in range(50)] for j in range(50)]
+ return matrix
+
+
+def step_board(board):
+ index_row = 1
+ result_board = zero_matrix()
+
+ while index_row + 1 < BOARD_SIZE + 2:
+ index_col = 1
+
+ while index_col + 1 < BOARD_SIZE + 2:
+ number_of_neighbours = count_neighbours(board, index_row, index_col)
+
+ if board[index_row][index_col] == 1:
+ if number_of_neighbours < 2:
+ result_board[index_row][index_col] = 0
+
+ if number_of_neighbours > 3:
+ result_board[index_row][index_col] = 0
+
+ if number_of_neighbours == 2 or number_of_neighbours == 3:
+ result_board[index_row][index_col] = 1
+
+ if board[index_row][index_col] == 0:
+ if number_of_neighbours == 3:
+ result_board[index_row][index_col] = 1
+
+ index_col = index_col + 1
+
+ index_row = index_row + 1
+
+ index_row = 0
+ while index_row < 50:
+ index_col = 0
+ while index_col < 50:
+ board[index_row][index_col] = result_board[index_row][index_col]
+ index_col = index_col + 1
+ index_row = index_row + 1
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ print_board(board)
+ step_board(board)