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

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

Към профила на Антония Пепелянкова

Резултати

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

Код

def step_board(board):
for i, row in enumerate(get_next_board(board)):
for j, col in enumerate(row):
board[i][j] = col
def get_next_board(board):
next_board = []
for i, row in enumerate(board):
next_board_row = []
if i == 0 or i == BOARD_SIZE + 1:
next_board.append(50*[0])
else:
for j, col in enumerate(row):
if j == 0 or j == BOARD_SIZE + 1:
next_board_row.append(0)
else:
next_board_row.append(get_next_cell_state(board, i, j))
next_board.append(next_board_row)
return next_board
def count_alive_neighbours(board, i, j):
alive_neighbours_count = 0
row_start_index = i - 1
row_end_index = i + 2
col_start_index = j - 1
col_end_index = j + 2
if i == 0:
row_start_index = i
if i == BOARD_SIZE + 1:
row_end_index = i + 1
if j == 0:
col_start_index = j
if j == BOARD_SIZE + 1:
col_end_index = j + 1
for row in range(row_start_index, row_end_index):
for col in range(col_start_index, col_end_index):
if (row != i or col != j):
if board[row][col] == 1:
alive_neighbours_count += 1
return alive_neighbours_count
def get_next_cell_state(board, x, y):
neighbours = count_alive_neighbours(board, x, y)
return int((board[x][y] and neighbours == 2) or (neighbours == 3))

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

E
======================================================================
ERROR: test_step_board (__main__.GameOfLifeTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120625-4142-1pnrzvc", line 10, in test_step_board
    self.solution.step_board(board)
  File "/tmp/solution20120625-4142-178zzp8", line 2, in step_board
    for i, row in enumerate(get_next_board(board)):
  File "/tmp/solution20120625-4142-178zzp8", line 10, in get_next_board
    if i == 0 or i == BOARD_SIZE + 1:
NameError: global name 'BOARD_SIZE' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.004s

FAILED (errors=1)

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

Антония обнови решението на 02.04.2012 21:01 (преди над 8 години)

+def step_board(board):
+ for i, row in enumerate(get_next_board(board)):
+ for j, col in enumerate(row):
+ board[i][j] = col
+
+def get_next_board(board):
+ next_board = []
+ for i, row in enumerate(board):
+ next_board_row = []
+ if i == 0 or i == BOARD_SIZE + 1:
+ next_board.append(50*[0])
+ else:
+ for j, col in enumerate(row):
+ if j == 0 or j == BOARD_SIZE + 1:
+ next_board_row.append(0)
+ else:
+ next_board_row.append(get_next_cell_state(board, i, j))
+ next_board.append(next_board_row)
+
+ return next_board
+
+def count_alive_neighbours(board, i, j):
+ alive_neighbours_count = 0
+ row_start_index = i - 1
+ row_end_index = i + 2
+ col_start_index = j - 1
+ col_end_index = j + 2
+ if i == 0:
+ row_start_index = i
+ if i == BOARD_SIZE + 1:
+ row_end_index = i + 1
+ if j == 0:
+ col_start_index = j
+ if j == BOARD_SIZE + 1:
+ col_end_index = j + 1
+
+ for row in range(row_start_index, row_end_index):
+ for col in range(col_start_index, col_end_index):
+ if (row != i or col != j):
+ if board[row][col] == 1:
+ alive_neighbours_count += 1
+
+ return alive_neighbours_count
+
+def get_next_cell_state(board, x, y):
+ neighbours = count_alive_neighbours(board, x, y)
+ return int((board[x][y] and neighbours == 2) or (neighbours == 3))