Решение на Игра на живот от Галин Тодоров

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

Към профила на Галин Тодоров

Резултати

  • 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 get_neighbours_count(x,y,board):
result = board[x][y + 1] + board[x][y - 1] + board[x - 1][y]
result += board[x + 1][y] + board[x + 1][y + 1] + board[x - 1][y + 1]
result += board[x + 1][y - 1] + board[x - 1][y - 1]
return result
def get_neigjbours_count_array(board):
return [[get_neighbours_count(x,y,board)
if x > 0 and y > 0 and x < (BOARD_SIZE + 1)
and y < (BOARD_SIZE + 1)
else 0 for y in range(0,BOARD_SIZE + 2)]
for x in range(0,BOARD_SIZE + 2)]
def get_next_gen_cell(x,y,board,neighbours_count_array):
if board[x][y] == 0 and neighbours_count_array[x][y] == 3 :
board[x][y]=1;
if board[x][y] == 1 and (neighbours_count_array[x][y] > 3
or neighbours_count_array[x][y] < 2):
board[x][y] = 0
def get_new_board(board,neighbours_count_array):
for x in range(0,BOARD_SIZE + 2):
for y in range(0,BOARD_SIZE + 2):
get_next_gen_cell(x,y,board,neighbours_count_array)
def step_board(board):
neighbours_count_array = get_neigjbours_count_array(board)
get_new_board(board,neighbours_count_array)
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.139s

OK

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

Галин обнови решението на 02.04.2012 20:55 (преди над 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 get_neighbours_count(x,y,board):
+ result = board[x][y + 1] + board[x][y - 1] + board[x - 1][y]
+ result += board[x + 1][y] + board[x + 1][y + 1] + board[x - 1][y + 1]
+ result += board[x + 1][y - 1] + board[x - 1][y - 1]
+ return result
+
+def get_neigjbours_count_array(board):
+ return [[get_neighbours_count(x,y,board)
+ if x > 0 and y > 0 and x < (BOARD_SIZE + 1)
+ and y < (BOARD_SIZE + 1)
+ else 0 for y in range(0,BOARD_SIZE + 2)]
+ for x in range(0,BOARD_SIZE + 2)]
+
+def get_next_gen_cell(x,y,board,neighbours_count_array):
+ if board[x][y] == 0 and neighbours_count_array[x][y] == 3 :
+ board[x][y]=1;
+ if board[x][y] == 1 and (neighbours_count_array[x][y] > 3
+ or neighbours_count_array[x][y] < 2):
+ board[x][y] = 0
+
+
+def get_new_board(board,neighbours_count_array):
+ for x in range(0,BOARD_SIZE + 2):
+ for y in range(0,BOARD_SIZE + 2):
+ get_next_gen_cell(x,y,board,neighbours_count_array)
+
+def step_board(board):
+ neighbours_count_array = get_neigjbours_count_array(board)
+ get_new_board(board,neighbours_count_array)
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)