1+ import { getColor , isInside , setColorEmpty } from "@snk/types/grid" ;
2+ import {
3+ createSnakeFromCells ,
4+ getHeadX ,
5+ getHeadY ,
6+ nextSnake ,
7+ } from "@snk/types/snake" ;
18import init , {
29 get_grid_sample ,
310 IPoint ,
@@ -12,23 +19,62 @@ await init(wasmUrl);
1219init_panic_hook ( ) ;
1320init_log ( ) ;
1421
15- const grid = get_grid_sample ( "cave2" ) ;
16- const { canvas, draw, highlightCell } = createCanvas ( grid ) ;
22+ const igrid = get_grid_sample (
23+ // "caves",
24+ "labyrinth" ,
25+ // "cave",
26+ // "one-dot",
27+ ) ;
28+ const { ctx, canvas, draw } = createCanvas ( igrid ) ;
1729document . body . appendChild ( canvas ) ;
1830
19- const path = solve ( grid , [
31+ const isnake = [
2032 IPoint . create ( 0 , - 1 ) ,
2133 IPoint . create ( 1 , - 1 ) ,
2234 IPoint . create ( 2 , - 1 ) ,
2335 IPoint . create ( 3 , - 1 ) ,
24- ] ) ;
36+ ] ;
37+ const snake0 = createSnakeFromCells ( isnake . map ( ( { x, y } ) => ( { x, y } ) ) ) ;
2538
26- draw (
27- { width : grid . width , height : grid . height , data : grid . cells } ,
28- [ ] as any ,
29- [ ] ,
30- ) ;
39+ const path = solve ( igrid , isnake ) ;
40+
41+ let i = 0 ;
42+ const onChange = ( ) => {
43+ ctx . clearRect ( 0 , 0 , 9999 , 9999 ) ;
44+
45+ const grid = {
46+ width : igrid . width ,
47+ height : igrid . height ,
48+ data : new Uint8Array ( igrid . cells ) ,
49+ } ;
50+
51+ let snake = snake0 ;
52+
53+ for ( let k = 0 ; k < i && k < path . length ; k ++ ) {
54+ snake = nextSnake ( snake , path [ k ] . x , path [ k ] . y ) ;
55+ if ( isInside ( grid , getHeadX ( snake ) , getHeadY ( snake ) ) ) {
56+ setColorEmpty ( grid , getHeadX ( snake ) , getHeadY ( snake ) ) ;
57+ }
58+ }
59+
60+ draw ( grid , snake , [ ] ) ;
61+ } ;
3162
32- console . log ( path ) ;
63+ onChange ( ) ;
3364
34- for ( const { x, y } of path ) highlightCell ( x , y ) ;
65+ const inputI = document . createElement ( "input" ) as any ;
66+ inputI . type = "range" ;
67+ inputI . value = 0 ;
68+ inputI . max = path ? path . length : 0 ;
69+ inputI . step = 1 ;
70+ inputI . min = 0 ;
71+ inputI . style . width = "90%" ;
72+ inputI . style . padding = "20px 0" ;
73+ inputI . addEventListener ( "input" , ( ) => {
74+ i = + inputI . value ;
75+ onChange ( ) ;
76+ } ) ;
77+ document . body . append ( inputI ) ;
78+ document . body . onclick = ( ) => {
79+ inputI . focus ( ) ;
80+ } ;
0 commit comments