typescript(loopover): feat: added the moveX method to the GameState class
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
6894448f60
commit
73f94f3018
3
typescript/loopover/.vscode/settings.json
vendored
Normal file
3
typescript/loopover/.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"typescript.tsdk": "node_modules/typescript/lib"
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
48
typescript/loopover/src/classes/GameState.ts
Normal file
48
typescript/loopover/src/classes/GameState.ts
Normal 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]()
|
||||
}
|
||||
}
|
6
typescript/loopover/src/helpers/createGame.ts
Normal file
6
typescript/loopover/src/helpers/createGame.ts
Normal 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)
|
|
@ -1 +1,3 @@
|
|||
export const message = 'hello world'
|
||||
export * from './classes/GameState'
|
||||
export * from './helpers/createGame'
|
||||
export * from './types/direction'
|
||||
|
|
|
@ -7,5 +7,5 @@
|
|||
"downlevelIteration": true,
|
||||
"target": "es6"
|
||||
},
|
||||
"include": ["src"]
|
||||
"include": ["src", "sandbox"]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue