Created
August 10, 2010 01:48
-
-
Save gcr/516501 to your computer and use it in GitHub Desktop.
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
// cave.js | |
// | |
// contains ways of changing, generating, and displaying a cave. | |
// a cave is a 2d array. | |
// | |
// All these methods are side-effect-free builders. | |
// | |
var sys = require('sys'); | |
function Cave(width, height, probability) { | |
// make a new cave with random probability | |
var cave = []; | |
for (var y=0; y<height; y++) { | |
var row = []; | |
for (var x=0; x<width; x++) { | |
row[x] = Math.random() > probability; | |
} | |
cave.push(row); | |
} | |
this.data = cave; | |
} | |
Cave.prototype.width = function() { | |
return this.data[0].length; | |
}; | |
Cave.prototype.height = function() { | |
return this.data.length; | |
}; | |
Cave.prototype.map = function(func) { | |
var newCave = new Cave(0,0,0); | |
var self = this; | |
newCave.data = this.data.map(function(row, y) { | |
return row.map(function(cell, x) { | |
return func.call(self, cell, x, y); | |
}); | |
}); | |
return newCave; | |
}; | |
Cave.prototype.mapTimes = function(n, func) { | |
if (n===0) { | |
return this; | |
} | |
return this.map(func).mapTimes(n-1, func); | |
}; | |
Cave.prototype.display = function(cb) { | |
cb = cb || sys.puts; | |
for (var i=0,l=this.height(); i<l; i++) { | |
cb(this.data[i].map(function(x) { return x?"#":" "; }).join('')); | |
} | |
}; | |
Cave.prototype.tilesAround = function(x, y, dist) { | |
var width=this.width(), height=this.height(); | |
var sum = 0; | |
for (var i = x-dist; i<=x+dist; i++) { | |
for (var j = y-dist; j<=y+dist; j++) { | |
if (i<0 || i>=width || j<0 || j>=height || | |
this.data[j][i]) { | |
sum++; | |
} | |
} | |
} | |
return sum; | |
}; | |
exports.Cave = Cave; |
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
var Cave = require('./cave').Cave, | |
sys = require('sys'), demonstrate=function(t) {sys.puts("\n\n"+t);}; | |
var cave = new Cave(70, 24, 0.55); | |
demonstrate("Cave"); | |
cave.display(); | |
demonstrate("Old algorithm -- 3x3 square contains >=5 walls (5 times)"); | |
cave.mapTimes(4, function(cell, x, y) { | |
return this.tilesAround(x, y, 1) >= 5; | |
}).display(); | |
demonstrate("New algorithm -- if 3x3 contains >=5 walls or if we're in an open space"); | |
cave.mapTimes(5, function(cell, x, y) { | |
return this.tilesAround(x, y, 1) >= 5 || | |
this.tilesAround(x, y, 2) <= 2; | |
}).display(); | |
demonstrate("New algorithm 2 -- previous, then thrice run old algorithm"); | |
cave.mapTimes(5, function(cell, x, y) { | |
return this.tilesAround(x, y, 1) >= 5 || | |
this.tilesAround(x, y, 2) <= 2; | |
}).mapTimes(3, function(cell, x, y) { | |
return this.tilesAround(x, y, 1) >= 5; | |
}).display(); |
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
Cave | |
# ## # # # ## # ### #### ## # ### ###### ## #### ## # | |
# # ## # # ## # # # ##### ## # ### # # | |
# # # ## ### # # # # ## # # # ## ## #### ## ## # | |
# ## ## ### ## # # ## ###### ## ## #### # # # # # | |
# #### ### # # # # #### # # # # # # # # ##### ##### # | |
## # # ## # ### # # ## ## ###### # ## ### ## # | |
# # #### # # ### # # ##### # # ### ## # | |
# # # # # # # # ## ## ## # ## ##### ## | |
## ## # # #### ## ### # ## # # #### ## # ## ## | |
# ### ## # ####### ### ## # #### ## # # # # # ## # ### ### | |
# #### # # # ### #### ##### ## # ## ### ### ## # # ### # | |
# # # # # # ## ## # # # # ### ## # ### ### # | |
# #### ####### ## ###### ## # # # # # # ## | |
## # ### ## # # # # # ##### ### # ### ## | |
## # ## ## # #### ## # # #### ## #### # # ## # ## # | |
# # ## # #### #### #### # ##### # ## ## ## # ### # # # ### | |
### ## # ## # # ### ### ## ## # #### # ## # | |
# ### ## # # # # ## # ### ## # #### # #### # ## | |
# # # ### # ## # # # # # ### # # # # ## # # | |
## #### # # # # ### # # # # ### # ## ## ## ### # | |
# # ## # # # # # ### # ## ## ## # | |
### # # # # ### # # # # ## # # #### ### # ## ## ## | |
## ##### # # # #### ### # ### ### # # # ## #### # | |
# # # # # # ## # ## # # ## ### ## # # # # # ## | |
Old algorithm -- 3x3 square contains >=5 walls (5 times) | |
###################### ########### ######################### ## | |
##### ###### ######### ####################### # | |
## ##### ## ## ######### ###### # | |
# ###### ####### #### ## | |
# ### # ####### ### ## ## | |
# ### ####### #### ## | |
## ### ####### ######### | |
## #### ###### ########## | |
# #### ## # ### #### ## ########## | |
# #### ####### ##### #### #### ######### | |
# ##### ###### ###### ######### ######## | |
# ##### ### #### ##### ###### ####### | |
# #### ##### ## ##### ####### | |
# ## ##### ##### ##### ####### | |
# #### ###### ####### ######## | |
## ## ##### ##### # ########### | |
### ### #### ############ | |
#### #### ###### ### | |
######## ### #### ## | |
# #### ### #### # | |
# #### ##### ## #### # | |
######## ## ############ ##### ## | |
######### #### ### ############# ########### | |
############# ################ ##################### ############## | |
New algorithm -- if 3x3 contains >=5 walls or if we're in an open space | |
###################### ########### ######################### ## | |
##### ####### ######### ####################### # | |
## ###### ## ## ### ######### ####### # | |
# ##### #### # ##### ####### #### ## | |
# ## ### ###### # ##### ####### ## ## ## ## | |
# ### ##### ## ##### #### ####### ### #### ### | |
## ### ## #### # ##### ## ####### ## ######### | |
## #### ## ##### ## ###### ########## | |
# #### ## ### ## ### # #### # ########## | |
# #### ## ###### ##### #### #### #### ######### | |
# ##### # ###### ###### ##### ######## ######## | |
# ##### ### #### # ##### ### ###### ####### | |
# #### #### ## ### ##### # ### ####### | |
# ## ##### #### ##### ##### # ####### | |
# ### ##### ###### ###### ######## | |
## ## ## ###### ##### ###### ### ########### | |
### ## ##### ### #### ### ########### | |
#### # ## # #### ###### ### | |
####### #### ### ### # ### #### ## | |
## ##### # ####### ### ####### #### ### # #### # | |
## ##### ###### ###### ##### ## # #### # | |
######## ## ## ############ ##### ## | |
######### #### ### ############# ########### | |
############# ################ #################################### | |
New algorithm 2 -- previous, then thrice run old algorithm | |
###################### ########### ######################### ## | |
##### ######## ######### ####################### # | |
## ###### ## ## ### ######### ####### # | |
# ##### ### ##### ####### #### ## | |
# ### ##### ##### ####### ## ## ### | |
# ### ##### # #### ####### ######## | |
## ### #### ## ## ####### ######### | |
## #### #### # ###### ########## | |
# #### ## ## ### #### ########## | |
# #### #### ##### # #### ## ######### | |
# ##### ##### ###### ## ####### ######## | |
# ##### # #### ##### # ###### ####### | |
# #### ### ## ## ##### ####### | |
# ## #### #### ##### ### ####### | |
# ## ##### ###### ##### ######## | |
## ##### ##### ##### ########## | |
### ### ### ##### ########### | |
#### ## #### ###### ### | |
####### #### ## ### #### ## | |
######## ##### #### ### #### # | |
######## ### #### ##### ## #### # | |
######## ## ## ############ ##### ## | |
######### #### ### ############## ########### | |
############# ################ #################################### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment