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

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

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

Резултати

  • 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 check_conditions(board,row,col):
if board[row][col] == 0:
return False
return True
def iternal_cell(board,row,col):
alive_cells = 0
if check_conditions(board,row-1,col-1):
alive_cells += 1
if check_conditions(board,row-1,col):
alive_cells += 1
if check_conditions(board,row,col-1):
alive_cells += 1
if check_conditions(board,row,col+1):
alive_cells += 1
if check_conditions(board,row+1,col):
alive_cells += 1
if check_conditions(board,row+1,col+1):
alive_cells += 1
if check_conditions(board,row+1,col-1):
alive_cells += 1
if check_conditions(board,row-1,col+1):
alive_cells += 1
return alive_cells
def perambulate_matrix(board):
board_range = range(1,BOARD_SIZE+1,1)
full_range = range(0,BOARD_SIZE+2,1)
alive_cell_holder = [[0]*(BOARD_SIZE+2) for x in full_range]
for row in board_range:
for col in board_range:
alive_cell_holder[row][col] = iternal_cell(board,row,col)
for row in full_range:
for col in full_range:
current_cell = alive_cell_holder[row][col]
if current_cell < 2 or current_cell > 3:
board[row][col] = 0
if current_cell == 3:
board[row][col] = 1
def step_board(board):
perambulate_matrix(board)
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.161s

OK

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

Илия обнови решението на 26.03.2012 18:57 (преди над 8 години)

+#!/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 check_conditions(board,row,col):
+ if board[row][col] == 0:
+ return False
+ return True
+
+def iternal_cell(board,row,col):
+ alive_cells = 0
+ if check_conditions(board,row-1,col-1):
+ alive_cells += 1
+ if check_conditions(board,row-1,col):
+ alive_cells += 1
+ if check_conditions(board,row,col-1):
+ alive_cells += 1
+ if check_conditions(board,row,col+1):
+ alive_cells += 1
+ if check_conditions(board,row+1,col):
+ alive_cells += 1
+ if check_conditions(board,row+1,col+1):
+ alive_cells += 1
+ if check_conditions(board,row+1,col-1):
+ alive_cells += 1
+ if check_conditions(board,row-1,col+1):
+ alive_cells += 1
+ return alive_cells
+
+def perambulate_matrix(board):
+ board_range = range(1,BOARD_SIZE+1,1)
+ full_range = range(0,BOARD_SIZE+2,1)
+ alive_cell_holder = [[0]*(BOARD_SIZE+2) for x in full_range]
+ for row in board_range:
+ for col in board_range:
+ alive_cell_holder[row][col] = iternal_cell(board,row,col)
+ for row in full_range:
+ for col in full_range:
+ current_cell = alive_cell_holder[row][col]
+ if current_cell < 2 or current_cell > 3:
+ board[row][col] = 0
+ if current_cell == 3:
+ board[row][col] = 1
+
+def step_board(board):
+ perambulate_matrix(board)
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)