From 3bdb9759d30793f98a06932338bf2f94d55fdc35 Mon Sep 17 00:00:00 2001 From: Arnaud Scheffler Date: Fri, 14 Mar 2025 17:30:29 +0100 Subject: [PATCH] feat: add annotation mode --- src/lib/Board.svelte | 31 ++++- src/lib/Cell.svelte | 35 ++++- src/lib/Switch.svelte | 263 +++++++++++++++++++++++++++++++++++ src/lib/ValueSelector.svelte | 4 +- src/lib/core/board.ts | 6 +- src/routes/+page.svelte | 4 +- 6 files changed, 327 insertions(+), 16 deletions(-) create mode 100644 src/lib/Switch.svelte diff --git a/src/lib/Board.svelte b/src/lib/Board.svelte index f64ab48..a9e8fe9 100644 --- a/src/lib/Board.svelte +++ b/src/lib/Board.svelte @@ -6,10 +6,12 @@ import { onMount } from 'svelte'; import GameCommands from './GameCommands.svelte'; import SumIndicator from './SumIndicator.svelte'; + import Switch from './Switch.svelte'; let selectedI: number | undefined = undefined; let selectedJ: number | undefined = undefined; let status: 'incomplete' | 'error' | 'won' = 'incomplete'; + let valueMode: 'Write' | 'Annotation' = 'Write'; const onSelect = (i: number, j: number) => () => { if (i === selectedI && j === selectedJ) { @@ -23,14 +25,24 @@ const onSetValue = (number: number) => { if (selectedI !== undefined && selectedJ !== undefined) { - board.grid[selectedI][selectedJ] = number; - if (board.isComplete()) { - status = board.isValid() ? 'won' : 'error'; - if (status === 'won') { - jsConfetti.addConfetti(); - selectedI = undefined; - selectedJ = undefined; + if (valueMode === 'Write') { + board.grid[selectedI][selectedJ] = number; + if (board.isComplete()) { + status = board.isValid() ? 'won' : 'error'; + if (status === 'won') { + jsConfetti.addConfetti(); + selectedI = undefined; + selectedJ = undefined; + } } + } else { + if (number) { + const cell = board.annotations[selectedI][selectedJ]; + if (!cell.includes(number)) board.annotations[selectedI][selectedJ] = [...cell, number]; + } else { + board.annotations[selectedI][selectedJ] = []; + } + console.log(board); } } }; @@ -38,6 +50,7 @@ const onGameCommand = (mode: string) => { if (mode === 'reset') { board.reset(); + board.grid = board.grid; // to trigger reload } else { board = new Board(); if (mode === 'easy') board.prepare(Difficulty.Easy); @@ -64,6 +77,7 @@ {#each row as cell, j} +
+ +
diff --git a/src/lib/Cell.svelte b/src/lib/Cell.svelte index 4903467..61a73bf 100644 --- a/src/lib/Cell.svelte +++ b/src/lib/Cell.svelte @@ -1,14 +1,41 @@ diff --git a/src/lib/Switch.svelte b/src/lib/Switch.svelte new file mode 100644 index 0000000..c5da96d --- /dev/null +++ b/src/lib/Switch.svelte @@ -0,0 +1,263 @@ + + +{#if design == 'inner'} +
+ {label} + +
+{:else if design == 'slider'} +
+ {label} + +
+{:else} +
+
+
{label}
+ {#each options as option} + + + {/each} +
+
+{/if} + + diff --git a/src/lib/ValueSelector.svelte b/src/lib/ValueSelector.svelte index 6584f21..ac10999 100644 --- a/src/lib/ValueSelector.svelte +++ b/src/lib/ValueSelector.svelte @@ -4,7 +4,7 @@ const numbers = Array.from({ length: 9 }, (_, i) => i + 1); -
+
{#each numbers as number (number)}
diff --git a/src/lib/core/board.ts b/src/lib/core/board.ts index 0a2261c..710f66a 100644 --- a/src/lib/core/board.ts +++ b/src/lib/core/board.ts @@ -17,7 +17,6 @@ function shuffleArray(array: Array): void { function getNumberToKeepFromDifficulty(difficulty: Difficulty) { const rules = { - // 0: 8, 0: 3, 1: 2, 2: 1, @@ -29,6 +28,7 @@ function getNumberToKeepFromDifficulty(difficulty: Difficulty) { export class Board { grid: Case[][]; + annotations: number[][][]; verticalSum: number[]; horizontalSum: number[]; lockedCells: boolean[][]; @@ -38,6 +38,7 @@ export class Board { shuffleArray(numbers); this.grid = []; + this.annotations = []; this.verticalSum = new Array(3).fill(0); this.horizontalSum = []; this.lockedCells = []; @@ -49,6 +50,7 @@ export class Board { this.verticalSum[j] += row[j]; } this.grid.push(row); + this.annotations.push([[], [], []]); this.horizontalSum[i] = row.reduce((partialSum, a) => partialSum + a, 0); this.lockedCells[i] = [false, false, false]; @@ -100,10 +102,10 @@ export class Board { } reset() { - console.log('ici'); for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { if (this.lockedCells[i][j]) this.grid[i][j] = undefined; + this.annotations[i][j] = []; } } } diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 5bf5167..1fbd321 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -4,4 +4,6 @@

Fubuki

- +
+ +