@zixial3/arcade-core 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,14 +14,14 @@ A board is a square array of numbers where `0` means empty.
14
14
 
15
15
  ```js
16
16
  import {
17
- createBoard,
18
17
  move,
19
18
  spawn,
20
19
  hasMoves,
21
20
  canMove,
21
+ startBoard,
22
22
  } from "@zixial3/arcade-core";
23
23
 
24
- let board = spawn(spawn(createBoard())); // empty 4×4 board with two starting tiles
24
+ let board = startBoard(); // empty 4×4 board with two starting tiles
25
25
 
26
26
  if (canMove(board, "left")) {
27
27
  board = move(board, "left"); // slide and merge
@@ -42,7 +42,9 @@ Every function returns a **new** board and never mutates its input.
42
42
  | `hasMoves(board)` | `true` if any direction would change the board; `false` means game over. |
43
43
  | `canMove(board, direction)` | `true` if moving in `direction` would change the board. |
44
44
  | `moveWithDetails(board, direction)` | `{ board, moves, merges, points, changed }`: where every tile went, for animations and scoring. |
45
+ | `startBoard(rng?, size?)` | Empty board (default 4×4) with two starting tiles. |
45
46
  | `createBoard(size?)` | Empty board. |
47
+ | `createRng(seed)` | Seeded random number generator to pass as `rng`. Its `state` resumes the same sequence. |
46
48
  | `emptyCells(board)`, `maxTile(board)` | Helpers. |
47
49
 
48
50
  `direction` is `'up' | 'down' | 'left' | 'right'`.
@@ -65,6 +67,19 @@ const { board, points } = moveWithDetails([[2, 2, 4, 4], ...], 'left');
65
67
  // board[0] → [4, 8, 0, 0], points → 12
66
68
  ```
67
69
 
70
+ ### Seeded games
71
+
72
+ ```js
73
+ import { createRng, startBoard, spawn } from "@zixial3/arcade-core";
74
+
75
+ const rng = createRng(1234);
76
+ let board = startBoard(rng); // same starting tiles for seed 1234, every time
77
+ board = spawn(move(board, "up"), rng); // same spawn for the same move
78
+
79
+ const saved = rng.state; // save this…
80
+ const resumed = createRng(saved); // …and pick up exactly where you left off
81
+ ```
82
+
68
83
  ## Development
69
84
 
70
85
  ```bash
package/dist/index.d.ts CHANGED
@@ -55,3 +55,6 @@ export declare function emptyCells(board: Board): Position[];
55
55
  export declare function spawn(board: Board, rng?: Rng): Board;
56
56
  /** The largest tile on the board. */
57
57
  export declare function maxTile(board: Board): number;
58
+ /** An empty board with two starting tiles. */
59
+ export declare function startBoard(rng?: Rng, size?: number): Board;
60
+ export { createRng, type SeededRng } from './rng.js';
package/dist/index.js CHANGED
@@ -105,3 +105,8 @@ export function spawn(board, rng = Math.random) {
105
105
  export function maxTile(board) {
106
106
  return Math.max(0, ...board.flat());
107
107
  }
108
+ /** An empty board with two starting tiles. */
109
+ export function startBoard(rng = Math.random, size = 4) {
110
+ return spawn(spawn(createBoard(size), rng), rng);
111
+ }
112
+ export { createRng } from './rng.js';
package/dist/rng.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type { Rng } from './index.js';
2
+ /** A seeded `Rng` whose current position can be saved and resumed. */
3
+ export interface SeededRng extends Rng {
4
+ /** Pass this to `createRng` to continue the exact same sequence later. */
5
+ readonly state: number;
6
+ }
7
+ /**
8
+ * Creates a deterministic random number generator (mulberry32).
9
+ * The same seed always produces the same sequence, so the same moves
10
+ * always spawn tiles in the same places.
11
+ */
12
+ export declare function createRng(seed: number): SeededRng;
package/dist/rng.js ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Creates a deterministic random number generator (mulberry32).
3
+ * The same seed always produces the same sequence, so the same moves
4
+ * always spawn tiles in the same places.
5
+ */
6
+ export function createRng(seed) {
7
+ let state = seed >>> 0;
8
+ const next = () => {
9
+ state = (state + 0x6d2b79f5) >>> 0;
10
+ let t = state;
11
+ t = Math.imul(t ^ (t >>> 15), t | 1);
12
+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
13
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
14
+ };
15
+ return Object.defineProperty(next, 'state', { get: () => state });
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zixial3/arcade-core",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Pure, framework-free game logic for 2048.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -18,7 +18,8 @@
18
18
  "scripts": {
19
19
  "build": "tsc -p tsconfig.json",
20
20
  "test": "vitest run",
21
- "prepublishOnly": "npm run build && npm test"
21
+ "prepublishOnly": "npm run build && npm test",
22
+ "prebuild": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\""
22
23
  },
23
24
  "keywords": [
24
25
  "2048",