From 4991f0eac8797d7432d67b284099a08e50654e32 Mon Sep 17 00:00:00 2001 From: Oliver Akins Date: Sun, 25 Dec 2022 17:40:13 -0600 Subject: [PATCH] backup 2022-12-25 --- common/src/algorithms/processCard.spec.ts | 161 +++++++++++++++++++--- common/src/algorithms/processCard.ts | 14 +- common/src/types/GameBoard.ts | 6 +- web-svelte/src/stores.ts | 57 +++++++- 4 files changed, 216 insertions(+), 22 deletions(-) diff --git a/common/src/algorithms/processCard.spec.ts b/common/src/algorithms/processCard.spec.ts index aef5df1..37eba34 100644 --- a/common/src/algorithms/processCard.spec.ts +++ b/common/src/algorithms/processCard.spec.ts @@ -1,18 +1,30 @@ -import { tractorBeam } from "./processCard"; +import { tractorBeam, moveShip } from "./processCard"; import { Board } from "../types/GameBoard"; import { expect } from "chai"; import "mocha"; +import { FuelCard } from "../types/FuelCard"; -interface test { - name: string; - args: [number, number]; - input: Board, - expect: Board; +function stringifyBoard(b: Board) { + return [...b].map(x => x != null ? x : `-`).join(`\t`); }; +interface BaseTest { + name: string; + input: Board; + expect: Board; +} + +interface tractorTest extends BaseTest { + args: [number, number]; +} + +interface moveTest extends BaseTest { + args: [number, FuelCard] | [number]; +} + describe("The tractorBeam function", () => { - const tests: test[] = [ + const tests: tractorTest[] = [ { name: `should move the ships closer when equidistant`, args: [4, 1], @@ -85,18 +97,8 @@ describe("The tractorBeam function", () => { input: [ null, null, null, "p1", "p2", "p3", null, null, null ], expect: [ null, null, null, "p3", "p2", "p1", null, null, null ] }, - // { - // name: ``, - // args: [4, 1], - // input: [ ], - // expect: [ ] - // }, ]; - function stringifyBoard(b: Board) { - return [...b].map(x => x != null ? x : `-`).join(`\t`); - }; - for (const test of tests) { it(test.name, () => { let b = [...test.input]; @@ -105,4 +107,127 @@ describe("The tractorBeam function", () => { expect(stringifyBoard(b)).to.equal(stringifyBoard(test.expect)); }); }; -}); \ No newline at end of file +}); + + +describe.only("The moveShip function [non-repulsor tests]", () => { + const tests: moveTest[] = [ + { + name: `should move forward when no other ships present`, + args: [2], + input: [ null, null, "p1", null, null, null, null ], + expect: [ null, null, null, "p1", null, null, null ], + }, + { + name: `should move forwards when closest is ahead of the ship`, + args: [0], + input: [ "p1", null, "p2" ], + expect: [ null, "p1", "p2" ], + }, + { + name: `should move backwards when closest is behind the ship`, + args: [2], + input: [ "p2", null, "p1" ], + expect: [ "p2", "p1", null ], + }, + { + name: `should drift forwards over a single ship`, + args: [2], + input: [ null, null, "p1", "p2", null, null, null, ], + expect: [ null, null, null, "p2", "p1", null, null, ], + }, + { + name: `should drift backwards over a single ship`, + args: [4], + input: [ null, null, null, "p2", "p1", null, null, ], + expect: [ null, null, "p1", "p2", null, null, null, ], + }, + { + name: `should drift forwards over multiple ships`, + args: [2], + input: [ null, null, "p1", "p2", "p3", null, null, ], + expect: [ null, null, null, "p2", "p3", "p1", null, ], + }, + { + name: `should drift backwards over multiple ships`, + args: [4], + input: [ null, null, "p3", "p2", "p1", null, null, ], + expect: [ null, "p1", "p3", "p2", null, null, null, ], + }, + { + name: `shouldn't affect other ships`, + args: [4], + expect: [ null, "p2", null, null, "p1", null, "p3" ], + input: [ null, "p2", null, null, null, "p1", "p3" ], + }, + { + name: `should move away from lower the edge going forwards`, + args: [0], + input: [ "p1", null, "p2", null ], + expect: [ null, "p1", "p2", null ], + }, + { + name: `should move away from upper the edge going backwards`, + args: [3], + input: [ null, "p2", null, "p1" ], + expect: [ null, "p2", "p1", null ], + }, + ]; + + for (const test of tests) { + it(test.name, () => { + let b = [...test.input]; + + if (test.args.length == 1) { + moveShip( + b, + test.args[0], + { + "magnitude": 1, + "symbol": "Ar", + "name": "Argon", + "type": "movement" + } + ); + } else { + moveShip(b, ...test.args); + }; + + expect(b).to.have.length(test.expect.length); + expect(stringifyBoard(b)).to.equal(stringifyBoard(test.expect)); + }); + }; +}); + +describe.only("The moveShip function [repulsor tests]", () => { + const tests: moveTest[] = [ + { + name: `should move backwards when no other ships present`, + args: [3], + input: [ null, null, null, "p1", null, null, null ], + expect: [ null, null, "p1", null, null, null, null ], + }, + ]; + + for (const test of tests) { + it(test.name, () => { + let b = [...test.input]; + if (test.args.length == 1) { + moveShip( + b, + test.args[0], + { + "magnitude": -1, + "symbol": "Ol", + "name": "Oliverium", + "type": "movement" + } + ); + } else { + moveShip(b, ...test.args); + }; + expect(b).to.have.length(test.expect.length); + expect(stringifyBoard(b)).to.equal(stringifyBoard(test.expect)); + }); + }; +}) \ No newline at end of file diff --git a/common/src/algorithms/processCard.ts b/common/src/algorithms/processCard.ts index 5f06fa3..e608629 100644 --- a/common/src/algorithms/processCard.ts +++ b/common/src/algorithms/processCard.ts @@ -1,3 +1,4 @@ +import { determineDirection } from "./movementDirection"; import { Board, GamePiece } from "../types/GameBoard"; import { FuelCard } from "../types/FuelCard"; @@ -66,6 +67,17 @@ export function tractorBeam(board: Board, origin: number, magnitude: number) { }; /** @internal */ -export function moveShip(board: Board, shipLocation: number, fuel: FuelCard) {}; +export function moveShip(board: Board, origin: number, fuel: FuelCard) { + let direction = determineDirection(board, origin); + if (direction == 0) { return }; + + let newIndex = origin + ( fuel.magnitude * direction ); + while (board[newIndex] != null) { + newIndex += direction; + }; + + board[newIndex] = board[origin]; + board[origin] = null; +}; export function processCard(board: Board, shipLocation: number, fuel: FuelCard) {}; \ No newline at end of file diff --git a/common/src/types/GameBoard.ts b/common/src/types/GameBoard.ts index 5660880..156544a 100644 --- a/common/src/types/GameBoard.ts +++ b/common/src/types/GameBoard.ts @@ -1,3 +1,7 @@ export type GamePiece = string | null; -export type Board = GamePiece[]; \ No newline at end of file +export interface Board { + singularity: GamePiece[]; + path: GamePiece[]; + warpgate: GamePiece[]; +} \ No newline at end of file diff --git a/web-svelte/src/stores.ts b/web-svelte/src/stores.ts index d168c6a..91a483f 100644 --- a/web-svelte/src/stores.ts +++ b/web-svelte/src/stores.ts @@ -2,9 +2,62 @@ import type { Board, FuelCard, PlayerData } from "common"; import { writable } from "svelte/store"; export const gameCode = writable(""); -export const state = writable("main-menu"); +export const state = writable("game-movement"); export const myName = writable(""); export const isHost = writable(false); -export const players = writable<{[index: string]: PlayerData}>({}); +export const players = writable<{[index: string]: PlayerData}>({ + a: { + id: `a`, + name: `Player 1`, + host: true, + colour: { name: `green`, hex: `#00aa00` }, + ship: { name: `shuttle`, id: `space-shuttle` }, + fuel: { + "magnitude": 2, + "symbol": "Jo", + "name": "Jodium", + "type": "stationary" + } + }, + b: { + id: `b`, + name: `Player 4`, + host: false, + colour: { name: `green`, hex: `#00a` }, + ship: { name: `shuttle`, id: `rocket` }, + fuel: { + "magnitude": 1, + "symbol": "Ar", + "name": "Argon", + "type": "movement" + } + }, + c: { + id: `c`, + name: `Player 3`, + host: false, + colour: { name: "Magenta", hex: "#b7094c" }, + ship: { name: `shuttle`, id: `rocket` }, + fuel: { + "magnitude": 10, + "symbol": "Mg", + "name": "Magnesium", + "type": "movement" + } + }, + d: { + id: `d`, + name: `Player 2`, + host: false, + colour: { name: "Purple", hex: "#7400b8" }, + ship: { name: `shuttle`, id: `rocket` }, + fuel: { + "magnitude": -2, + "symbol": "Kr", + "name": "Krypton", + "type": "movement" + } + }, +}); export const board = writable(new Array(54).fill(null)); export const hand = writable([]); \ No newline at end of file