From 73f94f301829878addcaac3baf7ff44151aae31c Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Mon, 25 Nov 2019 17:08:07 +0200 Subject: [PATCH] typescript(loopover): feat: added the moveX method to the GameState class Signed-off-by: prescientmoon --- typescript/loopover/.vscode/settings.json | 3 ++ typescript/loopover/src/classes/Game.ts | 45 ----------------- typescript/loopover/src/classes/GameState.ts | 48 +++++++++++++++++++ typescript/loopover/src/helpers/createGame.ts | 6 +++ typescript/loopover/src/index.ts | 4 +- typescript/loopover/tsconfig.json | 2 +- 6 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 typescript/loopover/.vscode/settings.json delete mode 100644 typescript/loopover/src/classes/Game.ts create mode 100644 typescript/loopover/src/classes/GameState.ts create mode 100644 typescript/loopover/src/helpers/createGame.ts diff --git a/typescript/loopover/.vscode/settings.json b/typescript/loopover/.vscode/settings.json new file mode 100644 index 0000000..55712c1 --- /dev/null +++ b/typescript/loopover/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "typescript.tsdk": "node_modules/typescript/lib" +} \ No newline at end of file diff --git a/typescript/loopover/src/classes/Game.ts b/typescript/loopover/src/classes/Game.ts deleted file mode 100644 index b3bb66b..0000000 --- a/typescript/loopover/src/classes/Game.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { assert } from '@thi.ng/api' -import { List } from 'immutable' -import { Direction } from '../types/direction' -import { rangeArray } from '../helpers/rangeArray' - -export class GameState { - public height: number - - public constructor(public cells: List, public width: number) { - const height = cells.count() / width - - assert( - height === Math.floor(height), - `Recived non-integer height: ${height}` - ) - - this.height = height - } - - public moveX(direction: Direction, layers: Iterable) { - const newState = this.cells.withMutations(list => { - for (const layer of layers) { - const slice = list.slice( - layer * this.width, - (layer + 1) * this.width - ) - - if (direction === -1) { - const first = slice.first() - slice.shift() - slice.push(first) - } else if (direction === 1) { - const last = slice.last() - slice.unshift(last) - } - - for (let i = 0; i < slice.count(); i++) { - list[i + layer * this.width] = slice[i] - } - } - }) - - return new GameState(newState, this.width) - } -} diff --git a/typescript/loopover/src/classes/GameState.ts b/typescript/loopover/src/classes/GameState.ts new file mode 100644 index 0000000..cbec344 --- /dev/null +++ b/typescript/loopover/src/classes/GameState.ts @@ -0,0 +1,48 @@ +import { assert } from '@thi.ng/api' +import { List } from 'immutable' +import { Direction } from '../types/direction' + +export class GameState { + public height: number + + public constructor(private cellList: List, public width: number) { + const height = cellList.count() / width + + assert( + height === Math.floor(height), + `Recived non-integer height: ${height}` + ) + + this.height = height + } + + public moveX(direction: Direction, layer: number) { + const minimumLayerIndex = layer * this.width + + const newState = this.cellList.reduce((accumulated, current, index) => { + if ( + index < minimumLayerIndex || + index >= (layer + 1) * this.width + ) { + return accumulated + } + + const copyFrom = + minimumLayerIndex + + ((index - minimumLayerIndex - direction + this.width) % + this.width) + + return accumulated.set(index, this.cellList.get(copyFrom)!) + }, this.cellList) + + return new GameState(newState, this.width) + } + + public get cells() { + return this.cellList.toArray() + } + + [Symbol.iterator]() { + return this.cellList[Symbol.iterator]() + } +} diff --git a/typescript/loopover/src/helpers/createGame.ts b/typescript/loopover/src/helpers/createGame.ts new file mode 100644 index 0000000..0b8ed19 --- /dev/null +++ b/typescript/loopover/src/helpers/createGame.ts @@ -0,0 +1,6 @@ +import { rangeArray } from './rangeArray' +import { GameState } from '../classes/GameState' +import { List } from 'immutable' + +export const createGame = (width: number, height: number) => + new GameState(List(rangeArray(0, width * height)), width) diff --git a/typescript/loopover/src/index.ts b/typescript/loopover/src/index.ts index fa31013..51c6dae 100644 --- a/typescript/loopover/src/index.ts +++ b/typescript/loopover/src/index.ts @@ -1 +1,3 @@ -export const message = 'hello world' +export * from './classes/GameState' +export * from './helpers/createGame' +export * from './types/direction' diff --git a/typescript/loopover/tsconfig.json b/typescript/loopover/tsconfig.json index 2a01362..ee2ac52 100644 --- a/typescript/loopover/tsconfig.json +++ b/typescript/loopover/tsconfig.json @@ -7,5 +7,5 @@ "downlevelIteration": true, "target": "es6" }, - "include": ["src"] + "include": ["src", "sandbox"] }