Skip to content

Commit a8431c7

Browse files
committed
set default RNG seed once per game, not game type
1 parent 1a24791 commit a8431c7

4 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/core/game.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import { FlowWithPhases } from './flow';
10-
import { Random } from './random';
1110

1211
/**
1312
* Game
@@ -76,7 +75,6 @@ function Game({ name, setup, moves, playerView, flow, seed }) {
7675
if (setup === undefined) setup = () => ({});
7776
if (moves === undefined) moves = {};
7877
if (playerView === undefined) playerView = G => G;
79-
if (seed === undefined) seed = Random.seed();
8078

8179
if (!flow || flow.processGameEvent === undefined) {
8280
flow = FlowWithPhases(flow || {});

src/core/random.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,21 @@ test('Random API is not executed optimisitically', () => {
148148
}
149149
});
150150

151-
test('onTurnBegin has the Random API at the beginning of the game', () => {
152-
let random;
151+
test('onTurnBegin has ctx APIs at the beginning of the game', () => {
152+
let random = null;
153+
let events = null;
154+
153155
const game = Game({
154156
flow: {
155157
onTurnBegin: (G, ctx) => {
156158
random = ctx.random;
159+
events = ctx.events;
157160
},
158161
},
159162
});
163+
160164
const reducer = CreateGameReducer({ game });
161165
reducer(undefined, { type: 'init' });
162-
expect(random).toBeDefined();
166+
expect(random).not.toBe(null);
167+
expect(events).not.toBe(null);
163168
});

src/core/reducer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ export function CreateGameReducer({ game, numPlayers, multiplayer }) {
2424
}
2525

2626
let ctx = game.flow.ctx(numPlayers);
27-
ctx._random = { seed: game.seed };
27+
28+
let seed = game.seed;
29+
if (seed === undefined) {
30+
seed = Random.seed();
31+
}
32+
ctx._random = { seed };
2833

2934
const random = new Random(ctx);
3035
let ctxWithAPI = random.attach(ctx);

src/core/reducer.test.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ test('log', () => {
205205
expect(state.log).toEqual([actionA, actionB, actionC]);
206206
});
207207

208-
test('using Random inside setup()', () => {
208+
describe('Random inside setup()', () => {
209209
const game1 = Game({
210210
seed: 'seed1',
211211
setup: ctx => ({ n: ctx.random.D6() }),
@@ -221,17 +221,29 @@ test('using Random inside setup()', () => {
221221
setup: ctx => ({ n: ctx.random.D6() }),
222222
});
223223

224-
const reducer1 = CreateGameReducer({ game: game1 });
225-
const state1 = reducer1(undefined, makeMove());
224+
const game4 = Game({
225+
setup: ctx => ({ n: ctx.random.D6() }),
226+
});
227+
228+
test('setting seed', () => {
229+
const reducer1 = CreateGameReducer({ game: game1 });
230+
const state1 = reducer1(undefined, makeMove());
226231

227-
const reducer2 = CreateGameReducer({ game: game2 });
228-
const state2 = reducer2(undefined, makeMove());
232+
const reducer2 = CreateGameReducer({ game: game2 });
233+
const state2 = reducer2(undefined, makeMove());
229234

230-
const reducer3 = CreateGameReducer({ game: game3 });
231-
const state3 = reducer3(undefined, makeMove());
235+
const reducer3 = CreateGameReducer({ game: game3 });
236+
const state3 = reducer3(undefined, makeMove());
232237

233-
expect(state1.G.n).not.toBe(state2.G.n);
234-
expect(state2.G.n).toBe(state3.G.n);
238+
expect(state1.G.n).not.toBe(state2.G.n);
239+
expect(state2.G.n).toBe(state3.G.n);
240+
});
241+
242+
test('not setting seed sets a default', () => {
243+
const reducer = CreateGameReducer({ game: game4 });
244+
const state = reducer(undefined, makeMove());
245+
expect(state.ctx._random.seed).toBeDefined();
246+
});
235247
});
236248

237249
test('undo / redo', () => {

0 commit comments

Comments
 (0)