Skip to content

Commit 9324c58

Browse files
jstacodernicolodavis
authored andcommitted
Allowed moves as function (#164)
* initial implementation of allowedMoves as function * upated an allowed moves test to use a function * added mentions of allowedMoves as function in docs * update Game.md
1 parent 45f3757 commit 9324c58

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

docs/api/Game.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ const game = Game({
119119
onTurnEnd: ...
120120
onPhaseBegin: ...
121121
onPhaseEnd: ...
122+
allowedMoves: ...
122123
...
123124
},
124125
{
@@ -129,6 +130,7 @@ const game = Game({
129130
onTurnEnd: ...
130131
onPhaseBegin: ...
131132
onPhaseEnd: ...
133+
allowedMoves: ...
132134
...
133135
},
134136
]

src/core/flow.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ export function Flow({
217217
* // A phase-specific movesPerTurn.
218218
* movesPerTurn: integer,
219219
*
220-
* // List of moves that are allowed in this phase.
221-
* allowedMoves: ['moveA', ...],
220+
* // List of moves or a function that returns a list of moves
221+
* // that are allowed in this phase.
222+
* allowedMoves: (G, ctx) => ['moveA', ...],
222223
* }
223224
*/
224225
export function FlowWithPhases({
@@ -295,6 +296,10 @@ export function FlowWithPhases({
295296
if (conf.turnOrder === undefined) {
296297
conf.turnOrder = turnOrder;
297298
}
299+
if (conf.allowedMoves && typeof conf.allowedMoves != 'function') {
300+
const { allowedMoves } = conf;
301+
conf.allowedMoves = () => allowedMoves;
302+
}
298303
}
299304

300305
const endTurnIfWrap = (G, ctx) => {
@@ -542,7 +547,7 @@ export function FlowWithPhases({
542547
const canMakeMoveWrap = (G, ctx, opts) => {
543548
const conf = phaseMap[ctx.phase] || {};
544549
if (conf.allowedMoves) {
545-
const set = new Set(conf.allowedMoves);
550+
const set = new Set(conf.allowedMoves({ G, ctx }));
546551
if (!set.has(opts.type)) {
547552
return false;
548553
}

src/core/flow.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ test('canMakeMove', () => {
419419

420420
flow: {
421421
phases: [
422-
{ name: 'A', allowedMoves: 'A' },
422+
{ name: 'A', allowedMoves: () => ['A'] },
423423
{ name: 'B', allowedMoves: 'B' },
424424
{ name: 'C' },
425425
],

0 commit comments

Comments
 (0)