-
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.
- Loading branch information
Showing
5 changed files
with
342 additions
and
12 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 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,72 @@ | ||
species Output(int i) { | ||
// print whatever output was made this tick | ||
// This makes the console actually show FizzBuzz and the number accordingly | ||
decorator Print { | ||
printCount(Output::?()); | ||
} | ||
|
||
// These states are only here to show up in the output | ||
state Other { destroy; } | ||
|
||
state Fizz { destroy; } | ||
|
||
state Buzz { destroy; } | ||
|
||
state FizzBuzz { destroy; } | ||
} | ||
|
||
// species responsible for creating the correct output | ||
species Manager(int startCount) { | ||
|
||
// local function, can only be used inside this species | ||
function countMod(int i) -> int { | ||
return startCount % i == 0; | ||
} | ||
|
||
decorator Increment { | ||
startCount = startCount + 1; | ||
} | ||
|
||
// always start the state by incrementing `startCount`. | ||
@Increment | ||
state Managing { | ||
|
||
// the FizzBuzz logic | ||
if (countMod(3)) { | ||
if (countMod(5)) { | ||
create Output::FizzBuzz(i : startCount); | ||
} | ||
else { | ||
create Output::Fizz(i : startCount); | ||
} | ||
} | ||
|
||
else if (countMod(5)) { | ||
create Output::Buzz(i : startCount); | ||
} | ||
|
||
else { | ||
create Output::Other(i : startCount); | ||
} | ||
|
||
// run this state every tick | ||
enter Managing; | ||
} | ||
} | ||
|
||
function setup() -> void { | ||
// create a single instance of Manager in Managing state, with startCount = 0 | ||
create Manager::Managing(startCount: 0); | ||
|
||
// create reproducible simulation | ||
seed(7); | ||
|
||
// random number between 0 and 60 | ||
int terminateAfter = (int)(rand() * 6 * 10); | ||
// function defined in the stdlib | ||
exitAfterTicks(terminateAfter); | ||
} |
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,71 @@ | ||
// each cell has a coordinate | ||
species Cell(int x, int y) { | ||
|
||
state Alive { | ||
int alive = countNeighbours(x, y); | ||
// conditional enter -- enter if the expression is true | ||
alive < 2 || alive > 3 => Dead; | ||
|
||
enter Alive; | ||
} | ||
|
||
state Dead { | ||
// a dead cell can become alive if they have 3 neighbours that are alive | ||
countNeighbours(x, y) == 3 => Alive; | ||
|
||
enter Dead; | ||
} | ||
} | ||
|
||
function countNeighbours(int x, int y) -> int { | ||
int alive = 0; | ||
int nx = x-1; | ||
|
||
// iterate each neighbour | ||
while (nx < x+2) { | ||
int ny = y-1; | ||
|
||
while (ny < y+2) { | ||
if (nx != x || ny != y) { | ||
// add 1 to `alive`, based on cell being alive | ||
alive = alive + exists(Cell::Alive(x:nx, y:ny)); | ||
} | ||
|
||
ny = ny+1; | ||
} | ||
|
||
nx = nx+1; | ||
} | ||
|
||
return alive; | ||
} | ||
|
||
function setup() -> void { | ||
int height = 10; | ||
int width = 10; | ||
int x = 0; | ||
|
||
// create grid of 10x10 dead cells | ||
while(x < height) { | ||
int y = 0; | ||
|
||
while (y < width) { | ||
create Cell::Dead(x:x, y:y); | ||
y = y+1; | ||
} | ||
|
||
x = x+1; | ||
} | ||
|
||
// Draw initial configuration | ||
// This is the glider pattern | ||
create Cell::Alive(x:0, y:1); | ||
create Cell::Alive(x:1, y:2); | ||
create Cell::Alive(x:2, y:0); | ||
create Cell::Alive(x:2, y:1); | ||
create Cell::Alive(x:2, y:2); | ||
|
||
// exit when no cells are alive, or after 50 ticks | ||
exitWhenNoLongerExists(Cell::Alive()); | ||
exitAfterTicks(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 @@ | ||
../scripts/society.sct |
Oops, something went wrong.