Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deprecate moveLimit in favour of minMoves/maxMoves #985

Merged
Merged
5 changes: 4 additions & 1 deletion docs/documentation/api/Game.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@
// Called at the end of each move.
onMove: (G, ctx) => G,

// Prevents ending the turn before a minimum number of moves.
minMoves: 1,

// Ends the turn automatically after a number of moves.
moveLimit: 1,
maxMoves: 1,

// Calls setActivePlayers with this as argument at the
// beginning of the turn.
Expand Down
4 changes: 2 additions & 2 deletions docs/documentation/phases.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function PlayCard(G, ctx) {
const game = {
setup: ctx => ({ deck: 6, hand: Array(ctx.numPlayers).fill(0) }),
moves: { DrawCard, PlayCard },
turn: { moveLimit: 1 },
turn: { minMoves: 1, maxMoves: 1 },
};
```

Expand All @@ -59,7 +59,7 @@ list of moves, which come into effect during that phase:
```js
const game = {
setup: ctx => ({ deck: 6, hand: Array(ctx.numPlayers).fill(0) }),
turn: { moveLimit: 1 },
turn: { minMoves: 1, maxMoves: 1 },

phases: {
draw: {
Expand Down
44 changes: 34 additions & 10 deletions docs/documentation/stages.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,23 @@ setActivePlayers({
...
},

// Prevents manual endStage before the player
// has made the specified number of moves.
minMoves: 1,

// Calls endStage automatically after the player
// has made the specified number of moves.
moveLimit: 5,
maxMoves: 5,

// This takes the stage configuration to the
// value prior to this setActivePlayers call
// once the set of active players becomes empty
// (due to players either calling endStage or
// a moveLimit ending the stage for them).
// maxMoves ending the stage for them).
revert: true,

// A next option will be used once the set of active players
// becomes empty (either by using moveLimit or manually removing
// becomes empty (either by using maxMoves or manually removing
// players).
// All options available inside setActivePlayers are available
// inside next.
Expand All @@ -131,7 +135,7 @@ require every other player to discard a card when we play one:

```js
function PlayCard(G, ctx) {
ctx.events.setActivePlayers({ others: 'discard', moveLimit: 1 });
ctx.events.setActivePlayers({ others: 'discard', minMoves: 1, maxMoves: 1 });
}

const game = {
Expand All @@ -152,21 +156,41 @@ const game = {

#### Advanced Move Limits

Passing a `moveLimit` argument to `setActivePlayers` limits all the
Passing a `minMoves` argument to `setActivePlayers` forces all the
active players to make at least that number of moves before being able to
end the stage, but sometimes you might want to set different move limits
for different players. For cases like this, `setStage` and `setActivePlayers`
support long-form arguments:

```js
setStage({ stage: 'stage-name', minMoves: 3 });
```

```js
setActivePlayers({
currentPlayer: { stage: 'stage-name', minMoves: 2 },
others: { stage: 'stage-name', minMoves: 1 },
value: {
'0': { stage: 'stage-name', minMoves: 4 },
},
});
```

Passing a `maxMoves` argument to `setActivePlayers` limits all the
active players to making that number of moves, but sometimes you might want
to set different move limits for different players. For cases like this,
`setStage` and `setActivePlayers` support long-form arguments:

```js
setStage({ stage: 'stage-name', moveLimit: 3 });
setStage({ stage: 'stage-name', maxMoves: 3 });
```

```js
setActivePlayers({
currentPlayer: { stage: 'stage-name', moveLimit: 2 },
others: { stage: 'stage-name', moveLimit: 1 },
currentPlayer: { stage: 'stage-name', maxMoves: 2 },
others: { stage: 'stage-name', maxMoves: 1 },
value: {
'0': { stage: 'stage-name', moveLimit: 4 },
'0': { stage: 'stage-name', maxMoves: 4 },
},
});
```
Expand Down Expand Up @@ -230,7 +254,7 @@ aren't restricted to any particular stage.

#### ALL_ONCE

Equivalent to `{ all: Stage.NULL, moveLimit: 1 }`. Any player can make
Equivalent to `{ all: Stage.NULL, minMoves: 1, maxMoves: 1 }`. Any player can make
exactly one move before they are removed from the set of active players.

#### OTHERS
Expand Down
8 changes: 5 additions & 3 deletions docs/documentation/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,18 @@ because a player could choose when to end their turn, but in
Tic-Tac-Toe we know that the turn should always end when a move is made.

There are several different ways to manage turns in boardgame.io.
We’ll use the `moveLimit` option in our game definition to tell
We’ll use the `maxMoves` option in our game definition to tell
the framework to automatically end a player’s turn after a single
move has been made.
move has been made, as well as the `minMoves` option, so players
*have* to make a move and can't just `endTurn`.

```js
export const TicTacToe = {
setup: () => { /* ... */ },

turn: {
moveLimit: 1,
minMoves: 1,
maxMoves: 1,
},

moves: { /* ... */ },
Expand Down
2 changes: 1 addition & 1 deletion examples/react-native/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const TicTacToe = {
},
},

turn: { moveLimit: 1 },
turn: { minMoves: 1, maxMoves: 1 },

endIf: (G, ctx) => {
if (IsVictory(G.cells)) {
Expand Down
2 changes: 1 addition & 1 deletion examples/react-web/src/chess/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const ChessGame = {
},
},

turn: { moveLimit: 1 },
turn: { minMoves: 1, maxMoves: 1 },

endIf: (G) => {
const chess = Load(G.pgn);
Expand Down
9 changes: 7 additions & 2 deletions examples/react-web/src/simulator/example-others-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const code = `{
play: (G, ctx) => {
ctx.events.setActivePlayers({
others: 'discard',
moveLimit: 1
minMoves: 1,
maxMoves: 1,
});
return G;
},
Expand Down Expand Up @@ -47,7 +48,11 @@ export default {

moves: {
play: (G, ctx) => {
ctx.events.setActivePlayers({ others: 'discard', moveLimit: 1 });
ctx.events.setActivePlayers({
others: 'discard',
minMoves: 1,
maxMoves: 1,
});
return G;
},
},
Expand Down
3 changes: 2 additions & 1 deletion examples/react-web/src/tic-tac-toe/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const TicTacToe = {
},

turn: {
moveLimit: 1,
minMoves: 1,
maxMoves: 1,
},

endIf: (G, ctx) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/snippets/src/example-2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const TicTacToe = {
},
},

turn: { moveLimit: 1 },
turn: { minMoves: 1, maxMoves: 1 },

endIf: (G, ctx) => {
if (IsVictory(G.cells)) {
Expand Down
2 changes: 1 addition & 1 deletion examples/snippets/src/example-3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const TicTacToe = {
},
},

turn: { moveLimit: 1 },
turn: { minMoves: 1, maxMoves: 1 },

endIf: (G, ctx) => {
if (IsVictory(G.cells)) {
Expand Down
2 changes: 1 addition & 1 deletion examples/snippets/src/multiplayer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const TicTacToe = {
},
},

turn: { moveLimit: 1 },
turn: { minMoves: 1, maxMoves: 1 },

endIf: (G, ctx) => {
if (IsVictory(G.cells)) {
Expand Down
2 changes: 1 addition & 1 deletion examples/snippets/src/phases-1/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function PlayCard(G, ctx) {
const game = {
setup: (ctx) => ({ deck: 6, hand: Array(ctx.numPlayers).fill(0) }),
moves: { DrawCard, PlayCard },
turn: { moveLimit: 1 },
turn: { minMoves: 1, maxMoves: 1 },
};

export default game;
2 changes: 1 addition & 1 deletion examples/snippets/src/phases-2/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const game = {
endIf: (G) => G.deck >= 6,
},
},
turn: { moveLimit: 1 },
turn: { minMoves: 1, maxMoves: 1 },
};

export default game;
2 changes: 1 addition & 1 deletion examples/snippets/src/stages-1/game.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function militia(G, ctx) {
ctx.events.setActivePlayers({ others: 'discard', moveLimit: 1 });
ctx.events.setActivePlayers({ others: 'discard', minMoves: 1, maxMoves: 1 });
}

function discard(G, ctx) {}
Expand Down
5 changes: 3 additions & 2 deletions integration/src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ const TicTacToe = {
},

turn: {
moveLimit: 1,
minMoves: 1,
maxMoves: 1,
},

endIf: (G, ctx) => {
if (IsVictory(G.cells)) {
return { winner: ctx.currentPlayer };
}
if (G.cells.filter(c => c === null).length === 0) {
if (G.cells.filter((c) => c === null).length === 0) {
return { draw: true };
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/ai/ai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const TicTacToe = ProcessGameConfig({
},
},

turn: { moveLimit: 1 },
turn: { minMoves: 1, maxMoves: 1 },

endIf: (G, ctx) => {
if (IsVictory(G.cells)) {
Expand Down
23 changes: 23 additions & 0 deletions src/core/backwards-compatibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type MoveLimitOptions = {
minMoves?: number;
maxMoves?: number;
moveLimit?: number;
};

/**
* Adjust the given options to use the new minMoves/maxMoves if a legacy moveLimit was given
* @param options The options object to apply backwards compatibility to
* @param enforceMinMoves Use moveLimit to set both minMoves and maxMoves
*/
export function supportDeprecatedMoveLimit(
options: MoveLimitOptions,
enforceMinMoves = false
) {
if (options.moveLimit) {
if (enforceMinMoves) {
options.minMoves = options.moveLimit;
}
options.maxMoves = options.moveLimit;
delete options.moveLimit;
}
}
Loading