From 30117a327b7507ec6642e061798edb940dc85022 Mon Sep 17 00:00:00 2001 From: Brian Kim Date: Wed, 31 May 2023 14:41:19 -0700 Subject: [PATCH] tweakas --- website/examples/minesweeper.ts | 115 +++++++++++++++++--------------- website/package-lock.json | 4 +- 2 files changed, 66 insertions(+), 53 deletions(-) diff --git a/website/examples/minesweeper.ts b/website/examples/minesweeper.ts index 5980b02c..12ea9657 100644 --- a/website/examples/minesweeper.ts +++ b/website/examples/minesweeper.ts @@ -2,16 +2,16 @@ import {jsx} from "@b9g/crank/standalone"; import {renderer} from "@b9g/crank/dom"; function Hexagon({cx = 0, cy = 0, r, ...props}) { - if (!r) { - return null; - } + if (!r) { + return null; + } - const points = []; - for (let i = 0, a = 0; i < 6; i++, a += Math.PI / 3) { - points.push([cx + Math.cos(a) * r, cy + Math.sin(a) * r]); - } + const points = []; + for (let i = 0, a = 0; i < 6; i++, a += Math.PI / 3) { + points.push([cx + Math.cos(a) * r, cy + Math.sin(a) * r]); + } - return jsx` + return jsx` { - const onclick = () => { - console.log(neighborsOf(cell, cells)); - }; - const {cx, cy} = centerCoordsFor(cell, radius); - const {q, r} = axialCoordsFor(cell); - return jsx` + return cells.map((cell, i) => { + const onclick = () => { + console.log(neighborsOf(cell, cells)); + }; + const {cx, cy} = centerCoordsFor(cell, radius); + const {q, r} = axialCoordsFor(cell); + return jsx` <${Hexagon} r=${radius} cx=${cx} cy=${cy} @@ -67,47 +67,58 @@ function HexagonalGrid({radius = 20, cells, testCell}) { >${q},${r} --> `; - }); + }); } function shuffle(arr) { - for (let i = arr.length - 1; i > 0; i--) { - const j = Math.floor(Math.random() * (i + 1)); - [arr[i], arr[j]] = [arr[j], arr[i]]; - } + for (let i = arr.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [arr[i], arr[j]] = [arr[j], arr[i]]; + } - return arr; + return arr; } function neighborsOf(cell, cells) { - const {q, r} = axialCoordsFor(cell); - const vectors = [[1, 0], [0, 1], [-1, 1], [-1, 0], [0, -1], [1, -1]]; - const axialSet = new Set(vectors.map(([q1, r1]) => `${q + q1},${r + r1}`)); - return cells.filter((cell1) => { - const {q, r} = axialCoordsFor(cell1); - return axialSet.has(`${q},${r}`); - }); + const {q, r} = axialCoordsFor(cell); + const vectors = [ + [1, 0], + [0, 1], + [-1, 1], + [-1, 0], + [0, -1], + [1, -1], + ]; + const axialSet = new Set(vectors.map(([q1, r1]) => `${q + q1},${r + r1}`)); + return cells.filter((cell1) => { + const {q, r} = axialCoordsFor(cell1); + return axialSet.has(`${q},${r}`); + }); } -function *Minesweeper() { - const rows = 12, cols = 15; - const cells = []; - for (let row = 0; row < rows; row++) { - for (let col = 0; col < cols; col++) { - cells.push({row, col, bomb: false, bombCount: 0}); - } - } +function* Minesweeper() { + const rows = 12, + cols = 15; + const cells = []; + for (let row = 0; row < rows; row++) { + for (let col = 0; col < cols; col++) { + cells.push({row, col, bomb: false, bombCount: 0}); + } + } - for (const cell of shuffle(cells.slice()).slice(0, 30)) { - cell.bomb = true; - } + for (const cell of shuffle(cells.slice()).slice(0, 30)) { + cell.bomb = true; + } - for (const cell of cells) { - cell.bombCount = neighborsOf(cell, cells).reduce((a, c) => a + (c.bomb ? 1 : 0), 0); - } + for (const cell of cells) { + cell.bombCount = neighborsOf(cell, cells).reduce( + (a, c) => a + (c.bomb ? 1 : 0), + 0, + ); + } - for ({} of this) { - yield jsx` + for ({} of this) { + yield jsx` `; - } + } } renderer.render(jsx`<${Minesweeper} />`, document.body); diff --git a/website/package-lock.json b/website/package-lock.json index 6b4a8f38..33f57075 100644 --- a/website/package-lock.json +++ b/website/package-lock.json @@ -98,7 +98,7 @@ }, "../dist": { "name": "@b9g/crank", - "version": "0.5.3", + "version": "0.5.4", "license": "MIT", "workspaces": [ "packages/*" @@ -111,6 +111,7 @@ "eslint": "^8.32.0", "eslint-config-prettier": "^8.6.0", "eslint-plugin-prettier": "^4.2.1", + "eslint-plugin-react": "^7.32.0", "magic-string": "^0.27.0", "playwright-test": "^8.1.2", "prettier": "^2.8.3", @@ -5083,6 +5084,7 @@ "eslint": "^8.32.0", "eslint-config-prettier": "^8.6.0", "eslint-plugin-prettier": "^4.2.1", + "eslint-plugin-react": "^7.32.0", "magic-string": "^0.27.0", "playwright-test": "^8.1.2", "prettier": "^2.8.3",