Created
November 25, 2018 22:47
-
-
Save runandrerun/b7950603c8f97e5b22ee200076d7cda9 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
const closestEnemy = (arr) => { | |
let player = []; | |
let enemy = []; | |
let distance = 0; | |
let rowLength = arr[0].length; | |
let board = arr.length; | |
// iterate over the length of the board stored in arrWidth | |
for (let i = 0; i < board; i++) { | |
// split each row within the board into singlular elements | |
let row = arr[i].split(""); | |
// set the length of each new split array for iteration | |
let rowLength = row.length; | |
for (let j = 0; j < rowLength; j++) { | |
// look for the player's starting position | |
// if the player (1) is found then push in the player's index | |
// && push the index of the row on the board into player array | |
if (row[j] == 1) { | |
player.push(j, i); | |
// look for every enemy (2) on the board | |
// if the enemy is found then push the enemy's index | |
// && the index of the row on the board into the enemy array | |
} else if (row[j] == 2) { | |
enemy.push(j, i); | |
} | |
} | |
} | |
console.log("Player", player) | |
console.log("Enemies", enemy) | |
// declare variable for enemy array length | |
// this is for basic optimization of iteration | |
let enemyLength = enemy.length; | |
// iterate over the length of the enemy array | |
for (let i = 0; i < enemyLength; i += 2) { | |
// declare a newDistance for counting | |
let newDistance = 0; | |
// check if the nearest enemy is on the same row | |
// use Math.abs in order to return an absolute (positive) integer | |
// if the player's location minus the enemy's location is less than | |
// the row's length / 2 then adjust the newDistance to that value | |
if (Math.abs(player[0] - enemy[i]) < rowLength / 2) { | |
console.log('Inside Loop', player[0]) | |
console.log('2nd', enemy[i]) | |
newDistance = Math.abs(player[0] - enemy[i]); | |
console.log(newDistance) | |
} else { | |
// otherwise adjust the newDistance to rowLength - the player's location of the enemy's location | |
newDistance = rowLength - Math.abs(player[0] - enemy[i]); | |
console.log(newDistance) | |
} | |
// check for distance between the player and enemy up and down the board | |
if (Math.abs(player[1] - enemy[i+1]) < board / 2) { | |
// calculate the difference between the player location and the enemy's | |
// add it to the overall count of the distance from above | |
newDistance += Math.abs(player[1] - enemy[i+1]); | |
} else { | |
newDistance += board - Math.abs(player[0] - enemy[i]); | |
} | |
// if the newDistance is less than the overall distance return it | |
// otherwise set the distance equal to the shortest travel | |
if (distance == 0 || newDistance < distance) { | |
distance = newDistance; | |
} | |
} | |
// return the distance | |
return distance; | |
} | |
closestEnemy(["0000", "2010", "0000", "2002"]); | |
closestEnemy(["0000", "1000", "0002", "0002"]); | |
closestEnemy(["0000", "2010", "0000", "2002"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment