| 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 |
1x
1x
1x
1x
1x
1x
1x
1x
47x
74x
115x
53x
65x
61x
43x
14x
11x
11x
2x
2x
20x
20x
10x
10x
1x
1x
1x
1x
1x
1x
29x
25x
12x
2x
22x
19x
1x
5x
6x
40x
2x
2x
30x
2x
2x
23x
2x
1x
19x
10x
9x
8x
8x
5x
4x
1x
1x
6x
2x
1x
1x
1x
| import { Graph, } from 'graph-curry';
import { Components, Filter, } from 'game_grid';
import { claim, } from './node';
import { genNodes, } from './board';
import { hasID, id, player, sameID, updatePlayer, xMatches, } from './player';
import { anyExceed, byExcess, byPlayer, lastFree, replace, } from './filter';
const { graph } = Graph;
const { omniComps } = Components;
const { byCol, } = Filter;
const dCol = 0;
const dPlr = [ player('player0', 0, 0), player('player1', 0, 1) ];
const dNod = genNodes(7, 6);
const init = ({ column: 0, nodes: genNodes(), players: dPlr, inPlay: false, min: 3 });
export const game =
(players = dPlr, nodes = dNod, column = 0, inPlay = false, min = 3) =>
({ players, nodes, column, inPlay, min });
export const column = ({ column } = init) => column;
export const nodes = ({ nodes } = init) => nodes;
export const inPlay = ({ inPlay } = init) => inPlay;
export const min = ({ min } = init) => min;
export const players = ({ players } = init) => players;
export const active = ({ players: [ active, passive ] } = init) => active;
export const passive = ({ players: [ active, passive ] } = init) => passive;
export const setNodes = nArr => g =>
game(players(g), nArr, column(g), inPlay(g), min(g));
export const setColumn = col => g =>
game(players(g), nodes(g), col, inPlay(g), min(g));
export const setPlayState = bool => g =>
game(players(g), nodes(g), column(g), !!bool, min(g));
export const setPlayers = pArr => g =>
game(pArr, nodes(g), column(g), inPlay(g), min(g));
export const setMin = m => g =>
game(players(g), nodes(g), column(g), inPlay(g), m);
export const copy = g =>
game(players(g), nodes(g), column(g), inPlay(g), min(g));
export const resetGame = g => game(players(g));
export const board = g => graph(...nodes(g));
export const colNodes = g => byCol(nodes(g))(column(g));
export const next = g => lastFree(colNodes(g));
export const start = g => setPlayState(true)(g);
export const stop = g => setPlayState(false)(g);
export const canPlay = g => inPlay(g) && !!next(g);
export const locked = g => !canPlay(g);
export const toggleState = g => setPlayState(!inPlay(g))(g);
export const togglePlayers = g => setPlayers([ passive(g), active(g) ])(g);
export const isActive = p => g => sameID(p)(active(g));
export const playerNodes = g => p => byPlayer(nodes(g))(id(p));
export const actNodes = g => playerNodes(g)(active(g));
export const passNodes = g => playerNodes(g)(passive(g));
export const playerGraph = g => p => graph(...playerNodes(g)(p));
export const actGraph = g => playerGraph(g)(active(g));
export const passGraph = g => playerGraph(g)(passive(g));
export const playerComps = g => p => byExcess(1)(omniComps(playerGraph(g)(p)));
export const actComps = g => playerComps(g)(active(g));
export const passComps = g => playerComps(g)(passive(g));
export const isWinner = g => p => anyExceed(min(g))(playerComps(g)(p));
export const winner = g => players(g).find(isWinner(g));
export const endIfWon = g => winner(g) ? stop(g) : g;
export const claimSwap = g => replace(claim(id(active(g)))(next(g)))(nodes(g));
export const claimNext = g => locked(g) ? g : endIfWon(setNodes(claimSwap(g))(g));
export const select = g => locked(g) ? g : togglePlayers(claimNext(g));
export const pSelect = p => g => isActive(p)(g) ? select(g) : g;
export const playerByID = i => g => players(g).find(hasID(i));
export const findPlr = p => g => players(g).find(sameID(p));
export const hasPlr = p => g => players(g).some(sameID(p));
export const mendPlr = p => g => setPlayers(players(g).map(updatePlayer(p)))(g);
export const pushPlr = p => g => setPlayers(players(g).concat(p))(g);
export const addPlr = p => g => hasPlr(p)(g) ? mendPlr(p)(g) : pushPlr(p)(g);
export const rmPlr = p => g => setPlayers((players(g)).filter(xMatches(p)))(g);
// export const canPlay
// export const claimNode = p=>g=> !isActive(p)(g)? g:setPlayers()
// export const isActive=player=>game=>id
// "playerByID findPlr hasPlr mendPlr pushPlr"
|