From a7344e36e10309f2d32cf81868fc4f13451783b0 Mon Sep 17 00:00:00 2001 From: Oliver Akins Date: Tue, 28 Jun 2022 00:44:44 -0600 Subject: [PATCH] Begin implementing the functions that actually modify the game board --- common/src/algorithms/processCard.spec.ts | 25 +++++++++++ common/src/algorithms/processCard.ts | 52 +++++++++++++++++++++++ common/src/index.ts | 1 + 3 files changed, 78 insertions(+) create mode 100644 common/src/algorithms/processCard.spec.ts create mode 100644 common/src/algorithms/processCard.ts diff --git a/common/src/algorithms/processCard.spec.ts b/common/src/algorithms/processCard.spec.ts new file mode 100644 index 0000000..542fe7c --- /dev/null +++ b/common/src/algorithms/processCard.spec.ts @@ -0,0 +1,25 @@ +import { tractorBeam } from "./processCard"; +import { expect } from "chai"; +import "mocha"; + +describe("The tractorBeam function", () => { + it("should move the ships closer", () => { + let b = [ + null, "p1", null, null, "p2", null, null, "p3", null + ]; + let nb = [...b]; + tractorBeam(nb, 4, 1); + + expect(nb).to.have.length(9); + + expect(nb[0]).to.be.null; + expect(nb[1]).to.be.null; + expect(nb[2]).to.equal("p1"); + expect(nb[3]).to.be.null; + expect(nb[4]).to.equal("p2"); + expect(nb[5]).to.be.null; + expect(nb[6]).to.equal("p3"); + expect(nb[7]).to.be.null; + expect(nb[8]).to.be.null; + }); +}); \ No newline at end of file diff --git a/common/src/algorithms/processCard.ts b/common/src/algorithms/processCard.ts new file mode 100644 index 0000000..ccbacb9 --- /dev/null +++ b/common/src/algorithms/processCard.ts @@ -0,0 +1,52 @@ +import { FuelCard } from "../types/FuelCard"; +import { Board } from "../types/GameBoard"; + +/** @internal */ +export function tractorBeam(board: Board, origin: number, magnitude: number) { + let delta = 0; + while (true) { + delta++; + + let behind = origin - delta; + let ahead = origin + delta; + + /* + Check for a ship behind the origin, if there is a ship there then pull + it towards the origin (forwards). If it were to collide with another + ship, then drift until it no longer collides. + */ + if (behind >= 0 && board[behind] != null) { + let ship = board[behind]; + let localMagnitude = magnitude; + while (board[behind + localMagnitude] != null) { + localMagnitude++; + }; + board[behind + localMagnitude] = ship + board[behind] = null; + }; + + /* + Check for a ship ahead of the origin, if there is a ship there then + pull it towards the origin (backwards). If it were to collide with + another ship, then drift until it no longer collides. + */ + if (ahead < board.length && board[ahead] != null) { + let ship = board[ahead]; + let localMagnitude = magnitude; + while (board[ahead - localMagnitude] != null) { + localMagnitude++; + }; + board[ahead - localMagnitude] = ship + board[ahead] = null; + }; + + if (behind < 0 && ahead >= board.length) { + break; + }; + }; +}; + +/** @internal */ +export function moveShip(board: Board, shipLocation: number, fuel: FuelCard) {}; + +export function processCard(board: Board, shipLocation: number, fuel: FuelCard) {}; \ No newline at end of file diff --git a/common/src/index.ts b/common/src/index.ts index 1e7f2d7..876227f 100644 --- a/common/src/index.ts +++ b/common/src/index.ts @@ -2,6 +2,7 @@ export { Status } from "./enums/Status"; // Algorithms + export * from "./algorithms/processCard"; export * from "./algorithms/movementDirection"; // Data