Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

tiles.test.ts

77 lines3.0 KB

1import { describe, expect, it } from "vitest"
2import { GameState } from "../../../shared/game-state"
3import { boxCoordsToId, coordsToEdgeId, EDGE_COUNT, getBoxEdges } from "../../../shared/edge-math"
4import {
5 TILE_COUNT,
6 emptyTile,
7 edgeLocation,
8 getOwner,
9 setOwner,
10 visitTileEdges,
11 visitTileBoxes,
12} from "../../../shared/tiles"
13import type { BoardTile } from "../../../shared/tiles"
14​
15describe("packed board tiles", () => {
16 it("packs owners across byte boundaries without changing neighboring owners", () => {
17 const data = new Uint8Array(54)
18 for (let i = 0; i < 144; i++) setOwner(data, i, i % 5)
19 for (let i = 0; i < 144; i++) expect(getOwner(data, i)).toBe(i % 5)
20 setOwner(data, 2, 4)
21 expect(getOwner(data, 1)).toBe(1)
22 expect(getOwner(data, 2)).toBe(4)
23 expect(getOwner(data, 3)).toBe(3)
24 expect(() => setOwner(data, 144, 1)).toThrow()
25 })
26 it("owns every edge exactly once, including the outer boundary", () => {
27 const seen = new Uint8Array(EDGE_COUNT)
28 let count = 0,
29 bytes = 0
30 for (let tileId = 0; tileId < TILE_COUNT; tileId++) {
31 const tile = emptyTile(tileId)
32 bytes += tile.edges.length + tile.boxes.length
33 visitTileEdges(tileId, (id, index) => {
34 const location = edgeLocation(id)
35 if (
36 seen[id] ||
37 location.tileId !== tileId ||
38 location.index !== index ||
39 index * 3 + 3 > tile.edges.length * 8
40 )
41 throw new Error(`Incorrect ownership for edge ${id}`)
42 seen[id] = 1
43 count++
44 })
45 }
46 expect(count).toBe(EDGE_COUNT)
47 expect(bytes).toBe(1125750)
48 })
49 it("hydrates claims from unordered tiles and clears authoritative owners on reset", () => {
50 const expected = new GameState()
51 const shared = coordsToEdgeId(8, 7, false)
52 const edges = new Set([...getBoxEdges(7, 7), ...getBoxEdges(8, 7)])
53 for (const edgeId of edges) if (edgeId !== shared) expected.applyEvent({ edgeId, teamId: 0 })
54 expected.applyEvent({ edgeId: shared, teamId: 3 })
55 const tiles = new Map<number, BoardTile>()
56 for (const edgeId of edges)
57 tiles.set(edgeLocation(edgeId).tileId, emptyTile(edgeLocation(edgeId).tileId))
58 for (const tile of tiles.values()) {
59 visitTileEdges(tile.tile_id, (id, index) =>
60 setOwner(tile.edges, index, (expected.getEdgeOwner(id) ?? -1) + 1),
61 )
62 visitTileBoxes(tile.tile_id, (id, index) =>
63 setOwner(tile.boxes, index, expected.getBoxOwner(id)),
64 )
65 }
66 const actual = new GameState()
67 for (const tile of [...tiles.values()].reverse()) actual.applyTile(tile)
68 expect(actual.getScores()).toEqual([0, 0, 0, 2])
69 expect(actual.getBoxOwner(boxCoordsToId(7, 7))).toBe(4)
70 expect(actual.getEdgesPlacedCount()).toBe(7)
71 for (const tile of tiles.values()) actual.applyTile(tile)
72 expect(actual.getScores()).toEqual(expected.getScores())
73 for (const tile of tiles.values()) actual.applyTile(emptyTile(tile.tile_id))
74 expect(actual.getScores()).toEqual([0, 0, 0, 0])
75 expect(actual.getEdgesPlacedCount()).toBe(0)
76 })
77})