blob: 9aab9039d34ac666a718607535aac6254620c0c5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
<script lang="ts">
import { validate } from "./lib/Sudoku";
let selTile: HTMLDivElement | EventTarget;
const getSelTile = () => selTile as HTMLDivElement;
const grid = (new Array(10)).fill((new Array(10)).fill(".")) as string[][];
function keyDownHandler(e: KeyboardEvent) {
if(e.key === "Backspace") {
setSelTile(".")
}
const num = parseInt(e.key);
if(Number.isNaN(num) || num === 0) return;
setSelTile(num)
validate(grid)
}
function showTile(val: string) {
return val === "." ? "" : val;
}
function setSelTile(val: string | number) {
grid[parseInt(getSelTile().dataset.i)][parseInt(getSelTile().dataset.j)] = val.toString();
getSelTile().innerText = val === "." ? "" : val.toString();
}
</script>
<!-- <svelte:body on:keydown|preventDefaultstopPropagation={keyDownHandler} /> -->
<svelte:body on:keydown={keyDownHandler} />
<main>
<div class="grid">
{#each {length: 9} as _, i}
{#each {length: 9} as _, j}
<div
class="tile"
class:br={j % 3 === 2 && j !== 8}
class:bb={i % 3 === 2 && i !== 8}
on:click|self={e => selTile = e.target}
data-i={i}
data-j={j}
on:keypress={null}>{grid[i][j] === "." ? "" : grid[i][j]}</div>
{/each}
{/each}
</div>
</main>
<style>
.grid {
display: grid;
grid-template-columns: repeat(9, 1fr);
}
.tile {
width: 4rem;
aspect-ratio: 1;
display: flex;
justify-content: center;
align-items: center;
font-size: 2.5rem;
gap: 0;
box-sizing: border-box;
user-select: none;
}
.tile:nth-child(odd) {
background-color: lightgrey;
}
.tile:nth-child(even) {
background-color: wheat;
}
.bb {
border-bottom: 3px solid black;
}
.br {
border-right: 3px solid black;
}
</style>
|