@@ -4,48 +4,60 @@ import "./menu";
44import { grid , snake } from "./sample" ;
55
66( async ( ) => {
7- const api = await import ( "@snk/solver-r" ) ;
8-
9- const iColorGrid = api . IColorGrid . create ( grid . width , grid . height , grid . data ) ;
10- const iSnake = snakeToCells ( snake ) . map ( ( p ) => api . IPoint . create ( p . x , p . y ) ) ;
11-
12- // const colorGrid = api.get_color_grid_sample(api.SampleGrid.Labyrinthe);
13-
14- const { canvas, draw, highlightCell } = createCanvas ( iColorGrid ) ;
15- document . body . appendChild ( canvas ) ;
16- draw ( iColorGrid , snake , [ ] ) ;
17-
18- api . greet ( ) ;
19-
20- const a = performance . now ( ) ;
21- const path = api . solve ( iColorGrid , iSnake ) . reverse ( ) ;
22- console . log ( performance . now ( ) - a ) ;
23-
24- {
25- const snakeLength = snake . length / 2 ;
26-
27- const onChange = ( ) => {
28- const i = + input . value ;
29- const s = createSnakeFromCells ( path . slice ( i , i + snakeLength ) . reverse ( ) ) ;
30-
31- draw ( iColorGrid , s , [ ] ) ;
32-
33- for ( let j = i + snakeLength ; j -- ; ) {
34- highlightCell ( path [ j ] . x , path [ j ] . y , "#123bde" ) ;
35- }
36- } ;
37-
38- const input = document . createElement ( "input" ) as any ;
39- input . type = "range" ;
40- input . value = 0 ;
41- input . step = 1 ;
42- input . min = 0 ;
43- input . max = path . length - snakeLength ;
44- input . style . width = "90%" ;
45- input . style . padding = "20px 0" ;
46- input . addEventListener ( "input" , onChange ) ;
47- document . body . append ( input ) ;
48-
49- onChange ( ) ;
50- }
7+ const { IColorGrid, IPoint, greet, solve} = await import ( "@snk/solver-r" ) ;
8+ greet ( ) ;
9+
10+ const iColorGrid = IColorGrid . create ( grid . width , grid . height , grid . data ) ;
11+ const iSnake = snakeToCells ( snake ) . map ( ( p ) => IPoint . create ( p . x , p . y ) ) ;
12+
13+
14+ const { canvas, draw, highlightCell } = createCanvas ( iColorGrid ) ;
15+ document . body . appendChild ( canvas ) ;
16+ draw ( iColorGrid , snake , [ ] ) ;
17+
18+
19+ const a = performance . now ( ) ;
20+ const path = solve ( iColorGrid , iSnake ) . reverse ( ) ;
21+ console . log ( performance . now ( ) - a ) ;
22+
23+ {
24+ const snakeLength = snake . length / 2 ;
25+
26+ const onChange = ( ) => {
27+ const i = + input . value ;
28+ const s = createSnakeFromCells ( path . slice ( i , i + snakeLength ) . reverse ( ) ) ;
29+
30+ const g = {
31+ width : iColorGrid . width ,
32+ height : iColorGrid . height ,
33+ data : new Uint8Array ( [ ...iColorGrid . data as any ] ) ,
34+ } ;
35+ for ( let j = i ; j -- ; ) {
36+ const { x, y } = path [ j ] ;
37+ if ( x >= 0 && y >= 0 && x < iColorGrid . width && y < iColorGrid . height ) {
38+ const index = x * g . height + y ;
39+ g . data [ index ] = 0 ;
40+ }
41+ }
42+
43+ draw ( g , s , [ ] ) ;
44+
45+ for ( let j = i + snakeLength ; j -- ; ) {
46+ highlightCell ( path [ j ] . x , path [ j ] . y , "#123bde" ) ;
47+ }
48+ } ;
49+
50+ const input = document . createElement ( "input" ) as any ;
51+ input . type = "range" ;
52+ input . value = 0 ;
53+ input . step = 1 ;
54+ input . min = 0 ;
55+ input . max = path . length - snakeLength ;
56+ input . style . width = "90%" ;
57+ input . style . padding = "20px 0" ;
58+ input . addEventListener ( "input" , onChange ) ;
59+ document . body . append ( input ) ;
60+
61+ onChange ( ) ;
62+ }
5163} ) ( ) ;
0 commit comments