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

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

Към профила на Ивелин Славов

Резултати

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

Код

#!/usr/bin/env python3
import os
import time
from random import Random
from itertools import chain
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):
for row_idx, row in enumerate(board):
for cell_idx, cell in enumerate(row):
if cell_idx in [BOARD_SIZE + 1, 0] or row_idx in [BOARD_SIZE + 1, 0]:
continue
alive = get_neighbors(row_idx, cell_idx, board)
if cell == 1:
if (alive - 1) not in [2, 3]:
row[cell_idx] = -1
elif cell == 0 and alive == 3:
row[cell_idx] = 2

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

E
======================================================================
ERROR: test_step_board (__main__.GameOfLifeTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120625-4142-h4le6o", line 10, in test_step_board
    self.solution.step_board(board)
  File "/tmp/solution20120625-4142-hdfdcs", line 29, in step_board
    alive = get_neighbors(row_idx, cell_idx, board)
NameError: global name 'get_neighbors' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.005s

FAILED (errors=1)

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

Ивелин обнови решението на 27.03.2012 11:02 (преди почти 13 години)

+#!/usr/bin/env python3
+import os
+import time
+from random import Random
+from itertools import chain
+
+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):
+ for row_idx, row in enumerate(board):
+ for cell_idx, cell in enumerate(row):
+ if cell_idx in [BOARD_SIZE + 1, 0] or row_idx in [BOARD_SIZE + 1, 0]:
+ continue
+
+ alive = get_neighbors(row_idx, cell_idx, board)
+ if cell == 1:
+ if (alive - 1) not in [2, 3]:
+ row[cell_idx] = -1
+ elif cell == 0 and alive == 3:
+ row[cell_idx] = 2