From b109c0d0824145e55e0de7c022964324da6a4376 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sat, 8 Jan 2022 21:34:03 -0600 Subject: [PATCH] Add the algorithm for helping with ship movement --- common/src/algorithms/movementDirection.ts | 84 ++++++++++++++++++++++ common/src/index.ts | 3 + 2 files changed, 87 insertions(+) create mode 100644 common/src/algorithms/movementDirection.ts diff --git a/common/src/algorithms/movementDirection.ts b/common/src/algorithms/movementDirection.ts new file mode 100644 index 0000000..d9f0dda --- /dev/null +++ b/common/src/algorithms/movementDirection.ts @@ -0,0 +1,84 @@ +/** + * A helper method for counting how many ships are on each side of a specific + * ship on the board. + * + * @param board The array of spaces that represents the Gravwell board + * @param shipLocation The index of the ship that we are counting the number of + * ships on each side for. + * @returns An object containing the number of ships that are on each side of + * the target ship + */ +function countShips(board: string[], shipLocation: number) { + return { + left: board.slice(1, shipLocation - 1).filter(x => x != null).length, + right: board.slice(shipLocation + 1).filter(x => x != null).length, + }; +}; + +/** + * The algorithm to determine which direction the closest ship is, this uses an + * integer that can be multiplied by the player's fuel card magnitude in order + * to determine the delta for the board index. + * + * --- + * + * Possible Return Values: + * - `-1` = Away from the Warp Gate + * - `0` = Not moving + * - `1` = Towards the Warp Gate + */ +export function determineDirection(board: string[], shipLocation: number) { + let delta = 1; + + while (true) { + // The left side of the board is out of range, nearest ship is always + // right + if (shipLocation - delta <= 0) { + return 1; + } + + // The right side of the board is out of range, nearest ship is always + // left + else if (shipLocation + delta >= board.length) { + return -1; + } + + // Both sides are still in range, continue + else { + + let left = board[shipLocation - delta]; + let right = board[shipLocation + delta]; + + // Nearest ships are equidistant, fallback to sum + if (left != null && right != null) { + let sum = countShips(board, shipLocation); + + // More ships on the left, going backwards + if (sum.left > sum.right) { + return -1; + } + + // more ships on the right, going forward + else if (sum.left < sum.right) { + return 1; + } + + // Equal number of ships, staying still + else { + return 0; + }; + } + + // Left side of ship is closer + else if (left != null) { + return -1; + } + + // Right side of ship is closer + else if (right != null) { + return 1; + }; + delta++; + }; + }; +}; \ No newline at end of file diff --git a/common/src/index.ts b/common/src/index.ts index f9abf23..c211a31 100644 --- a/common/src/index.ts +++ b/common/src/index.ts @@ -1,6 +1,9 @@ // Enums export { Status } from "./enums/Status"; +// Algorithms + export * from "./algorithms/movementDirection"; + // Data Structures export * from "./types/Colour"; export * from "./types/Spaceship";