Last active
August 18, 2017 07:08
-
-
Save zach-c-d/d6f52a36235883a742b702a4ed7d975d to your computer and use it in GitHub Desktop.
Prints a grid to the console filled with random binary data
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
/*Common Algorithms*/ | |
/* | |
i = x + (y * width) | |
//Index of Grid = X coordinate * (Y coordinate * width) | |
Math.floor(Math.random() + 0.5) | |
//Randomly chooses between 0 and 1 | |
*/ | |
function Grid(x,y){ | |
genGrid(x,y); | |
printGrid(); | |
findMiddle(); | |
return(str); | |
} | |
var grid = []; | |
var width, height; | |
var str = ''; //string to print to console | |
function genGrid(w,h){ | |
//clear grid and string | |
grid = []; | |
str = ''; | |
width = w; | |
height = h; | |
//incorrect inputs////// | |
//incorrect inputs////// | |
//incorrect inputs////// | |
if (w === undefined && | |
h === undefined) { | |
console.log('Enter grid size (x,y)'); | |
}else if (w === undefined){ | |
console.log('Enter grid size (x,y)'); | |
} | |
//if only width | |
//volume = width X width OR | |
//if width and height are set | |
//volume = width X heigth | |
else if (h === null){ | |
vol = w*w; | |
}else{ | |
var vol = w * h; | |
} | |
for (var i = 0; i < vol; i++){ | |
var cell = Math.floor(Math.random() + 0.5); | |
grid.push(cell); | |
} | |
} | |
function returnLocation(x,y){ | |
var i = x + (y * width); | |
if (i > grid.length){ | |
return("not a location"); | |
}else{ | |
return grid[i]; | |
} | |
} | |
function printGrid(){ | |
str = str.concat('\n'); | |
for (var i = 0; i < height; i++){ | |
for (var j = 0; j < width; j++){ | |
str = str.concat(' ' + returnLocation(j,i)); | |
}str = str.concat('\n'); | |
} | |
} | |
function findMiddle(){ | |
//if value is even, choose random between middle two | |
//else, grab middle value | |
if(width % 2 == 0){ | |
this.x = (width / 2)-(Math.floor(Math.random() + 0.5)); | |
}else{ | |
this.x = (width/2) - 0.5; | |
} | |
if(height % 2 ==0){ | |
this.y = (height/2) - (Math.floor(Math.random() + 0.5)); | |
}else{ | |
this.y = height/2 - 0.5; | |
} | |
this.middleIndex = this.x + (this.y * width); | |
console.log(this.x + "," + this.y); | |
console.log(middleIndex); | |
} | |
function spawnBot(){ | |
//find middle spawn point | |
//change value of spawn point to 0 | |
//spawn Bot in middle of Grid | |
findMiddle().middleIndex; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment