-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
51 lines (43 loc) · 1.26 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict'
export let config = {
X_TOKEN: 'X',
O_TOKEN: 'O',
EMPTY_TOKEN: '.',
GAME_SIZE: 3
}
const range = (length) => Array.from({length}, (e, i) => i)
export function generateEmptyGame() {
const _ = config.EMPTY_TOKEN
const SIZE = config.GAME_SIZE
const matrix = range(SIZE).map(() => range(SIZE).fill(_))
return {matrix, turn: 0}
}
export function reduceTurn(previousState, {position}) {
const X = config.X_TOKEN
const O = config.O_TOKEN
const token = previousState.turn % 2 == 0 ? X : O
const updateIfNotEmptyOnPosition = (cell, coords) => {
return isPositionSame(position, coords) ? token : cell
}
const matrix = matrixMap(previousState.matrix, updateIfPositionIsSame)
return {
matrix,
turn: previousState.turn + 1
}
}
function isPositionSame(pos1, pos2) {
return pos1.every((e, i) => e === pos2[i])
}
function matrixMap(matrix, iterator) {
return matrix.map((row, j) =>
row.map((cell, i) => iterator(matrix[j][i], [i, j]))
)
}
function immutableSetMatrixElement(matrix, coords, element) {
const placeElementOnCoords =
(cell, indexes) => isPositionSame(indexes, coords) ? element : cell
return matrixMap(matrix, placeElementOnCoords)
}
function getMatrixElement(matrix, coords) {
return matrix[coords[]]
}