1
- import { jsx } from "@b9g/crank/standalone" ;
1
+ emport { jsx } from "@b9g/crank/standalone" ;
2
2
import { renderer } from "@b9g/crank/dom" ;
3
3
4
4
function Hexagon ( { cx = 0 , cy = 0 , r, ...props } ) {
5
- if ( ! r ) {
6
- return null ;
7
- }
5
+ if ( ! r ) {
6
+ return null ;
7
+ }
8
8
9
- const points = [ ] ;
10
- for ( let i = 0 , a = 0 ; i < 6 ; i ++ , a += Math . PI / 3 ) {
11
- points . push ( [ cx + Math . cos ( a ) * r , cy + Math . sin ( a ) * r ] ) ;
12
- }
9
+ const points = [ ] ;
10
+ for ( let i = 0 , a = 0 ; i < 6 ; i ++ , a += Math . PI / 3 ) {
11
+ points . push ( [ cx + Math . cos ( a ) * r , cy + Math . sin ( a ) * r ] ) ;
12
+ }
13
13
14
- return jsx `
14
+ return jsx `
15
15
<path
16
16
...${ props }
17
17
d="M ${ points . map ( ( [ x , y ] , i ) => `${ i > 0 ? "L" : "" } ${ x } ${ y } ` ) } Z"
@@ -20,26 +20,26 @@ function Hexagon({cx = 0, cy = 0, r, ...props}) {
20
20
}
21
21
22
22
function centerCoordsFor ( cell , size ) {
23
- const colSpacing = ( size * 3 ) / 2 ;
24
- const rowSpacing = Math . sqrt ( 3 ) * size ;
25
- return {
26
- cx : cell . col * colSpacing ,
27
- cy : cell . row * rowSpacing + ( cell . col % 2 === 0 ? 0 : rowSpacing / 2 ) ,
28
- } ;
23
+ const colSpacing = ( size * 3 ) / 2 ;
24
+ const rowSpacing = Math . sqrt ( 3 ) * size ;
25
+ return {
26
+ cx : cell . col * colSpacing ,
27
+ cy : cell . row * rowSpacing + ( cell . col % 2 === 0 ? 0 : rowSpacing / 2 ) ,
28
+ } ;
29
29
}
30
30
31
31
function axialCoordsFor ( cell ) {
32
- return { q : cell . col , r : cell . row - Math . floor ( cell . col / 2 ) } ;
32
+ return { q : cell . col , r : cell . row - Math . floor ( cell . col / 2 ) } ;
33
33
}
34
34
35
35
function HexagonalGrid ( { radius = 20 , cells /*, testCell*/ } ) {
36
- return cells . map ( ( cell /*, i*/ ) => {
37
- const onclick = ( ) => {
38
- //console.log(neighborsOf(cell, cells));
39
- } ;
40
- const { cx, cy} = centerCoordsFor ( cell , radius ) ;
41
- const { q, r} = axialCoordsFor ( cell ) ;
42
- return jsx `
36
+ return cells . map ( ( cell /*, i*/ ) => {
37
+ const onclick = ( ) => {
38
+ //console.log(neighborsOf(cell, cells));
39
+ } ;
40
+ const { cx, cy} = centerCoordsFor ( cell , radius ) ;
41
+ const { q, r} = axialCoordsFor ( cell ) ;
42
+ return jsx `
43
43
<${ Hexagon }
44
44
r=${ radius }
45
45
cx=${ cx } cy=${ cy }
@@ -67,58 +67,58 @@ function HexagonalGrid({radius = 20, cells /*, testCell*/}) {
67
67
>${ q } ,${ r } </text>
68
68
-->
69
69
` ;
70
- } ) ;
70
+ } ) ;
71
71
}
72
72
73
73
function shuffle ( arr ) {
74
- for ( let i = arr . length - 1 ; i > 0 ; i -- ) {
75
- const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
76
- [ arr [ i ] , arr [ j ] ] = [ arr [ j ] , arr [ i ] ] ;
77
- }
74
+ for ( let i = arr . length - 1 ; i > 0 ; i -- ) {
75
+ const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
76
+ [ arr [ i ] , arr [ j ] ] = [ arr [ j ] , arr [ i ] ] ;
77
+ }
78
78
79
- return arr ;
79
+ return arr ;
80
80
}
81
81
82
82
function neighborsOf ( cell , cells ) {
83
- const { q, r} = axialCoordsFor ( cell ) ;
84
- const vectors = [
85
- [ 1 , 0 ] ,
86
- [ 0 , 1 ] ,
87
- [ - 1 , 1 ] ,
88
- [ - 1 , 0 ] ,
89
- [ 0 , - 1 ] ,
90
- [ 1 , - 1 ] ,
91
- ] ;
92
- const axialSet = new Set ( vectors . map ( ( [ q1 , r1 ] ) => `${ q + q1 } ,${ r + r1 } ` ) ) ;
93
- return cells . filter ( ( cell1 ) => {
94
- const { q, r} = axialCoordsFor ( cell1 ) ;
95
- return axialSet . has ( `${ q } ,${ r } ` ) ;
96
- } ) ;
83
+ const { q, r} = axialCoordsFor ( cell ) ;
84
+ const vectors = [
85
+ [ 1 , 0 ] ,
86
+ [ 0 , 1 ] ,
87
+ [ - 1 , 1 ] ,
88
+ [ - 1 , 0 ] ,
89
+ [ 0 , - 1 ] ,
90
+ [ 1 , - 1 ] ,
91
+ ] ;
92
+ const axialSet = new Set ( vectors . map ( ( [ q1 , r1 ] ) => `${ q + q1 } ,${ r + r1 } ` ) ) ;
93
+ return cells . filter ( ( cell1 ) => {
94
+ const { q, r} = axialCoordsFor ( cell1 ) ;
95
+ return axialSet . has ( `${ q } ,${ r } ` ) ;
96
+ } ) ;
97
97
}
98
98
99
99
function * Minesweeper ( ) {
100
- const rows = 12 ,
101
- cols = 15 ;
102
- const cells = [ ] ;
103
- for ( let row = 0 ; row < rows ; row ++ ) {
104
- for ( let col = 0 ; col < cols ; col ++ ) {
105
- cells . push ( { row, col, bomb : false , bombCount : 0 } ) ;
106
- }
107
- }
100
+ const rows = 12 ,
101
+ cols = 15 ;
102
+ const cells = [ ] ;
103
+ for ( let row = 0 ; row < rows ; row ++ ) {
104
+ for ( let col = 0 ; col < cols ; col ++ ) {
105
+ cells . push ( { row, col, bomb : false , bombCount : 0 } ) ;
106
+ }
107
+ }
108
108
109
- for ( const cell of shuffle ( cells . slice ( ) ) . slice ( 0 , 30 ) ) {
110
- cell . bomb = true ;
111
- }
109
+ for ( const cell of shuffle ( cells . slice ( ) ) . slice ( 0 , 30 ) ) {
110
+ cell . bomb = true ;
111
+ }
112
112
113
- for ( const cell of cells ) {
114
- cell . bombCount = neighborsOf ( cell , cells ) . reduce (
115
- ( a , c ) => a + ( c . bomb ? 1 : 0 ) ,
116
- 0 ,
117
- ) ;
118
- }
113
+ for ( const cell of cells ) {
114
+ cell . bombCount = neighborsOf ( cell , cells ) . reduce (
115
+ ( a , c ) => a + ( c . bomb ? 1 : 0 ) ,
116
+ 0 ,
117
+ ) ;
118
+ }
119
119
120
- for ( { } of this ) {
121
- yield jsx `
120
+ for ( { } of this ) {
121
+ yield jsx `
122
122
<svg
123
123
width="500px"
124
124
height="500px"
@@ -131,7 +131,7 @@ function* Minesweeper() {
131
131
<${ HexagonalGrid } cells=${ cells } radius=${ 15 } />
132
132
</svg>
133
133
` ;
134
- }
134
+ }
135
135
}
136
136
137
137
renderer . render ( jsx `<${ Minesweeper } />` , document . body ) ;
0 commit comments