# Решение на Игра на живот от Христо Георгиев

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

Към профила на Христо Георгиев

## Резултати

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

## Код

#!/usr/bin/env python3
import os
import time
from random import Random
BOARD_SIZE = 48
MIN_NEIGHBOURS = 2
MAX_NEIGHBOURS = 3
ALIVE = 1
directions = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
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):
aliveNeighbours = [[]]
#populating the alive neighbours count lists
for y in range(1,BOARD_SIZE+1):
aliveNeighbours.append([])
aliveNeighbours[y].append(0)
for x in range(1,BOARD_SIZE+1):
aliveNeighbours[y].append(0)
for dir in directions:
if board[y+dir[0]][x+dir[1]] == ALIVE:
aliveNeighbours[y][x]=aliveNeighbours[y][x]+1
#decide who lives in the next round
for y in range(1,BOARD_SIZE+1):
for x in range(1,BOARD_SIZE+1):
if board[y][x] == ALIVE:
if MIN_NEIGHBOURS > aliveNeighbours[y][x] or aliveNeighbours[y][x] > MAX_NEIGHBOURS:
if aliveNeighbours[y][x] == MAX_NEIGHBOURS:
board[y][x] = ALIVE
if __name__ == '__main__':
board = create_board()
#Note: here we should also display the board - this is the real beginning of the game.
while True:
step_board(board)
print_board(board)

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

Traceback (most recent call last):
File "/tmp/test20120625-4142-kp6bvb", line 43, in <module>
GameOfLifeTestCase.main()
File "/data/rails/pyfmi-2012/releases/20120501152555/python/homework.py", line 27, in main
rude.solution = module or imp.load_source('solution', sys.argv[1])
File "/tmp/solution20120625-4142-qgaeo6", line 46
^
SyntaxError: invalid syntax

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

### Христо обнови решението на 01.04.2012 10:35 (преди над 8 години)

+#!/usr/bin/env python3
+import os
+import time
+from random import Random
+
+BOARD_SIZE = 48
+MIN_NEIGHBOURS = 2
+MAX_NEIGHBOURS = 3
+ALIVE = 1
+
+directions = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
+
+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):
+ aliveNeighbours = [[]]
+ #populating the alive neighbours count lists
+ for y in range(1,BOARD_SIZE+1):
+ aliveNeighbours.append([])
+ aliveNeighbours[y].append(0)
+ for x in range(1,BOARD_SIZE+1):
+ aliveNeighbours[y].append(0)
+ for dir in directions:
+ if board[y+dir[0]][x+dir[1]] == ALIVE:
+ aliveNeighbours[y][x]=aliveNeighbours[y][x]+1
+
+ #decide who lives in the next round
+ for y in range(1,BOARD_SIZE+1):
+ for x in range(1,BOARD_SIZE+1):
+ if board[y][x] == ALIVE:
+ if MIN_NEIGHBOURS > aliveNeighbours[y][x] or aliveNeighbours[y][x] > MAX_NEIGHBOURS: