## Creature Project ## CSC323: Group 1: Nora, Terian, Josh, and Jordan ## 4/7/10 ## localworld.py A local world with methods to set/get creatures from creature import Creature from conflict import ConflictResolutionPolicy class LocalWorld: """The Local User's World Class""" def __init__(self, x, y, defaultSpecies): """Fills an x by y grid with default creatures.""" self.width = x self.height = y if defaultSpecies == None: defaultSpecies = "Dead" self.defaultSpecies = defaultSpecies self.grid = [[Creature(defaultSpecies) for i in range(y)] for j in range(x)] self.conflictManager = ConflictResolutionPolicy() def __str__(self): """Returns a string of human readable attributes of localworld.""" printer = "" for i in range(self.height): printer += "\n" for j in range(self.width): printer += self.getCreature(j,i).__str__() + " " return ("Width: " + str(self.width) + " Height: " + str(self.height) + " " + printer + "\n") def setCreature(self, x, y, creature): """If valid, stores creature in grid at (x,y)""" if x >= self.width | y >= self.height: print("Bounds error") #replace with try/catch return if creature == None: #replace with try/catch print("Invalid creature") return self.grid[x][y] = Creature(creature) def getCreature(self, x, y): """Returns creature stored in the grid at (x,y)""" if x >= self.width | y >= self.height: print("Bounds error") #replace with try/catch return return self.grid[x][y] def toString(self): """Creates serialzed version of world for saving""" serialized = str(self.width) + "#" + str(self.height) for i in range(self.width): for j in range(self.height): c = self.getCreature(i, j) serialized += "#" + c.toString() return serialized def fromString(self, serialized): """Restores world to self from a serialized string""" wordlist = serialized.split("#") self.width = wordlist[0] self.height = wordlist[1] for i in range(self.width): for j in range(self.height): c = Creature() c.fromString(wordlist[i + j + 2]) self.setCreature(self, i, j, c) def __expandWorld(self, add_width, add_height): """Expands current world to a larger size. Requires the change in width/height as parameters.""" self.width += add_width self.height += add_height for i in range(self.width - add_width, add_width): for j in range(self.height - add_height, add_height): c = Creature(defaultSpecies) self.setCreature(self, i, j, c) def step(self): """Applies next step of changes to whole world.""" changes = [] for i in range(self.height): for j in range(self.width): c = self.getCreature(j,i) changes.append(c.spec.next(self,[j].append(i))) # got a list with all the changes, now we need to apply the changes.... new_grid = [[None for i in range(self.height)] for j in range(self.width)] for change in changes: for instruction in change: x = instruction.location[0] y = instruction.location[1] creat = instruction.creature if new_grid[x][y] == None: new_grid[x][y] = Creature(species) else: new_grid[x][y] = self.conflictManager.victor(new_grid[x][y], creat) #check if any empty spots, if so add dead creatures for i in range(self.height): for j in range(self.width): if new_grid[j][i] == None: new_grid[j][i] = Creature("Dead") self.grid = new_grid #if empty, add #if something there, run conflict resolution def applyChanges(): '''Takes a dictionary/list of changes and applys them to board.''' '''Ignores the "edges"''' pass def getChanges(): '''Iterates over the board and checks for changes''' pass