forked from projectmesa/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Game of Life example to new format
Also some refactoring and stylistic cleanup
- Loading branch information
Showing
8 changed files
with
101 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from mesa import Agent | ||
|
||
|
||
class Cell(Agent): | ||
'''Represents a single ALIVE or DEAD cell in the simulation.''' | ||
|
||
DEAD = 0 | ||
ALIVE = 1 | ||
|
||
def __init__(self, pos, model, init_state=DEAD): | ||
''' | ||
Create a cell, in the given state, at the given x, y position. | ||
''' | ||
Agent.__init__(self, pos, model) | ||
self.x, self.y = pos | ||
self.state = init_state | ||
self._nextState = None | ||
|
||
@property | ||
def isAlive(self): | ||
return self.state == self.ALIVE | ||
|
||
@property | ||
def neighbors(self): | ||
return self.model.grid.neighbor_iter((self.x, self.y), True) | ||
|
||
def step(self, model): | ||
''' | ||
Compute if the cell will be dead or alive at the next tick. This is | ||
based on the number of alive or dead neighbors. The state is not | ||
changed here, but is just computed and stored in self._nextState, | ||
because our current state may still be necessary for our neighbors | ||
to calculate their next state. | ||
''' | ||
|
||
# Get the neighbors and apply the rules on whether to be alive or dead | ||
# at the next tick. | ||
live_neighbors = sum(neighbor.isAlive for neighbor in self.neighbors) | ||
|
||
# Assume nextState is unchanged, unless changed below. | ||
self._nextState = self.state | ||
if self.isAlive: | ||
if live_neighbors < 2 or live_neighbors > 3: | ||
self._nextState = self.DEAD | ||
else: | ||
if live_neighbors == 3: | ||
self._nextState = self.ALIVE | ||
|
||
def advance(self, model): | ||
''' | ||
Set the state to the new computed state -- computed in step(). | ||
''' | ||
self.state = self._nextState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def portrayCell(cell): | ||
''' | ||
This function is registered with the visualization server to be called | ||
each tick to indicate how to draw the cell in its current state. | ||
:param cell: the cell in the simulation | ||
:return: the portrayal dictionary. | ||
''' | ||
assert cell is not None | ||
return { | ||
"Shape": "rect", | ||
"w": 1, | ||
"h": 1, | ||
"Filled": "true", | ||
"Layer": 0, | ||
"x": cell.x, | ||
"y": cell.y, | ||
"Color": "black" if cell.isAlive else "white" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from game_of_life.model import GameOfLife | ||
from mesa.visualization.modules import CanvasGrid | ||
from mesa.visualization.ModularVisualization import ModularServer | ||
from game_of_life.portrayal import portrayCell | ||
|
||
# Make a world that is 50x50, on a 250x250 display. | ||
canvas_element = CanvasGrid(portrayCell, 50, 50, 250, 250) | ||
|
||
server = ModularServer(GameOfLife, [canvas_element], "Game of Life", | ||
50, 50) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from game_of_life.server import server | ||
|
||
server.launch() |