Benutzer:Flikkes
Aus ProgrammingWiki
Wiki-Quelltext dieser Seite.
<code>import time
from random import choice
from sys import argv
preMap = []
filename = argv[1]
times = int(argv[2])
rawMap = open(filename, 'r')
for line in rawMap:
preMap.append(line.replace("\n", ""))
positions = ((0, -1), (0, 1), (-1, -1), (-1, 0), (-1, 1), (1, -1), (1, 0), (1, 1))
def processMap(inMap):
retMap = []
l = getLongestLine(inMap)
for line in inMap:
newLine = line
if len(line) < l:
for i in range(0, l - len(line)):
newLine += " "
retMap.append(newLine)
return retMap
def getLongestLine(lines):
longest = 0
for line in lines:
if len(line) > longest:
longest = len(line)
return longest
def live(cell):
if cell != " ":
return True
return False
def dead(cell):
if cell == " ":
return True
return False
def isValidNeighbor(cell):
if cell != " ":
return True
return False
def isInBounds(i, j, iP, jP, inMap):
if i + iP >= 0 and i + iP <= len(inMap) - 1 and j + jP >= 0 and j + jP <= len(inMap[i]) - 1:
return True
return False
def getNeighbors(i, j, inMap):
n = []
for p in positions:
if isInBounds(i, j, p[0], p[1], inMap):
if isValidNeighbor(inMap[i + p[0]][j + p[1]]):
n.append(inMap[i + p[0]][j + p[1]])
return n
gameMap = processMap(preMap)
for line in gameMap:
print(line)
def main(inMap, iterations):
newMap = []
if iterations == 0:
return 0
for i in range(0, len(inMap)):
newLine = ""
for j in range(0, len(inMap[i])):
neighbors = getNeighbors(i, j, inMap)
if len(neighbors) < 2 and live(inMap[i][j]):
newLine += " "
if len(neighbors) < 4 and len(neighbors) >= 2 and live(inMap[i][j]):
newLine += inMap[i][j]
if len(neighbors) >= 4 and live(inMap[i][j]):
newLine += " "
if len(neighbors) == 3 and dead(inMap[i][j]):
newLine += neighbors[choice(range(0, len(neighbors)))]
if len(neighbors) < 3 and dead(inMap[i][j]):
newLine += " "
if len(neighbors) > 3 and dead(inMap[i][j]):
newLine += " "
newMap.append(newLine)
for i in range(0, 5):
print("")
time.sleep(0.5)
for line in newMap:
print(line)
main(processMap(newMap), iterations - 1)
main(gameMap, times)</code>