@@ -11,181 +11,181 @@ const DOWN = 3;
11
11
const LEFT = 4 ;
12
12
13
13
const rules = [
14
- [
15
- [ BLANK , UP ] ,
16
- [ BLANK , RIGHT ] ,
17
- [ BLANK , DOWN ] ,
18
- [ BLANK , LEFT ] ,
19
- ] ,
20
- [
21
- [ RIGHT , LEFT , DOWN ] ,
22
- [ LEFT , UP , DOWN ] ,
23
- [ BLANK , DOWN ] ,
24
- [ RIGHT , UP , DOWN ] ,
25
- ] ,
26
- [
27
- [ RIGHT , LEFT , DOWN ] ,
28
- [ LEFT , UP , DOWN ] ,
29
- [ RIGHT , LEFT , UP ] ,
30
- [ BLANK , LEFT ] ,
31
- ] ,
32
- [
33
- [ BLANK , UP ] ,
34
- [ LEFT , UP , DOWN ] ,
35
- [ RIGHT , LEFT , UP ] ,
36
- [ RIGHT , UP , DOWN ] ,
37
- ] ,
38
- [
39
- [ RIGHT , LEFT , DOWN ] ,
40
- [ BLANK , RIGHT ] ,
41
- [ RIGHT , LEFT , UP ] ,
42
- [ UP , DOWN , RIGHT ] ,
43
- ] ,
14
+ [
15
+ [ BLANK , UP ] ,
16
+ [ BLANK , RIGHT ] ,
17
+ [ BLANK , DOWN ] ,
18
+ [ BLANK , LEFT ] ,
19
+ ] ,
20
+ [
21
+ [ RIGHT , LEFT , DOWN ] ,
22
+ [ LEFT , UP , DOWN ] ,
23
+ [ BLANK , DOWN ] ,
24
+ [ RIGHT , UP , DOWN ] ,
25
+ ] ,
26
+ [
27
+ [ RIGHT , LEFT , DOWN ] ,
28
+ [ LEFT , UP , DOWN ] ,
29
+ [ RIGHT , LEFT , UP ] ,
30
+ [ BLANK , LEFT ] ,
31
+ ] ,
32
+ [
33
+ [ BLANK , UP ] ,
34
+ [ LEFT , UP , DOWN ] ,
35
+ [ RIGHT , LEFT , UP ] ,
36
+ [ RIGHT , UP , DOWN ] ,
37
+ ] ,
38
+ [
39
+ [ RIGHT , LEFT , DOWN ] ,
40
+ [ BLANK , RIGHT ] ,
41
+ [ RIGHT , LEFT , UP ] ,
42
+ [ UP , DOWN , RIGHT ] ,
43
+ ] ,
44
44
] ;
45
45
46
46
function preload ( ) {
47
- tiles [ 0 ] = loadImage ( " tiles/blank.png" ) ;
48
- tiles [ 1 ] = loadImage ( " tiles/up.png" ) ;
49
- tiles [ 2 ] = loadImage ( " tiles/right.png" ) ;
50
- tiles [ 3 ] = loadImage ( " tiles/down.png" ) ;
51
- tiles [ 4 ] = loadImage ( " tiles/left.png" ) ;
47
+ tiles [ 0 ] = loadImage ( ' tiles/blank.png' ) ;
48
+ tiles [ 1 ] = loadImage ( ' tiles/up.png' ) ;
49
+ tiles [ 2 ] = loadImage ( ' tiles/right.png' ) ;
50
+ tiles [ 3 ] = loadImage ( ' tiles/down.png' ) ;
51
+ tiles [ 4 ] = loadImage ( ' tiles/left.png' ) ;
52
52
}
53
53
54
54
function setup ( ) {
55
- createCanvas ( 800 , 800 ) ;
56
- randomSeed ( " SHIFFMAN" ) ;
57
- for ( let i = 0 ; i < DIM * DIM ; i ++ ) {
58
- grid [ i ] = {
59
- collapsed : false ,
60
- options : [ BLANK , UP , RIGHT , DOWN , LEFT ] ,
61
- } ;
62
- }
55
+ createCanvas ( 800 , 800 ) ;
56
+ randomSeed ( ' SHIFFMAN' ) ;
57
+ for ( let i = 0 ; i < DIM * DIM ; i ++ ) {
58
+ grid [ i ] = {
59
+ collapsed : false ,
60
+ options : [ BLANK , UP , RIGHT , DOWN , LEFT ] ,
61
+ } ;
62
+ }
63
63
}
64
64
65
65
function checkValid ( arr , valid ) {
66
- //console.log(arr, valid);
67
- for ( let i = arr . length - 1 ; i >= 0 ; i -- ) {
68
- // VALID: [BLANK, RIGHT]
69
- // ARR: [BLANK, UP, RIGHT, DOWN, LEFT]
70
- // result in removing UP, DOWN, LEFT
71
- let element = arr [ i ] ;
72
- // console.log(element, valid.includes(element));
73
- if ( ! valid . includes ( element ) ) {
74
- arr . splice ( i , 1 ) ;
75
- }
76
- }
77
- // console.log(arr);
78
- // console.log("----------");
66
+ //console.log(arr, valid);
67
+ for ( let i = arr . length - 1 ; i >= 0 ; i -- ) {
68
+ // VALID: [BLANK, RIGHT]
69
+ // ARR: [BLANK, UP, RIGHT, DOWN, LEFT]
70
+ // result in removing UP, DOWN, LEFT
71
+ let element = arr [ i ] ;
72
+ // console.log(element, valid.includes(element));
73
+ if ( ! valid . includes ( element ) ) {
74
+ arr . splice ( i , 1 ) ;
75
+ }
76
+ }
77
+ // console.log(arr);
78
+ // console.log("----------");
79
79
}
80
80
81
81
function mousePressed ( ) {
82
- redraw ( ) ;
82
+ redraw ( ) ;
83
83
}
84
84
85
85
function draw ( ) {
86
- background ( 0 ) ;
87
-
88
- const w = width / DIM ;
89
- const h = height / DIM ;
90
- for ( let j = 0 ; j < DIM ; j ++ ) {
91
- for ( let i = 0 ; i < DIM ; i ++ ) {
92
- let cell = grid [ i + j * DIM ] ;
93
- if ( cell . collapsed ) {
94
- let index = cell . options [ 0 ] ;
95
- image ( tiles [ index ] , i * w , j * h , w , h ) ;
96
- } else {
97
- fill ( 0 ) ;
98
- stroke ( 255 ) ;
99
- rect ( i * w , j * h , w , h ) ;
100
- }
101
- }
102
- }
103
-
104
- // Pick cell with least entropy
105
- let gridCopy = grid . slice ( ) ;
106
- gridCopy = gridCopy . filter ( ( a ) => ! a . collapsed ) ;
107
- // console.table(grid);
108
- // console.table(gridCopy);
109
-
110
- if ( gridCopy . length == 0 ) {
111
- return ;
112
- }
113
- gridCopy . sort ( ( a , b ) => {
114
- return a . options . length - b . options . length ;
115
- } ) ;
116
-
117
- let len = gridCopy [ 0 ] . options . length ;
118
- let stopIndex = 0 ;
119
- for ( let i = 1 ; i < gridCopy . length ; i ++ ) {
120
- if ( gridCopy [ i ] . options . length > len ) {
121
- stopIndex = i ;
122
- break ;
123
- }
124
- }
125
-
126
- if ( stopIndex > 0 ) gridCopy . splice ( stopIndex ) ;
127
- const cell = random ( gridCopy ) ;
128
- cell . collapsed = true ;
129
- const pick = random ( cell . options ) ;
130
- cell . options = [ pick ] ;
131
-
132
- const nextGrid = [ ] ;
133
- for ( let j = 0 ; j < DIM ; j ++ ) {
134
- for ( let i = 0 ; i < DIM ; i ++ ) {
135
- let index = i + j * DIM ;
136
- if ( grid [ index ] . collapsed ) {
137
- nextGrid [ index ] = grid [ index ] ;
138
- } else {
139
- let options = [ BLANK , UP , RIGHT , DOWN , LEFT ] ;
140
- // Look up
141
- if ( j > 0 ) {
142
- let up = grid [ i + ( j - 1 ) * DIM ] ;
143
- let validOptions = [ ] ;
144
- for ( let option of up . options ) {
145
- let valid = rules [ option ] [ 2 ] ;
146
- validOptions = validOptions . concat ( valid ) ;
147
- }
148
- checkValid ( options , validOptions ) ;
149
- }
150
- // Look right
151
- if ( i < DIM - 1 ) {
152
- //console.log(i, j);
153
- let right = grid [ i + 1 + j * DIM ] ;
154
- let validOptions = [ ] ;
155
- for ( let option of right . options ) {
156
- let valid = rules [ option ] [ 3 ] ;
157
- validOptions = validOptions . concat ( valid ) ;
158
- }
159
- checkValid ( options , validOptions ) ;
160
- }
161
- // Look down
162
- if ( j < DIM - 1 ) {
163
- let down = grid [ i + ( j + 1 ) * DIM ] ;
164
- let validOptions = [ ] ;
165
- for ( let option of down . options ) {
166
- let valid = rules [ option ] [ 0 ] ;
167
- validOptions = validOptions . concat ( valid ) ;
168
- }
169
- checkValid ( options , validOptions ) ;
170
- }
171
- // Look left
172
- if ( i > 0 ) {
173
- let left = grid [ i - 1 + j * DIM ] ;
174
- let validOptions = [ ] ;
175
- for ( let option of left . options ) {
176
- let valid = rules [ option ] [ 1 ] ;
177
- validOptions = validOptions . concat ( valid ) ;
178
- }
179
- checkValid ( options , validOptions ) ;
180
- }
181
-
182
- nextGrid [ index ] = {
183
- options,
184
- collapsed : false ,
185
- } ;
186
- }
187
- }
188
- }
189
-
190
- grid = nextGrid ;
86
+ background ( 0 ) ;
87
+
88
+ const w = width / DIM ;
89
+ const h = height / DIM ;
90
+ for ( let j = 0 ; j < DIM ; j ++ ) {
91
+ for ( let i = 0 ; i < DIM ; i ++ ) {
92
+ let cell = grid [ i + j * DIM ] ;
93
+ if ( cell . collapsed ) {
94
+ let index = cell . options [ 0 ] ;
95
+ image ( tiles [ index ] , i * w , j * h , w , h ) ;
96
+ } else {
97
+ fill ( 0 ) ;
98
+ stroke ( 255 ) ;
99
+ rect ( i * w , j * h , w , h ) ;
100
+ }
101
+ }
102
+ }
103
+
104
+ // Pick cell with least entropy
105
+ let gridCopy = grid . slice ( ) ;
106
+ gridCopy = gridCopy . filter ( ( a ) => ! a . collapsed ) ;
107
+ // console.table(grid);
108
+ // console.table(gridCopy);
109
+
110
+ if ( gridCopy . length == 0 ) {
111
+ return ;
112
+ }
113
+ gridCopy . sort ( ( a , b ) => {
114
+ return a . options . length - b . options . length ;
115
+ } ) ;
116
+
117
+ let len = gridCopy [ 0 ] . options . length ;
118
+ let stopIndex = 0 ;
119
+ for ( let i = 1 ; i < gridCopy . length ; i ++ ) {
120
+ if ( gridCopy [ i ] . options . length > len ) {
121
+ stopIndex = i ;
122
+ break ;
123
+ }
124
+ }
125
+
126
+ if ( stopIndex > 0 ) gridCopy . splice ( stopIndex ) ;
127
+ const cell = random ( gridCopy ) ;
128
+ cell . collapsed = true ;
129
+ const pick = random ( cell . options ) ;
130
+ cell . options = [ pick ] ;
131
+
132
+ const nextGrid = [ ] ;
133
+ for ( let j = 0 ; j < DIM ; j ++ ) {
134
+ for ( let i = 0 ; i < DIM ; i ++ ) {
135
+ let index = i + j * DIM ;
136
+ if ( grid [ index ] . collapsed ) {
137
+ nextGrid [ index ] = grid [ index ] ;
138
+ } else {
139
+ let options = [ BLANK , UP , RIGHT , DOWN , LEFT ] ;
140
+ // Look up
141
+ if ( j > 0 ) {
142
+ let up = grid [ i + ( j - 1 ) * DIM ] ;
143
+ let validOptions = [ ] ;
144
+ for ( let option of up . options ) {
145
+ let valid = rules [ option ] [ 2 ] ;
146
+ validOptions = validOptions . concat ( valid ) ;
147
+ }
148
+ checkValid ( options , validOptions ) ;
149
+ }
150
+ // Look right
151
+ if ( i < DIM - 1 ) {
152
+ //console.log(i, j);
153
+ let right = grid [ i + 1 + j * DIM ] ;
154
+ let validOptions = [ ] ;
155
+ for ( let option of right . options ) {
156
+ let valid = rules [ option ] [ 3 ] ;
157
+ validOptions = validOptions . concat ( valid ) ;
158
+ }
159
+ checkValid ( options , validOptions ) ;
160
+ }
161
+ // Look down
162
+ if ( j < DIM - 1 ) {
163
+ let down = grid [ i + ( j + 1 ) * DIM ] ;
164
+ let validOptions = [ ] ;
165
+ for ( let option of down . options ) {
166
+ let valid = rules [ option ] [ 0 ] ;
167
+ validOptions = validOptions . concat ( valid ) ;
168
+ }
169
+ checkValid ( options , validOptions ) ;
170
+ }
171
+ // Look left
172
+ if ( i > 0 ) {
173
+ let left = grid [ i - 1 + j * DIM ] ;
174
+ let validOptions = [ ] ;
175
+ for ( let option of left . options ) {
176
+ let valid = rules [ option ] [ 1 ] ;
177
+ validOptions = validOptions . concat ( valid ) ;
178
+ }
179
+ checkValid ( options , validOptions ) ;
180
+ }
181
+
182
+ nextGrid [ index ] = {
183
+ options,
184
+ collapsed : false ,
185
+ } ;
186
+ }
187
+ }
188
+ }
189
+
190
+ grid = nextGrid ;
191
191
}
0 commit comments