From 23ca24db3e0a84066bc14af0841f0ba87d5141d5 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sat, 23 Oct 2021 23:15:45 +0200 Subject: Add PRNG seed to game state. Log all game actions to a table so they can be replayed. --- rules.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'rules.js') diff --git a/rules.js b/rules.js index bab18a1..867c432 100644 --- a/rules.js +++ b/rules.js @@ -35,6 +35,10 @@ const RESERVE_MARK = ""; let game = null; +function random(n) { + return Math.floor(((game.seed = game.seed * 48271 % 0x7fffffff) / 0x7fffffff) * n); +} + function log(...args) { let s = Array.from(args).join(""); game.log.push(s); @@ -171,7 +175,7 @@ function edge_id(A, B) { } function roll_d6() { - return Math.floor(Math.random() * 6) + 1; + return random(6) + 1; } function reset_deck() { @@ -184,7 +188,7 @@ function reset_deck() { function deal_cards(deck, n) { let hand = []; for (let i = 0; i < n; ++i) { - let c = Math.floor(Math.random() * deck.length); + let c = random(deck.length); hand.push(deck[c]); deck.splice(c, 1); } @@ -1138,7 +1142,7 @@ states.jupiter = { for (let x in BLOCKS) if (game.location[x] === where) list.push(x); - let i = Math.floor(Math.random() * list.length); + let i = random(list.length); jupiter_block(list[i]); }, secret: function (args) { @@ -1151,7 +1155,7 @@ states.jupiter = { for (let b in BLOCKS) if (game.location[b] === where && BLOCKS[b].owner === owner) list.push(b); - let i = Math.floor(Math.random() * list.length); + let i = random(list.length); jupiter_block(list[i]); } }, @@ -2376,8 +2380,9 @@ exports.ready = function (scenario, players) { return players.length === 2; } -exports.setup = function (scenario) { +exports.setup = function (seed, scenario) { game = { + seed: seed, tournament: (scenario === "Tournament"), c_hand: [], p_hand: [], -- cgit v1.2.3