Add the algorithm for helping with ship movement
This commit is contained in:
parent
5c7afc7668
commit
b109c0d082
2 changed files with 87 additions and 0 deletions
84
common/src/algorithms/movementDirection.ts
Normal file
84
common/src/algorithms/movementDirection.ts
Normal file
|
|
@ -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++;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
|
@ -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";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue