# Решение на Игра на живот от Владислав Вълчев

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

Към профила на Владислав Вълчев

## Резултати

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

## Код

#!/usr/bin/env python3
import copy
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 up_neigh(board, i_coord, j_coord):
return board[i_coord - 1][j_coord]
def down_neigh(board, i_coord, j_coord):
return board[i_coord + 1][j_coord]
def right_neigh(board, i_coord, j_coord):
return board[i_coord][j_coord + 1]
def left_neigh(board, i_coord, j_coord):
return board[i_coord][j_coord - 1]
def up_right_neigh(board, i_coord, j_coord):
return board[i_coord - 1][j_coord + 1]
def up_left_neigh(board, i_coord, j_coord):
return board[i_coord - 1][j_coord - 1]
def down_right_neigh(board, i_coord, j_coord):
return board[i_coord + 1][j_coord + 1]
def down_left_neigh(board, i_coord, j_coord):
return board[i_coord + 1][j_coord - 1]
def check_neighbours(board, i_coord, j_coord):
'''
Checks surrounding alive neighbours
@param board: current matrix
@param i_coord: row number of the current cell
@param j_coord: column number of the current cell
'''
cell = board[i_coord][j_coord]
neigh_1 = up_neigh(board, i_coord, j_coord)
neigh_2 = down_neigh(board, i_coord, j_coord)
neigh_3 = right_neigh(board, i_coord, j_coord)
neigh_4 = left_neigh(board, i_coord, j_coord)
neigh_5 = up_right_neigh(board, i_coord, j_coord)
neigh_6 = up_left_neigh(board, i_coord, j_coord)
neigh_7 = down_right_neigh(board, i_coord, j_coord)
neigh_8 = down_left_neigh(board, i_coord, j_coord)
sum_1 = neigh_1 + neigh_2 + neigh_3 + neigh_4
sum_2 = neigh_5 + neigh_6 + neigh_7 + neigh_8
# The sum of all neighbours gives how many
# alive neighbours there are
sum = sum_1 + sum_2
# Checking the rules
if cell:
if sum < 2:
return 0
if sum == 2 or sum == 3:
return 1
if sum > 3:
return 0
else:
if sum == 3:
return 1
else:
return cell
def step_board(board):
new_board = copy.deepcopy(board)
for i in range(1, BOARD_SIZE + 1):
for j in range(1, BOARD_SIZE + 1):
new_board[i][j] = check_neighbours(board, i, j)
for ii in range(0, BOARD_SIZE + 2):
for jj in range(0, BOARD_SIZE +2):
board[ii][jj] = new_board[ii][jj]
return board
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.185s

OK

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

### Владислав обнови решението на 26.03.2012 23:44 (преди над 8 години)

+#!/usr/bin/env python3
+import copy
+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 up_neigh(board, i_coord, j_coord):
+ return board[i_coord - 1][j_coord]
+
+def down_neigh(board, i_coord, j_coord):
+ return board[i_coord + 1][j_coord]
+
+def right_neigh(board, i_coord, j_coord):
+ return board[i_coord][j_coord + 1]
+
+def left_neigh(board, i_coord, j_coord):
+ return board[i_coord][j_coord - 1]
+
+def up_right_neigh(board, i_coord, j_coord):
+ return board[i_coord - 1][j_coord + 1]
+
+def up_left_neigh(board, i_coord, j_coord):
+ return board[i_coord - 1][j_coord - 1]
+
+def down_right_neigh(board, i_coord, j_coord):
+ return board[i_coord + 1][j_coord + 1]
+
+def down_left_neigh(board, i_coord, j_coord):
+ return board[i_coord + 1][j_coord - 1]
+
+def check_neighbours(board, i_coord, j_coord):
+ '''
+ Checks surrounding alive neighbours
+
+ @param board: current matrix
+ @param i_coord: row number of the current cell
+ @param j_coord: column number of the current cell
+ '''
+ cell = board[i_coord][j_coord]
+
+ neigh_1 = up_neigh(board, i_coord, j_coord)
+ neigh_2 = down_neigh(board, i_coord, j_coord)
+ neigh_3 = right_neigh(board, i_coord, j_coord)
+ neigh_4 = left_neigh(board, i_coord, j_coord)
+ neigh_5 = up_right_neigh(board, i_coord, j_coord)
+ neigh_6 = up_left_neigh(board, i_coord, j_coord)
+ neigh_7 = down_right_neigh(board, i_coord, j_coord)
+ neigh_8 = down_left_neigh(board, i_coord, j_coord)
+
+ sum_1 = neigh_1 + neigh_2 + neigh_3 + neigh_4
+ sum_2 = neigh_5 + neigh_6 + neigh_7 + neigh_8
+
+ # The sum of all neighbours gives how many
+ # alive neighbours there are
+ sum = sum_1 + sum_2
+
+ # Checking the rules
+ if cell:
+ if sum < 2:
+ return 0
+ if sum == 2 or sum == 3:
+ return 1
+ if sum > 3:
+ return 0
+ else:
+ if sum == 3:
+ return 1
+ else:
+ return cell
+
+def step_board(board):
+ new_board = copy.deepcopy(board)
+ for i in range(1, BOARD_SIZE + 1):
+ for j in range(1, BOARD_SIZE + 1):
+ new_board[i][j] = check_neighbours(board, i, j)
+ for ii in range(0, BOARD_SIZE + 2):
+ for jj in range(0, BOARD_SIZE +2):
+ board[ii][jj] = new_board[ii][jj]
+ return board
+
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ step_board(board)
+ print_board(board)