Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2015-12-20T10:04:34+00:00 → 2015-12-20T10:04:34+00:00
=Eigene Projekte= | |
==Game of (text) Life - Source== | |
<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> | |
==Angewandt auf sich selbst== | |
<code>i t ee moor hioc | |
choic | |
f m s v oii | |
y ro rv | |
p = []rar[11 | |
m r' | |
me r' | |
f ee"nnn )) | |
, ) 1 ) 1, 1 ( -, ( , ( ) | |
osi = - (-1 ) -1 ) 1 1) 1 ) 1 ) | |
oiiitio 0, )1 , ) (, ), 1, 0 11 1 ( -) 1 , ( ) | |
d rrcessaa(((Mnpp: | |
e pp M inMap) | |
e (MMa) | |
f (inMap | |
f r nMaMp | |
f | |
i (( n((ilee)) | |
l 0,, en(line)) | |
rM " nn(ine)): | |
re n ) | |
etu en(d(nwinn) | |
ttuu rtMM | |
f gtoLgessLLLie(liiss) | |
e fg s Line(lines) | |
f e: | |
f :> nnnst: | |
o ii : | |
rt ==l ) | |
etur l s een(nen | |
teun ooget | |
e ivecelel) | |
e ll : | |
f ii " : | |
e | |
etu e u | |
euuu ale | |
f adac(le)) | |
e id : | |
f fi " : | |
e | |
etu e r | |
retr Fss | |
d VsaadNNieghbbo(ecell | |
e fi h (cell) | |
e ii ele)l | |
e | |
etu e e | |
ttun Fll | |
d InIBuuns(i, i , MnMap | |
e ii n o ,0j, i , j , i nMMpp d a e(niMnapiii | |
f ii d i + iP (inMap n n en(inMap[i] | |
uenn e(i(Map) n n l(niMaM[p[] | |
etu e r | |
ettu Fas | |
d gteNiigbrs((i Mnap: | |
e ng t ghbors(i nMap) | |
d iiap)) | |
f r n : [[, [1] nMaa: | |
r rif ,ii [ 1]1)] | |
i + p[1]]) | |
reun i n + ] j + ])[[11)] | |
etur ..ppndndnMMaa[ p[][j p]1] | |
ettr | |
mmeea ocossMsM(pprMMap | |
g = p (preMap | |
f reeaM) | |
f rp eap | |
prtttini) | |
d anii(Mnpp ieratinss) | |
e nm M ,] terations) | |
e ons | |
i s | |
t 0n | |
f rr (0, e(inMMa)p) | |
o ii g , en(inMap)) | |
f []])) | |
f ( , :, iapa | |
o ]]jj] | |
i 2 l i][j]) | |
< a ieeinMnp[[]]]j): | |
l 4 len(neighbors n ive(inMap[i][j]) | |
22 n ivv(nnMMai[]j[): | |
l 4 d] ive(inMap[i][j]) | |
l 3 negghoss))) | |
neighbors))) | |
l 3 neiigbrsr))) | |
< | |
l > n ead(inMap[i][j]) | |
ww " n aa((inMMpp][jjj) | |
ewMap.a n ) | |
f ne Le) | |
o ii e(0 ) | |
: | |
t p: | |
f ap | |
pa, eraaains | |
ain (newMap) teration | |
mn nwMa), etrrioin | |
ain(gameMap imes</code> |