2 min readOct 25, 2020
To find the front line of Invaders for my space invaders game, I had to find the lowest coordinate of each invader in each column.
I brute-forced it.
Here is my code:
getFrontLine = () => {//I need to find the row of aliens that's unimpeded.let allInvaderCopy = this.state.allInvaderslet invaders = this.state.invaderCoordinates//if I reverse the list, and then iterate through it, the first eleven that aren't dead should be the front line.//if one gets shot though, that alters things. So I need to keep iterating and checking against the previous row.//So what I need to do is reverse the whole array, then break it into increments of 11, and then check them against each other.let firstRow = [];let secondRow = [];let thirdRow = [];let fourthRow = [];let fifthRow = [];for (let i = 0; i < 11; i++){firstRow.push(allInvaderCopy[i]);secondRow.push(allInvaderCopy[i + 11]);thirdRow.push(allInvaderCopy[i + 22]);fourthRow.push(allInvaderCopy[i + 33]);fifthRow.push(allInvaderCopy[i + 44]);}let i = 0;let frontLine = [];while (i < 11){if (this.checkIfDead(firstRow, i) === undefined){let newInvader = invaders.find(invader => invader.id === (firstRow[i].id));frontLine.push(newInvader);i++;} else if (this.checkIfDead(firstRow, i) !== undefined){if (this.checkIfDead(secondRow, i) === undefined){let newInvader = invaders.find(invader => invader.id === (secondRow[i].id));frontLine.push(newInvader);i++;} else if (this.checkIfDead(secondRow, i) !== undefined){if (this.checkIfDead(thirdRow, i) === undefined){let newInvader = invaders.find(invader => invader.id === (thirdRow[i].id));frontLine.push(newInvader);i++;} else if (this.checkIfDead(thirdRow, i) !== undefined){if (this.checkIfDead(fourthRow, i) === undefined){let newInvader = invaders.find(invader => invader.id === (fourthRow[i].id));frontLine.push(newInvader);i++;} else if (this.checkIfDead(fourthRow, i) !== undefined){if (this.checkIfDead(fifthRow, i) === undefined){let newInvader = invaders.find(invader => invader.id === (fifthRow[i].id));frontLine.push(newInvader);i++;} else if (this.checkIfDead(fifthRow, i) !== undefined){i++;}}}}}}return frontLine;}
The code is ugly. Even uglier on medium, which apparently doesn’t carry indentations over. But it runs, and it works.