From 670729c892d645f4b6249335fda83a62c5607ebe Mon Sep 17 00:00:00 2001 From: Oliver Akins Date: Wed, 22 Jun 2022 00:35:03 -0600 Subject: [PATCH] Apply a fix to the determineDirection function that makes it conform to the rule specification --- common/src/algorithms/movementDirection.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/common/src/algorithms/movementDirection.ts b/common/src/algorithms/movementDirection.ts index 29f34ae..9348a82 100644 --- a/common/src/algorithms/movementDirection.ts +++ b/common/src/algorithms/movementDirection.ts @@ -40,8 +40,27 @@ export function countShips(board: any[], shipLocation: number) { * - `1` = Towards the Warp Gate */ 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) { // The left side of the board is out of range, nearest ship is always // right