Решение на Игра на живот от Лидия Георгиева

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

Към профила на Лидия Георгиева

Резултати

  • 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 step_board(board):
help_board = create_help_board()
make_iteration(board, help_board)
set_board_to_help_board(board, help_board)
def create_help_board():
"""Create help board, initialized with zeroes"""
return [list(x) for x in [[0] * 50] * 50]
def count_living_neighbours(cell, i, j):
"""Count living neighbours of a cell[i][j]"""
count_neighbours = 0
for row in range(-1, 2):
for column in range(-1, 2):
count_neighbours += cell[i + row][j + column]
return count_neighbours - cell[i][j]
def dead_or_alive_in_next_iteration(cell, count_neighbours):
"""Decide whether cell will live or die in the next iteration,
depending on the number of living neigbours
"""
if (cell == 1) and (count_neighbours < 2 or count_neighbours > 3):
cell = 0
if cell == 0 and count_neighbours == 3:
cell = 1
return cell
def set_board_to_help_board(board, help_board):
"""Set the elements of help_board to board elements"""
for row in range(1, 49):
for column in range(1, 49):
value = help_board[row][column]
board[row].__setitem__(column, value)
def make_iteration(board, help_board):
"""Make an iteration and change the help_board"""
for row in range(1, 49):
for column in range(1, 49):
count = count_living_neighbours(board, row, column)
new_value = dead_or_alive_in_next_iteration(board[row][column], count)
help_board[row].__setitem__(column, new_value)
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.240s

OK

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

Лидия обнови решението на 31.03.2012 16:03 (преди над 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])
+ 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(board):
+ help_board = create_help_board()
+ make_iteration(board, help_board)
+ set_board_to_help_board(board, help_board)
+
+
+def create_help_board():
+ """Create help board, initialized with zeroes"""
+ return [list(x) for x in [[0] * 50] * 50]
+
+
+def count_living_neighbours(cell, i, j):
+ """Count living neighbours of a cell[i][j]"""
+ count_neighbours = 0
+ for row in range(-1, 2):
+ for column in range(-1, 2):
+ count_neighbours += cell[i + row][j + column]
+ return count_neighbours - cell[i][j]
+
+
+def dead_or_alive_in_next_iteration(cell, count_neighbours):
+ """Decide whether cell will live or die in the next iteration,
+ depending on the number of living neigbours
+ """
+ if (cell == 1) and (count_neighbours < 2 or count_neighbours > 3):
+ cell = 0
+ if cell == 0 and count_neighbours == 3:
+ cell = 1
+ return cell
+
+
+def set_board_to_help_board(board, help_board):
+ """Set the elements of help_board to board elements"""
+ for row in range(1, 49):
+ for column in range(1, 49):
+ value = help_board[row][column]
+ board[row].__setitem__(column, value)
+
+
+def make_iteration(board, help_board):
+ """Make an iteration and change the help_board"""
+ for row in range(1, 49):
+ for column in range(1, 49):
+ count = count_living_neighbours(board, row, column)
+ new_value = dead_or_alive_in_next_iteration(board[row][column], count)
+ help_board[row].__setitem__(column, new_value)
+
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)