[QUIZ#7] Thomas Darimont (Python)

Thomas Darimont

Erfahrenes Mitglied
Hallo,

hier mal eine Python Version von unserem GameOfLife:
Python:
import sys
from sys import stdin

class World(object):
    def encode(self,worldMap):
       data = [0 for x in xrange(0,rows*columns)]
       for i in xrange(0,len(data)):
           if worldMap[i] == self.lifeChar:
               data[i] = 1
       return data    
    
    def __init__(self, worldMap,rows,columns,deadChar,lifeChar,rule):
       self.deadChar = deadChar
       self.lifeChar = lifeChar
       self.rule = rule
       self.grid = Grid(rows,columns,self.encode(worldMap))
      
    def getNeighbourCount(self,row,column):
        neighbourCount = 0
        neighbourCount += self.grid.getCellValue(row - 1, column - 1)
        neighbourCount += self.grid.getCellValue(row - 1, column)
        neighbourCount += self.grid.getCellValue(row - 1, column + 1)
        neighbourCount += self.grid.getCellValue(row, column - 1)
        neighbourCount += self.grid.getCellValue(row, column + 1)
        neighbourCount += self.grid.getCellValue(row + 1, column - 1)
        neighbourCount += self.grid.getCellValue(row + 1, column)
        neighbourCount += self.grid.getCellValue(row + 1, column + 1)
        return neighbourCount

    def evolve(self):
        newGrid = self.grid.copy()
        for row in xrange(0,self.grid.rows):
            for column in xrange(0,self.grid.columns):
                alifeNeighbourCount = self.getNeighbourCount(row, column);
                cellValue = self.grid.getCellValue(row, column);
                self.rule.apply(self,newGrid,row,column,alifeNeighbourCount,cellValue)
        self.grid = newGrid

    def __str__(self):
        s =""
        for row in xrange(0,self.grid.rows):
            for column in xrange(0,self.grid.columns):
                s+= 'o' if self.grid.getCellValue(row, column) == 1 else '+'
            s +="\n"
        return s
       
class Grid(object):
    def __init__(self, rows,columns,data):
        self.rows = rows
        self.columns = columns
        self.data = data
        
    def getCellValue(self,row,column):
        if (row < 0 or row >= rows or column < 0 or column >= columns):
            return 0;
        return self.data[row * self.columns + column];
    
    def setCellValue(self,row,column,value):
        self.data[row*self.columns+column] = value
        
    def copy(self):
        return Grid(rows,columns,[0 for x in xrange(0,self.rows*self.columns)])

class Rule(object):
    def __init__(self,rule):
        survivalRuleVSNewLifeRule = rule.split("/")
        self.neighbourCountsForSurvival = survivalRuleVSNewLifeRule[0]
        self.neighbourCountForNewLife = survivalRuleVSNewLifeRule[1]
    
    def apply(self,world,grid,row,column,aliveNeighbours,cellValue):
        s = str(aliveNeighbours)
        
        if (cellValue == 1 and s in self.neighbourCountsForSurvival ):
            grid.setCellValue(row, column, 1)
            
        if (cellValue == 0 and s in self.neighbourCountForNewLife):
            grid.setCellValue(row, column, 1)
           
if __name__ == "__main__": 
    rows = int(stdin.readline().strip())
    columns = int(stdin.readline().strip())
    iterations = int(stdin.readline().strip())
    
    worldMap =""
    for lineNo in xrange(0,rows):
        worldMap += sys.stdin.readline().strip()
    
    rule = sys.stdin.readline().strip()
    if len(rule) == 0:
        rule = Rule("23/3") #default: conways rule
    else:
        rule = Rule(rule)
    
    world = World(worldMap, rows, columns, '+', 'o', rule)
    
    for iteration in xrange(0,iterations):
        world.evolve()
        
    print world

Ausgabe:
Code:
C:\development\eclipse\workspaces\w35M3\de.tutorials.python.training\src\de\tutorials\contest>python GameOfLife.py
6
7
4
+++++++
++o++++
+++o+++
+ooo+++
+++++++
+++++++
23/3

+++++++
+++++++
+++o+++
++++o++
++ooo++
+++++++

Gruß Tom
 
Hallo Tom,

man kann bei deinem Programm ja scheinbar mehrere Regeln angeben. Wenn ich das richtig sehe, werden dann alle Regeln nacheinander auf demselben Grid angewandt, d.h. am Schluss bleibt nur das Ergebnis der letzten angegebenen Regel übrig. Ist das gewollt?

Grüße,
Matthias
 
Hallo,

das ist tatsächlich ein Bug. Eigentlich unterstützt die Implementierung vom Ablauf her nur eine Regel.

Habs mal korrigiert.

Gruß Tom
 
Zurück