Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This javascript file creates a simple 2d map representation
- function d2_array(size){
- //Returns a basic 2D array of specified size
- x = new Array(size);
- for (var i = 0; i < size; i++) {
- x[i] = new Array(size)
- }
- for (var i = 0; i < size; i++) {
- for (var j = 0; j < size; j++) {
- x[i][j] = 0
- }
- }
- return x
- }
- //-----------------------------------------------------------------------------
- var Map = function (size) {
- //Creates an empty map
- this.size = size
- this.raw_map = d2_array(size)
- }
- Map.prototype.set_point = function(x,y,z) {
- //Sets the height of a specified point, doesn't have to be set to a number :-)
- if (x >= this.size || y >= this.size || x < 0 || y < 0){
- console.log('Improper Map Access:',x,y)
- } else {
- this.raw_map[x][y] = z
- }
- }
- Map.prototype.get_point = function(x,y) {
- //Returns the height of a specified point or 1 if outside map area
- if (x >= this.size || y >= this.size || x < 0 || y < 0){
- return 1
- } else {
- return this.raw_map[x][y]
- }
- }
- Map.prototype.display = function(new_line, border) {
- //Returns a string containing a representation of the map
- out_str = '\n'
- for (var i = 0-border; i < this.size+border; i++) {
- for (var j = 0-border; j < this.size+border; j++) {
- out_str += this.get_point(i,j)
- }
- out_str += new_line
- }
- return out_str
- }
- Map.prototype.get_num_neighbours = function(x,y) {
- //Returns the number of neighbours of a map
- total = 0
- for (var i = -1; i <= 1; i++) {
- for (var j = -1; j <= 1; j++) {
- total += this.get_point(x+i,y+j)
- }
- }
- return total
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement