Created
September 16, 2017 18:53
-
-
Save zach-c-d/6ff5ca4f0b222cab1de0c42dc800e680 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
/*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 | |
*/ | |
window.onload = function(){ | |
Grid(21,21); | |
console.log(str); | |
} | |
function Grid(x,y){ | |
genGrid(x,y); | |
spawnBot(); | |
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'); | |
// Challenge: Transform the for-loop style of printing the grid, into an eloquent reduce function that will conditionally append either 1/0 or a newline character based on some condition. | |
// https://stackoverflow.com/questions/38270089/reduce-array-to-a-single-string | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce?v=b | |
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 | |
var pickleMiddle = function(dimension) { | |
if(dimension % 2 == 0){ | |
return (dimension / 2)-(Math.floor(Math.random() + 0.5)); | |
} else { | |
return (dimension/2) - 0.5; | |
} | |
} | |
this.x = pickleMiddle(width); | |
this.y = pickeMiddle(height); | |
/* | |
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 | |
this.spawnLocation = findMiddle.middleIndex; | |
console.log(this.spawnLocation); | |
grid[45] = '#'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment