Begin implementing the functions that actually modify the game board
This commit is contained in:
parent
54ffc0a0f4
commit
a7344e36e1
3 changed files with 78 additions and 0 deletions
25
common/src/algorithms/processCard.spec.ts
Normal file
25
common/src/algorithms/processCard.spec.ts
Normal file
|
|
@ -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;
|
||||||
|
});
|
||||||
|
});
|
||||||
52
common/src/algorithms/processCard.ts
Normal file
52
common/src/algorithms/processCard.ts
Normal file
|
|
@ -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) {};
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
export { Status } from "./enums/Status";
|
export { Status } from "./enums/Status";
|
||||||
|
|
||||||
// Algorithms
|
// Algorithms
|
||||||
|
export * from "./algorithms/processCard";
|
||||||
export * from "./algorithms/movementDirection";
|
export * from "./algorithms/movementDirection";
|
||||||
|
|
||||||
// Data
|
// Data
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue