|
|
@ -1,10 +1,9 @@ |
|
|
|
import sys |
|
|
|
|
|
|
|
import ast |
|
|
|
import ast |
|
|
|
import time as t |
|
|
|
|
|
|
|
import random as r |
|
|
|
import random as r |
|
|
|
import blup.animation |
|
|
|
from blup import frame |
|
|
|
import blup.frame |
|
|
|
from blup import animation |
|
|
|
import blup.writebml |
|
|
|
import blup.writebml |
|
|
|
|
|
|
|
import argparse |
|
|
|
|
|
|
|
|
|
|
|
r.seed() |
|
|
|
r.seed() |
|
|
|
|
|
|
|
|
|
|
@ -16,81 +15,89 @@ COLORS = 3 |
|
|
|
DELAY = 300 |
|
|
|
DELAY = 300 |
|
|
|
|
|
|
|
|
|
|
|
DEAD = 0 |
|
|
|
DEAD = 0 |
|
|
|
DEADCOLOR = (0,0,0) |
|
|
|
DEAD_COLOR = (0, 0, 0) |
|
|
|
ALIVE = 1 |
|
|
|
ALIVE = 1 |
|
|
|
ALIVECOLOR = (0,0,255) |
|
|
|
ALIVE_COLOR = (0, 0, 255) |
|
|
|
|
|
|
|
|
|
|
|
dimension = blup.frame.FrameDimension(WIDTH, HEIGHT, COLOR_DEPTH, COLORS) |
|
|
|
dimension = frame.FrameDimension(WIDTH, HEIGHT, COLOR_DEPTH, COLORS) |
|
|
|
animation = blup.animation.Animation(dimension) |
|
|
|
game_of_life_animation = animation.Animation(dimension) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_rand_game_board(): |
|
|
|
|
|
|
|
board = [] |
|
|
|
|
|
|
|
for rows in range(HEIGHT): |
|
|
|
|
|
|
|
x_coordinates = [] |
|
|
|
|
|
|
|
for cols in range(WIDTH): |
|
|
|
|
|
|
|
x_coordinates.append(r.randint(0, 1)) |
|
|
|
|
|
|
|
board.append(x_coordinates) |
|
|
|
|
|
|
|
return board |
|
|
|
|
|
|
|
|
|
|
|
def generateRandomGameBoard(): |
|
|
|
|
|
|
|
gameBoard = [] |
|
|
|
|
|
|
|
for i in range(HEIGHT): |
|
|
|
|
|
|
|
xCoordinates = [] |
|
|
|
|
|
|
|
for j in range(WIDTH): |
|
|
|
|
|
|
|
xCoordinates.append(r.randint(0, 1)) |
|
|
|
|
|
|
|
gameBoard.append(xCoordinates) |
|
|
|
|
|
|
|
return gameBoard |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_neighbors(x, y): |
|
|
|
def get_neighbors(x, y): |
|
|
|
for rowNumber in range(y-1, y+2): |
|
|
|
for rowNumber in range(y-1, y+2): |
|
|
|
if 0 <= rowNumber < HEIGHT: |
|
|
|
if 0 <= rowNumber < HEIGHT: |
|
|
|
for colNumber in range(x-1, x+2): |
|
|
|
for colNumber in range(x-1, x+2): |
|
|
|
if 0 <= colNumber < WIDTH: |
|
|
|
if 0 <= colNumber < WIDTH: |
|
|
|
if not (y==rowNumber and x==colNumber): |
|
|
|
if not (y == rowNumber and x == colNumber): |
|
|
|
yield gameBoard[rowNumber][colNumber] |
|
|
|
yield game_board[rowNumber][colNumber] |
|
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) == 1: |
|
|
|
|
|
|
|
gameBoard = generateRandomGameBoard() |
|
|
|
|
|
|
|
gameBoardIsSanitized = True |
|
|
|
|
|
|
|
elif len(sys.argv) != 2: |
|
|
|
|
|
|
|
print("check args. found "+str(len(sys.argv))) |
|
|
|
|
|
|
|
exit() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not gameBoardIsSanitized: |
|
|
|
if __name__ == "__main__": |
|
|
|
gameBoard = ast.literal_eval(sys.argv[1]) |
|
|
|
argument_parser = argparse.ArgumentParser(description="Game of Life bml generator") |
|
|
|
|
|
|
|
argument_parser.add_argument('-w', dest="world", type=list, default=None, help="pre-constructed world (optional)") |
|
|
|
|
|
|
|
argument_parser.add_argument('-o', dest="output_path", type=str, help="output file (mandatory)") |
|
|
|
|
|
|
|
args = argument_parser.parse_args() |
|
|
|
|
|
|
|
|
|
|
|
if len(gameBoard) != HEIGHT: |
|
|
|
if args.output_path is None: |
|
|
|
print("len(gameBoard) != "+str(HEIGHT)+". counted "+str(len(gameBoard))) |
|
|
|
argument_parser.print_help() |
|
|
|
exit() |
|
|
|
exit() |
|
|
|
|
|
|
|
|
|
|
|
for i in gameBoard: |
|
|
|
if args.world is None: |
|
|
|
if len(i) != WIDTH: |
|
|
|
game_board = generate_rand_game_board() |
|
|
|
print("len(rows) != "+str(WIDTH)+". counted "+str(len(i))) |
|
|
|
else: |
|
|
|
|
|
|
|
game_board = ast.literal_eval(args.world) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(game_board) != HEIGHT: |
|
|
|
|
|
|
|
print("len(gameBoard) != " + str(HEIGHT) + ". counted " + str(len(game_board))) |
|
|
|
exit() |
|
|
|
exit() |
|
|
|
|
|
|
|
|
|
|
|
for frame in range(500): |
|
|
|
for i in game_board: |
|
|
|
# construct frame |
|
|
|
if len(i) != WIDTH: |
|
|
|
newframe = blup.animation.AnimationFrame(dimension, DELAY) |
|
|
|
print("len(rows) != "+str(WIDTH)+". counted "+str(len(i))) |
|
|
|
for rowIndex in range(HEIGHT): |
|
|
|
exit() |
|
|
|
for colIndex in range(WIDTH): |
|
|
|
|
|
|
|
cellStatus = gameBoard[rowIndex][colIndex] |
|
|
|
for frame in range(500): |
|
|
|
if cellStatus == ALIVE: |
|
|
|
# construct frame |
|
|
|
newframe.setPixel(colIndex, rowIndex, ALIVECOLOR) |
|
|
|
new_frame = animation.AnimationFrame(dimension, DELAY) |
|
|
|
else: |
|
|
|
for row_index in range(HEIGHT): |
|
|
|
newframe.setPixel(colIndex, rowIndex, DEADCOLOR) |
|
|
|
for col_index in range(WIDTH): |
|
|
|
animation.addFrame(newframe) |
|
|
|
cell_status = game_board[row_index][col_index] |
|
|
|
|
|
|
|
if cell_status == ALIVE: |
|
|
|
# calculate next step |
|
|
|
new_frame.setPixel(col_index, row_index, ALIVE_COLOR) |
|
|
|
gameBoardNextStep = [] |
|
|
|
else: |
|
|
|
lastGameBoard = gameBoard |
|
|
|
new_frame.setPixel(col_index, row_index, DEAD_COLOR) |
|
|
|
for rowIndex in range(HEIGHT): |
|
|
|
game_of_life_animation.addFrame(new_frame) |
|
|
|
newRow = [] |
|
|
|
|
|
|
|
for colIndex in range(WIDTH): |
|
|
|
# calculate next step |
|
|
|
neighborList = list(get_neighbors(colIndex, rowIndex)) |
|
|
|
next_game_board_step = [] |
|
|
|
aliveNeighbors = neighborList.count(1) |
|
|
|
previous_game_board_step = game_board |
|
|
|
if aliveNeighbors < 2: |
|
|
|
for row_index in range(HEIGHT): |
|
|
|
newRow.append(DEAD) |
|
|
|
new_row = [] |
|
|
|
elif aliveNeighbors == 2: |
|
|
|
for col_index in range(WIDTH): |
|
|
|
newRow.append(gameBoard[rowIndex][colIndex]) |
|
|
|
neighbor_list = list(get_neighbors(col_index, row_index)) |
|
|
|
elif aliveNeighbors == 3: |
|
|
|
alive_neighbors = neighbor_list.count(1) |
|
|
|
newRow.append(ALIVE) |
|
|
|
if alive_neighbors < 2: |
|
|
|
elif aliveNeighbors >= 4: |
|
|
|
new_row.append(DEAD) |
|
|
|
newRow.append(DEAD) |
|
|
|
elif alive_neighbors == 2: |
|
|
|
gameBoardNextStep.append(newRow) |
|
|
|
new_row.append(game_board[row_index][col_index]) |
|
|
|
gameBoard = gameBoardNextStep |
|
|
|
elif alive_neighbors == 3: |
|
|
|
if(gameBoard == lastGameBoard): |
|
|
|
new_row.append(ALIVE) |
|
|
|
for i in range(20): |
|
|
|
elif alive_neighbors >= 4: |
|
|
|
animation.addFrame(newframe) |
|
|
|
new_row.append(DEAD) |
|
|
|
break |
|
|
|
next_game_board_step.append(new_row) |
|
|
|
blup.writebml.writeBml(animation, '/home/pi/animations/gameOfLife.bml') |
|
|
|
game_board = next_game_board_step |
|
|
|
|
|
|
|
if game_board == previous_game_board_step: |
|
|
|
|
|
|
|
for i in range(20): |
|
|
|
|
|
|
|
game_of_life_animation.addFrame(new_frame) |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
blup.writebml.writeBml(game_of_life_animation, args.output_path) |
|
|
|