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() }