Skip to content

Commit

Permalink
Update Game of Life example to new format
Browse files Browse the repository at this point in the history
Also some refactoring and stylistic cleanup
  • Loading branch information
njvrzm committed Jun 6, 2016
1 parent c30c220 commit fa1d91d
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 113 deletions.
16 changes: 9 additions & 7 deletions examples/ConwaysGameOfLife/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ The "game" is a zero-player game, meaning that its evolution is determined by it

## How to Run

To run the model interactively, run ``run.py`` in this directory. e.g.
To run the model interactively, run ``run.py`` in this directory:

```
$ python cgol_main.py
$ python run.py
```

Then open your browser to [http://127.0.0.1:8888/](http://127.0.0.1:8888/) and press Reset, then Run.
Then open your browser to [http://127.0.0.1:8888/](http://127.0.0.1:8888/) and press ``run``.

## Files

* ``cgol_cell.py``: Defines the behavior of an individual cell, which can be in two states: DEAD or ALIVE.
* * ``cgol_model.py``: Defines the model itself, initialized with a random configuration of alive and dead cells.
* * ``cgol_main.py``: Defines and launches an interactive visualization.
* ``game_of_life/cell.py``: Defines the behavior of an individual cell, which can be in two states: DEAD or ALIVE.
* ``game_of_life/model.py``: Defines the model itself, initialized with a random configuration of alive and dead cells.
* ``game_of_life/portrayal.py``: Describes for the front end how to render a cell.
* ``game_of_live/server.py``: Defines an interactive visualization.
* ``run.py``: Launches the visualization

## Further Reading

[Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life)

62 changes: 0 additions & 62 deletions examples/ConwaysGameOfLife/cgol_cell.py

This file was deleted.

29 changes: 0 additions & 29 deletions examples/ConwaysGameOfLife/cgol_main.py

This file was deleted.

53 changes: 53 additions & 0 deletions examples/ConwaysGameOfLife/game_of_life/cell.py
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from mesa import Model
from mesa.time import SimultaneousActivation
from mesa.space import Grid
from cgol_cell import CGoLCell
import random
from game_of_life.cell import Cell
from random import random



class CGoLModel(Model):
class GameOfLife(Model):
'''
Represents the 2-dimensional array of cells in Conway's
Game of Life.
Expand All @@ -31,21 +30,15 @@ def __init__(self, height, width):
# Place a cell at each location, with some initialized to
# ALIVE and some to DEAD.
for (contents, x, y) in self.grid.coord_iter():
pos = (x, y)
init_state = CGoLCell.DEAD
# Initially, make 10% of the cells ALIVE.
if random.random() < 0.1:
init_state = CGoLCell.ALIVE
cell = CGoLCell(pos, self, init_state)
# Put this cell in the grid at position (x, y)
self.grid.place_agent(cell, pos)
# Add this cell to the scheduler.
cell = Cell((x, y), self)
if random() < .1:
cell.state = cell.ALIVE
self.grid.place_agent(cell, (x, y))
self.schedule.add(cell)
self.running = True

def step(self):
'''
Advance the model by one step.
Have the scheduler advance each cell by one step
'''
self.schedule.step()

18 changes: 18 additions & 0 deletions examples/ConwaysGameOfLife/game_of_life/portrayal.py
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"
}
10 changes: 10 additions & 0 deletions examples/ConwaysGameOfLife/game_of_life/server.py
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)
3 changes: 3 additions & 0 deletions examples/ConwaysGameOfLife/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from game_of_life.server import server

server.launch()

0 comments on commit fa1d91d

Please sign in to comment.