Parent Directory
|
Revision Log
Gave life to the repository
from time import sleep
import sys
EMPTY = ' '
POPULATED = 'o'
width = 80
height = 23
def showBoard(board, verbose=False):
str = ''
for x in range(len(board)):
for y in range(len(board[x])):
if not verbose:
sys.stdout.write(board[x][y])
else:
print '(', x, y, ')', repr(board[x][y])
def init(width, height):
board = []
for x in range(height):
board.append([])
for y in range(width):
board[x].append(EMPTY)
return board
def neighbors(board, x, y):
count = 0
for x in range(x - 1, x + 1):
for y in range(y - 1, y + 1):
if not x < 0 and not y < 0:
if board[x][y] == POPULATED:
count += 1
return count - 1
def update(board):
for x in range(len(board)):
for y in range(len(board[x])):
count = 0
cell = board[x][y]
count = neighbors(board, x, y)
if cell == EMPTY and count == 3:
board[x][y] = POPULATED
if cell == POPULATED and count < 2:
board[x][y] = EMPTY
if cell == POPULATED and count > 3:
board[x][y] = EMPTY
def prompt(board):
prompting = True
while prompting:
sys.stdout.write('life> ')
line = sys.stdin.readline()
line = line[:-1]
args = line.split(' ')
cmd = args[0]
if len(args) > 2:
x = args[1]
y = args[2]
xi = int(x)
yi = int(y)
if cmd == 'peek':
print '(%(x)s, %(y)s): %(val)s' % {'x': x, 'y': y, 'val': board[xi][yi]}
if cmd == 'pop':
board[xi][yi] = POPULATED
if cmd == 'kill':
board[xi][yi] = EMPTY
if cmd == 'neighbors':
print neighbors(board, xi, yi)
if cmd == 'clear':
board = init(len(board), len(board[x]))
if cmd == 'go':
prompting = False
if cmd == 'next':
update(board)
if cmd == 'show':
showBoard(board)
if cmd == 'showv':
showBoard(board, verbose=True)
if cmd == 'help':
print '''peek x y:\tshow the value at location x y
pop x y:\tpopulate the value at location x y
kill x y:\tkill the organism at location x y
neighbors x y:\tshow the number of neighbors at x y
clear:\twipe out the board
go:\tresume execution
next:\titerate the board once
show:\tshow the current state of the board
showv:\tshow the current state of the board with coordinate listings
help:\tthis text'''
def main(board):
count = 0
while True:
count += 1
showBoard(board)
print 'Generation', count
update(board)
sleep(1)
board = init(width, height)
board[0][0] = POPULATED
board[0][1] = POPULATED
while True:
try:
main(board)
except:
prompt(board)
| synack at csh.rit.edu | ViewVC Help |
| Powered by ViewVC 1.0.0 |