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

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

Към профила на Илиян Киряков

Резултати

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

Код

#!/usr/bin/env python3
import os
import time
from random import Random
BOARD_SIZE = 48
new_x = 0
new_y = 0
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):
copy_board = [[0 for x in range(0, BOARD_SIZE + 2)]
for y in range(0, BOARD_SIZE + 2)]
for x in range(1, BOARD_SIZE + 1):
for y in range(1, BOARD_SIZE + 1):
copy_board[x][y] = board[x][y]
for x in range(1, BOARD_SIZE + 1):
for y in range(1, BOARD_SIZE + 1):
new_x = x
new_y = y
if copy_board[x][y] == 1:
live_cell(copy_board, board, x, y)
elif copy_board[x][y] == 0:
dead_cell(copy_board, board, x, y)
def live_cell(copy_board, board, new_x, new_y):
if ((copy_board[new_x + 1][new_y] +
copy_board[new_x - 1][new_y] +
copy_board[new_x][new_y + 1] +
copy_board[new_x][new_y - 1] +
copy_board[new_x + 1][new_y - 1] +
copy_board[new_x + 1][new_y + 1] +
copy_board[new_x - 1][new_y - 1] +
copy_board[new_x - 1][new_y + 1]) < 2
or (copy_board[new_x + 1][new_y] +
copy_board[new_x - 1][new_y] +
copy_board[new_x][new_y + 1] +
copy_board[new_x][new_y - 1] +
copy_board[new_x + 1][new_y - 1] +
copy_board[new_x + 1][new_y + 1] +
copy_board[new_x - 1][new_y - 1] +
copy_board[new_x - 1][new_y + 1]) > 3):
board[new_x][new_y] = 0
def dead_cell(copy_board, board, new_x, new_y):
if (copy_board[new_x + 1][new_y] +
copy_board[new_x - 1][new_y] +
copy_board[new_x][new_y + 1] +
copy_board[new_x][new_y - 1] +
copy_board[new_x + 1][new_y - 1] +
copy_board[new_x + 1][new_y + 1] +
copy_board[new_x - 1][new_y - 1] +
copy_board[new_x - 1][new_y + 1]) == 3:
board[new_x][new_y] = 1
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.121s

OK

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

Илиян обнови решението на 01.04.2012 01:02 (преди над 8 години)

+#!/usr/bin/env python3
+import os
+import time
+from random import Random
+
+BOARD_SIZE = 48
+new_x = 0
+new_y = 0
+
+
+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):
+ copy_board = [[0 for x in range(0, BOARD_SIZE + 2)]
+ for y in range(0, BOARD_SIZE + 2)]
+ for x in range(1, BOARD_SIZE + 1):
+ for y in range(1, BOARD_SIZE + 1):
+ copy_board[x][y] = board[x][y]
+
+ for x in range(1, BOARD_SIZE + 1):
+ for y in range(1, BOARD_SIZE + 1):
+ new_x = x
+ new_y = y
+ if copy_board[x][y] == 1:
+ live_cell(copy_board, board, x, y)
+ elif copy_board[x][y] == 0:
+ dead_cell(copy_board, board, x, y)
+
+
+def live_cell(copy_board, board, new_x, new_y):
+ if ((copy_board[new_x + 1][new_y] +
+ copy_board[new_x - 1][new_y] +
+ copy_board[new_x][new_y + 1] +
+ copy_board[new_x][new_y - 1] +
+ copy_board[new_x + 1][new_y - 1] +
+ copy_board[new_x + 1][new_y + 1] +
+ copy_board[new_x - 1][new_y - 1] +
+ copy_board[new_x - 1][new_y + 1]) < 2
+
+ or (copy_board[new_x + 1][new_y] +
+ copy_board[new_x - 1][new_y] +
+ copy_board[new_x][new_y + 1] +
+ copy_board[new_x][new_y - 1] +
+ copy_board[new_x + 1][new_y - 1] +
+ copy_board[new_x + 1][new_y + 1] +
+ copy_board[new_x - 1][new_y - 1] +
+ copy_board[new_x - 1][new_y + 1]) > 3):
+ board[new_x][new_y] = 0
+
+
+def dead_cell(copy_board, board, new_x, new_y):
+ if (copy_board[new_x + 1][new_y] +
+ copy_board[new_x - 1][new_y] +
+ copy_board[new_x][new_y + 1] +
+ copy_board[new_x][new_y - 1] +
+ copy_board[new_x + 1][new_y - 1] +
+ copy_board[new_x + 1][new_y + 1] +
+ copy_board[new_x - 1][new_y - 1] +
+ copy_board[new_x - 1][new_y + 1]) == 3:
+ board[new_x][new_y] = 1
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)