Решение на Игра на живот от Петко Пъдевски

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

Към профила на Петко Пъдевски

Резултати

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

Код

'''
Created on Mar 22, 2012
@author: ppadevski
'''
#!/usr/bin/env python3
import os
import time
from random import Random
BOARD_SIZE = 48
def print_board(board):
'''
Prints the board representing the universe of the Game of Life.
@param board The board representing the universe of the Game of Life.
@type board C{list} of C{list} of C{int}
'''
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])
def create_cell(fill_rate, rand, x, y):
'''
Creates a single cell depending on it's coordinates.
@param fill_rate Used to determine iff the cell should be alive or dead.
@type fill_rate C{float}
@param rand A random number generator
@type L{Random}
@param x The x coordinate of the cell in the board
@type x C{int}
@param y The y coordinate of the cell in the board
@type y C{int}
@return The state of the cell after the creation - 0(dead) or 1(alive).
@rtype C{int}
'''
return int(x and x != BOARD_SIZE + 1
and y and y != BOARD_SIZE + 1
and rand.normalvariate(0.5, 0.2) < fill_rate)
def create_board(fill_rate=0.3):
'''
Creates a random board with 50x50 size in which the first line/column and
the last line/column are all dead cells and the rest are random generated.
@param fill_rate Used to determine iff a cell should be alive or dead.
@type fill_rate C{float}
@return The generated board
@rtype C{list} of C{list} of C{int}
'''
rand = Random()
board = []
for x in range(0, BOARD_SIZE + 2):
row = [create_cell(fill_rate, rand, x, y)
for y in range(0, BOARD_SIZE + 2)]
board.append(row)
return board
def evolve_cell(board, x, y):
'''
Evolves a single cell in the board with coordinates (x, y).
@note The rules of the game are described in wikipedia.
@param board The board representing the universe of the Game of Life.
@type board C{list} of C{list} of C{int}
@param x The x coordinate of the cell.
@type x C{int}
@param y The y coordinate of the cell.
@type y C{int}
@return The state of the cell after the evolution - 0(dead) or 1(alive).
@rtype C{int}
'''
evolution_rate = sum([board[i][j] & 1 for i in range(x - 1, x + 2)
for j in range(y - 1, y + 2)])
evolution_rate -= board[x][y]
if evolution_rate == 3 or (board[x][y] & 1 and evolution_rate == 2):
return 1
return 0
def step_board(board):
'''
Performs a one step evolution to the entire board.
@param board The board representing the universe of the Game of Life.
@type board C{list} of C{list} of C{int}
@attention This method modifies the board!
'''
evolved_board = []
for x in range(1, BOARD_SIZE + 1):
row = [0, 0]
row[1:-1] = [evolve_cell(board, x, y)
for y in range(1, BOARD_SIZE + 1)]
evolved_board.append(row)
board[1:-1] = evolved_board[:]
if __name__ == '__main__':
board = create_board()
while True:
print_board(board)
step_board(board)
print_board(board)

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

.
----------------------------------------------------------------------
Ran 1 test in 0.268s

OK

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

Петко обнови решението на 27.03.2012 23:09 (преди над 12 години)

+'''
+Created on Mar 22, 2012
+
+@author: ppadevski
+'''
+#!/usr/bin/env python3
+import os
+import time
+from random import Random
+
+BOARD_SIZE = 48
+
+
+def print_board(board):
+ '''
+ Prints the board representing the universe of the Game of Life.
+
+ @param board The board representing the universe of the Game of Life.
+ @type board C{list} of C{list} of C{int}
+ '''
+ 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])
+
+
+def create_cell(fill_rate, rand, x, y):
+ '''
+ Creates a single cell depending on it's coordinates.
+
+ @param fill_rate Used to determine iff the cell should be alive or dead.
+ @type fill_rate C{float}
+ @param rand A random number generator
+ @type L{Random}
+ @param x The x coordinate of the cell in the board
+ @type x C{int}
+ @param y The y coordinate of the cell in the board
+ @type y C{int}
+
+ @return The state of the cell after the creation - 0(dead) or 1(alive).
+ @rtype C{int}
+ '''
+ return int(x and x != BOARD_SIZE + 1
+ and y and y != BOARD_SIZE + 1
+ and rand.normalvariate(0.5, 0.2) < fill_rate)
+
+
+def create_board(fill_rate=0.3):
+ '''
+ Creates a random board with 50x50 size in which the first line/column and
+ the last line/column are all dead cells and the rest are random generated.
+
+ @param fill_rate Used to determine iff a cell should be alive or dead.
+ @type fill_rate C{float}
+
+ @return The generated board
+ @rtype C{list} of C{list} of C{int}
+ '''
+ rand = Random()
+ board = []
+
+ for x in range(0, BOARD_SIZE + 2):
+ row = [create_cell(fill_rate, rand, x, y)
+ for y in range(0, BOARD_SIZE + 2)]
+ board.append(row)
+
+ return board
+
+
+def evolve_cell(board, x, y):
+ '''
+ Evolves a single cell in the board with coordinates (x, y).
+ @note The rules of the game are described in wikipedia.
+
+ @param board The board representing the universe of the Game of Life.
+ @type board C{list} of C{list} of C{int}
+ @param x The x coordinate of the cell.
+ @type x C{int}
+ @param y The y coordinate of the cell.
+ @type y C{int}
+
+ @return The state of the cell after the evolution - 0(dead) or 1(alive).
+ @rtype C{int}
+ '''
+ evolution_rate = sum([board[i][j] & 1 for i in range(x - 1, x + 2)
+ for j in range(y - 1, y + 2)])
+ evolution_rate -= board[x][y]
+
+ if evolution_rate == 3 or (board[x][y] & 1 and evolution_rate == 2):
+ return 1
+ return 0
+
+
+def step_board(board):
+ '''
+ Performs a one step evolution to the entire board.
+
+ @param board The board representing the universe of the Game of Life.
+ @type board C{list} of C{list} of C{int}
+
+ @attention This method modifies the board!
+ '''
+ evolved_board = []
+
+ for x in range(1, BOARD_SIZE + 1):
+ row = [0, 0]
+ row[1:-1] = [evolve_cell(board, x, y)
+ for y in range(1, BOARD_SIZE + 1)]
+ evolved_board.append(row)
+
+ board[1:-1] = evolved_board[:]
+
+
+if __name__ == '__main__':
+ board = create_board()
+ while True:
+ print_board(board)
+ step_board(board)
+ print_board(board)