Решение на Игра на живот от Габриела Цанова

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

Към профила на Габриела Цанова

Резултати

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

Код

#!/usr/bin/env python3
import os
import time
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 is_lonely(neighbours):
return neighbours < 2
def is_overcrowded(neighbours):
return neighbours > 3
def neighbours_count(x, y, board):
neighbours = 0
for row in board[y - 1:y + 2]:
for cell in row[x - 1:x + 2]:
neighbours += cell
return neighbours - board[y][x]
def should_spawn(x, y, board):
neighbours = neighbours_count(x, y, board)
return neighbours == 3
def should_die(x, y, board):
neighbours = neighbours_count(x, y, board)
return is_lonely(neighbours) or is_overcrowded(neighbours)
def spawn(x, y, board):
board[y][x] = 1
def die(x, y, board):
board[y][x] = 0
def step_board(board):
old_board = deepcopy(board)
pos_y = 1
for row in old_board[1:-1]:
pos_x = 1
for cell in row[1:-1]:
if cell and should_die(pos_x, pos_y, old_board):
die(pos_x, pos_y, board)
else:
if should_spawn(pos_x, pos_y, old_board):
spawn(pos_x, pos_y, board)
pos_x += 1
pos_y += 1
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

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

OK

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

Габриела обнови решението на 02.04.2012 02:02 (преди над 8 години)

+#!/usr/bin/env python3
+import os
+import time
+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 is_lonely(neighbours):
+ return neighbours < 2
+
+
+def is_overcrowded(neighbours):
+ return neighbours > 3
+
+
+def neighbours_count(x, y, board):
+ neighbours = 0
+ for row in board[y - 1:y + 2]:
+ for cell in row[x - 1:x + 2]:
+ neighbours += cell
+ return neighbours - board[y][x]
+
+
+def should_spawn(x, y, board):
+ neighbours = neighbours_count(x, y, board)
+ return neighbours == 3
+
+
+def should_die(x, y, board):
+ neighbours = neighbours_count(x, y, board)
+ return is_lonely(neighbours) or is_overcrowded(neighbours)
+
+
+def spawn(x, y, board):
+ board[y][x] = 1
+
+
+def die(x, y, board):
+ board[y][x] = 0
+
+
+def step_board(board):
+ old_board = deepcopy(board)
+
+ pos_y = 1
+ for row in old_board[1:-1]:
+ pos_x = 1
+ for cell in row[1:-1]:
+ if cell and should_die(pos_x, pos_y, old_board):
+ die(pos_x, pos_y, board)
+ else:
+ if should_spawn(pos_x, pos_y, old_board):
+ spawn(pos_x, pos_y, board)
+ pos_x += 1
+ pos_y += 1
+
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)