flash on 2011-5-18
Slightly modified to show step by step work.
Originela code on http://www.emanueleferonato.com/2011/05/17/using-cellular-automata-to-generate-random-land-and-water-maps-with-flash/
/**
* Copyright St3vE3 ( http://wonderfl.net/user/St3vE3 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/6Ep3
*/
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
public class terrain extends Sprite {
private const WATER:int=0;
private const LAND:int=1;
private var iterations:int=0;
private var simultaneously:Boolean=true;
private var landRatio:Number=0.5;
private var mapWidth:int=64;
private var mapHeight:int=64;
private var tileSize:int=7;
private var canvas:Sprite;
private var button:Sprite;
private var landArray:Array;
private var newLandArray:Array;
private var originalSeed:Number = 3;
private var seed:Number = originalSeed+0;
public function terrain() {
canvas = new Sprite();
button = new Sprite();
button.graphics.beginFill(0xFF0000);
button.graphics.drawRect(0,0,100,30);
button.graphics.endFill();
button.addEventListener(MouseEvent.CLICK,getNewSeed);
landArray=new Array();
newLandArray=new Array();
addChild(canvas);
addChild(button);
generateMap();
canvas.addEventListener(MouseEvent.CLICK,onClick);
}
private function generateSeed():Number
{
return (seed * 16807) % 2147483647;
}
private function getRandom():Number
{
seed = generateSeed();
return seed/2147483647;
}
private function generateMap():void {
landOrWater();
drawMap();
}
private function drawMap():void {
for (var i:int=0; i<mapWidth; i++) {
for (var j:int=0; j<=mapWidth; j++) {
switch (landArray[i][j]) {
case WATER :
drawWater(i,j);
break;
case LAND :
drawLand(i,j);
break;
}
}
}
}
private function landOrWater():void {
for (var i:int=0; i<mapWidth; i++) {
landArray[i]=new Array();
newLandArray[i]=new Array();
for (var j:int=0; j<=mapWidth; j++) {
if (getRandom()<landRatio) {
landArray[i][j]=LAND;
} else {
landArray[i][j]=WATER;
}
}
}
for (i=1; i<=iterations; i++) {
iterateLand(LAND,WATER);
}
}
private function drawLand(col:int,row:int):void {
canvas.graphics.beginFill(0x00ff00);
canvas.graphics.lineStyle(1,0x000000);
canvas.graphics.drawRect(tileSize*col,tileSize*row,tileSize,tileSize);
canvas.graphics.endFill();
}
private function drawWater(col:int,row:int):void {
canvas.graphics.beginFill(0x0000ff);
canvas.graphics.lineStyle(1,0x000000);
canvas.graphics.drawRect(tileSize*col,tileSize*row,tileSize,tileSize);
canvas.graphics.endFill();
}
private function iterateLand(land:int,defaultLand:int):void {
for (var i:int=0; i<mapWidth; i++) {
for (var j:int=0; j<=mapWidth; j++) {
if (landArray[i][j]==land||landArray[i][j]==defaultLand) {
if (adjacentLand(i,j,land)>=5) {
if (! simultaneously) {
landArray[i][j]=land;
} else {
newLandArray[i][j]=land;
}
} else {
if (! simultaneously) {
landArray[i][j]=defaultLand;
} else {
newLandArray[i][j]=defaultLand;
}
}
}
}
}
if (simultaneously) {
for (i=0; i<mapWidth; i++) {
for (j=0; j<=mapWidth; j++) {
landArray[i][j]=newLandArray[i][j];
}
}
}
}
private function adjacentLand(col:int,row:int,lookFor:int):int {
var found:int=0;
for (var i:int=-1; i<=1; i++) {
for (var j:int=-1; j<=1; j++) {
if (landArray[col+i]!=undefined&&landArray[col+i][row+j]==lookFor) {
found++;
}
}
}
return found;
}
private function onClick(e:MouseEvent=null):void {
canvas.graphics.clear();
iterations++;
seed=originalSeed+0;
generateMap();
}
private function getNewSeed(e:MouseEvent=null):void
{
originalSeed = generateSeed();
iterations =- 1;
onClick();
}
}
}