1
Fork 0

typescript(loopover): feat: added the moveX method to the GameState class

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2019-11-25 17:08:07 +02:00 committed by prescientmoon
parent 6894448f60
commit 73f94f3018
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
6 changed files with 61 additions and 47 deletions

View file

@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}

View file

@ -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<number>, 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<number>) {
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<number>()
slice.shift()
slice.push(first)
} else if (direction === 1) {
const last = slice.last<number>()
slice.unshift(last)
}
for (let i = 0; i < slice.count(); i++) {
list[i + layer * this.width] = slice[i]
}
}
})
return new GameState(newState, this.width)
}
}

View file

@ -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<number>, 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]()
}
}

View file

@ -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)

View file

@ -1 +1,3 @@
export const message = 'hello world' export * from './classes/GameState'
export * from './helpers/createGame'
export * from './types/direction'

View file

@ -7,5 +7,5 @@
"downlevelIteration": true, "downlevelIteration": true,
"target": "es6" "target": "es6"
}, },
"include": ["src"] "include": ["src", "sandbox"]
} }