Решение на Игра на живот от Марин Делчев

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

Към профила на Марин Делчев

Резултати

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

Код

#!/usr/bin/env python3
import os
import time
import copy
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 if you agree to sell me your soul")
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):
new_board = [[0 for x in range(0, BOARD_SIZE + 2)]
for y in range(0, BOARD_SIZE + 2)]
for x in range(1, BOARD_SIZE + 1):
for y in range(1, BOARD_SIZE + 1):
new_board[x][y] = check(x, y, board)
#board=copy.deepcopy(new_board)
for x in range(1, BOARD_SIZE + 1):
for y in range(1, BOARD_SIZE + 1):
board[x][y] = new_board[x][y]
def check(x, y, board):
live_cells_counter = 0
direction_basis = [[1, 1], [1, -1], [0, 1], [1, 0]]
for direction in direction_basis:
if board[x - direction[0]][y - direction[1]]:
live_cells_counter += 1
if board[x + direction[0]][y + direction[1]]:
live_cells_counter += 1
if board[x][y] == 1:
if live_cells_counter > 3 or live_cells_counter < 2:
return 0
else:
return 1
else:
if live_cells_counter == 3:
return 1
else:
return 0
if __name__ == '__main__':
board = create_board()
print_board(board)
while True:
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.184s

OK

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

Марин обнови решението на 02.04.2012 20:06 (преди над 12 години)

+#!/usr/bin/env python3
+import os
+import time
+import copy
+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 if you agree to sell me your soul")
+
+
+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):
+ new_board = [[0 for x in range(0, BOARD_SIZE + 2)]
+ for y in range(0, BOARD_SIZE + 2)]
+ for x in range(1, BOARD_SIZE + 1):
+ for y in range(1, BOARD_SIZE + 1):
+ new_board[x][y] = check(x, y, board)
+ #board=copy.deepcopy(new_board)
+ for x in range(1, BOARD_SIZE + 1):
+ for y in range(1, BOARD_SIZE + 1):
+ board[x][y] = new_board[x][y]
+
+
+def check(x, y, board):
+ live_cells_counter = 0
+ direction_basis = [[1, 1], [1, -1], [0, 1], [1, 0]]
+ for direction in direction_basis:
+ if board[x - direction[0]][y - direction[1]]:
+ live_cells_counter += 1
+ if board[x + direction[0]][y + direction[1]]:
+ live_cells_counter += 1
+ if board[x][y] == 1:
+ if live_cells_counter > 3 or live_cells_counter < 2:
+ return 0
+ else:
+ return 1
+ else:
+ if live_cells_counter == 3:
+ return 1
+ else:
+ return 0
+
+
+if __name__ == '__main__':
+ board = create_board()
+ print_board(board)
+ while True:
+ step_board(board)
+ print_board(board)