Apply a fix to the determineDirection function that makes it conform to the rule specification

This commit is contained in:
Oliver Akins 2022-06-22 00:35:03 -06:00
parent 3b78f224df
commit 670729c892
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99

View file

@ -40,8 +40,27 @@ export function countShips(board: any[], shipLocation: number) {
* - `1` = Towards the Warp Gate * - `1` = Towards the Warp Gate
*/ */
export function determineDirection(board: any[], shipLocation: number) { export function determineDirection(board: any[], shipLocation: number) {
let delta = 1;
/*
Edge-Case check for when the board only has a single ship on it because any
ships that hit the warp gate get removed from the game, and ships in the
singularity don't create gravity wells, not affecting other ships.
*/
let shipCount = 0;
for (const location of board) {
if (location != null) {
shipCount++;
if (shipCount >= 2) {
break;
};
};
};
if (shipCount == 1) {
return 1;
};
let delta = 1;
while (true) { while (true) {
// The left side of the board is out of range, nearest ship is always // The left side of the board is out of range, nearest ship is always
// right // right