From 90b404f68b46aa3c19de5293e65c06698d7a4855 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Mon, 25 Nov 2019 17:31:07 +0200 Subject: [PATCH] typescript(loopover): feat: added the moveY method to the GameState class Signed-off-by: prescientmoon --- typescript/loopover/src/classes/GameState.ts | 24 +++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/typescript/loopover/src/classes/GameState.ts b/typescript/loopover/src/classes/GameState.ts index cbec344..17d4a00 100644 --- a/typescript/loopover/src/classes/GameState.ts +++ b/typescript/loopover/src/classes/GameState.ts @@ -19,7 +19,7 @@ export class GameState { public moveX(direction: Direction, layer: number) { const minimumLayerIndex = layer * this.width - const newState = this.cellList.reduce((accumulated, current, index) => { + const newState = this.cellList.reduce((accumulated, _, index) => { if ( index < minimumLayerIndex || index >= (layer + 1) * this.width @@ -38,6 +38,28 @@ export class GameState { return new GameState(newState, this.width) } + public moveY(direction: Direction, layer: number) { + const minimumLayerIndex = layer * this.width + + const newState = this.cellList.reduce((accumulated, current, index) => { + const currentX = index % this.width + const currentY = Math.floor(index / this.width) + + if (currentX !== layer) { + return accumulated + } + + const copyFrom = + currentX + + this.width * + ((currentY + this.height - direction) % this.height) + + return accumulated.set(index, this.cellList.get(copyFrom)!) + }, this.cellList) + + return new GameState(newState, this.width) + } + public get cells() { return this.cellList.toArray() }