Skip to content

Commit 45599e5

Browse files
philihpnicolodavis
authored andcommitted
Server-side array shuffling. (#116)
* deterministic array shuffling * use fast-shuffle for faster shuffle * fix rollup config
1 parent 1a3ced2 commit 45599e5

13 files changed

Lines changed: 165 additions & 1 deletion

File tree

docs/random.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@ const SomeGame = Game({
5050
This will place a request to a D6 dice roll inside `G`.
5151
While processing the move, the request gets evaluated and the result placed into `diceValue`, where it can be used.
5252

53+
### Shuffles
54+
55+
To simulate a shuffled deck of cards, sometimes it might be simplest to draw a card from a random index in an array. This can get confusing if your game moves
56+
include a move for peeking at the top card which is later rendered moot by
57+
another move which shuffles the deck. To simplify modeling this, you might want
58+
to represent your deck as an array. Use `Random.Shuffle` to perform a shallow
59+
shuffle of the elements.
60+
61+
```js
62+
import { Random } from 'boardgame.io/core';
63+
64+
const SomeGame = Game({
65+
setup: () => ({
66+
deck: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
67+
}),
68+
moves: {
69+
shuffleDeck(G) {
70+
return Random.Shuffle(G, 'deck');
71+
},
72+
},
73+
});
74+
```
75+
5376
## Seed
5477

5578
The library uses a `seed` in `ctx` that is stripped before it

docs/react/boardgameio.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/modules/routes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import tic_tac_toe from './tic-tac-toe';
1010
import chess from './chess';
1111
import phases from './phases';
1212
import liars_dice from './liars-dice';
13+
import shuffle from './shuffle';
1314

1415
const routes = [
1516
{
@@ -28,6 +29,10 @@ const routes = [
2829
name: 'Liars Dice',
2930
routes: liars_dice.routes,
3031
},
32+
{
33+
name: 'Shuffle',
34+
routes: shuffle.routes,
35+
},
3136
];
3237

3338
export default routes;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2018 The boardgame.io Authors.
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import React from 'react';
10+
import PropTypes from 'prop-types';
11+
12+
const Board = ({ G }) => <pre>{JSON.stringify(G, null, 2)}</pre>;
13+
14+
Board.propTypes = {
15+
G: PropTypes.any.isRequired,
16+
};
17+
18+
export default Board;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2018 The boardgame.io Authors.
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import React from 'react';
10+
import { Client } from 'boardgame.io/client';
11+
import Game from '../game';
12+
import Board from './board';
13+
14+
const App = Client({
15+
game: Game,
16+
numPlayers: 1,
17+
board: Board,
18+
});
19+
20+
const SingleView = () => (
21+
<div style={{ padding: 50 }}>
22+
<App gameID="Shuffle" />
23+
</div>
24+
);
25+
26+
export default SingleView;

examples/modules/shuffle/game.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2018 The boardgame.io Authors
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import { Game, Random } from 'boardgame.io/core';
10+
11+
const Shuffle = Game({
12+
name: 'shuffle',
13+
14+
setup: () => ({
15+
deck: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
16+
}),
17+
18+
moves: {
19+
shuffle: G => Random.Shuffle(G, 'deck'),
20+
},
21+
});
22+
23+
export default Shuffle;

examples/modules/shuffle/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright 2018 The boardgame.io Authors.
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import routes from './routes';
10+
11+
// Any other additional setup for this this module
12+
export default {
13+
routes,
14+
};

examples/modules/shuffle/routes.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2018 The boardgame.io Authors.
3+
*
4+
* Use of this source code is governed by a MIT-style
5+
* license that can be found in the LICENSE file or at
6+
* https://opensource.org/licenses/MIT.
7+
*/
8+
9+
import View from './components/view';
10+
11+
const routes = [
12+
{
13+
path: '/shuffle/cards',
14+
text: 'Shuffle Cards',
15+
component: View,
16+
},
17+
];
18+
19+
export default routes;

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"webpack": "^3.5.5"
110110
},
111111
"dependencies": {
112+
"fast-shuffle": "^1.0.4",
112113
"koa": "^2.3.0",
113114
"koa-router": "^7.2.1",
114115
"koa-socket": "^4.4.0",

0 commit comments

Comments
 (0)