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
1 parent
5860101
commit 03164ed
Showing
4 changed files
with
70 additions
and
3 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
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,14 @@ | ||
An implementation of [Langton's ant](https://en.wikipedia.org/wiki/Langton%27s_ant) | ||
using Reactive Extensions. | ||
|
||
**How to play** | ||
|
||
This game is played differently than the others. | ||
|
||
You can: | ||
|
||
- adjust the parameters of the algorithm to see how it behaves | ||
- implement an extension, e.g. | ||
[multiple colors](https://en.wikipedia.org/wiki/Langton%27s_ant#Extension_to_multiple_colors), | ||
[multiple states](https://en.wikipedia.org/wiki/Langton%27s_ant#Extension_to_multiple_states), | ||
[multiple ants](https://en.wikipedia.org/wiki/Langton%27s_ant#Extension_to_multiple_ants) |
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,42 @@ | ||
var squareSize = 4, antPulse = 1 | ||
|
||
api.initGrid({squareSize: squareSize}) | ||
|
||
var boardLayer = api.addLayer({color: '#337ab7'}), antLayer = api.addLayer({color: '#275b8c'}) | ||
|
||
// place the ant in the middle of the board | ||
antLayer.fill({x: api.gameSize / 2, y: api.gameSize / 2}) | ||
|
||
var pulse = Rx.Observable.interval(antPulse).share() | ||
|
||
// is the ant walking on an activated square? | ||
var probes = pulse.map(() => antLayer.getActiveSquares()[0]) | ||
.map(antSquare => ({ | ||
coord: antSquare, | ||
active: boardLayer.getActiveSquares() | ||
.find(boardSquare => boardSquare.x == antSquare.x && boardSquare.y == antSquare.y) | ||
}) | ||
).share() | ||
|
||
// http://javascript.about.com/od/problemsolving/a/modulobug.htm | ||
var modulo = (n, m) => ((n % m) + m) % m | ||
|
||
// determine next direction | ||
var directions = probes.scan((acc, step) => modulo(acc + (step.active ? -1 : 1), 4), 0) | ||
.map(directionIndex => | ||
[api.directions.Left, api.directions.Up, | ||
api.directions.Right, api.directions.Down][directionIndex]) | ||
.map(direction => direction(antLayer.getActiveSquares()[0])) | ||
.share() | ||
|
||
// update ant's position | ||
directions.do(antLayer.fill).subscribe(() => antLayer.clear(antLayer.getActiveSquares()[0])) | ||
|
||
// switch color of square left behind | ||
probes.subscribe(step => (step.active ? boardLayer.clear : boardLayer.fill)(step.coord)) | ||
|
||
// handle end of 'game' | ||
directions.where(api.isOffLimits).subscribe(api.gameOver); | ||
|
||
// display iterations | ||
pulse.subscribe(i => api.setText({text: i})); |