-
Notifications
You must be signed in to change notification settings - Fork 58
/
sketch.js
214 lines (190 loc) · 6.02 KB
/
sketch.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
let tiles = [];
const tileImages = [];
let grid = [];
const DIM = 25;
function preload() {
// const path = 'rail';
// for (let i = 0; i < 7; i++) {
// tileImages[i] = loadImage(`${path}/tile${i}.png`);
// }
const path = 'tiles/circuit-coding-train';
for (let i = 0; i < 13; i++) {
tileImages[i] = loadImage(`${path}/${i}.png`);
}
}
function removeDuplicatedTiles(tiles) {
const uniqueTilesMap = {};
for (const tile of tiles) {
const key = tile.edges.join(','); // ex: "ABB,BCB,BBA,AAA"
uniqueTilesMap[key] = tile;
}
return Object.values(uniqueTilesMap);
}
function setup() {
createCanvas(400, 400);
//randomSeed(15);
// tiles[0] = new Tile(tileImages[0], ['AAA', 'AAA', 'AAA', 'AAA']);
// tiles[1] = new Tile(tileImages[1], ['ABA', 'ABA', 'ABA', 'AAA']);
// tiles[2] = new Tile(tileImages[2], ['BAA', 'AAB', 'AAA', 'AAA']);
// tiles[3] = new Tile(tileImages[3], ['BAA', 'AAA', 'AAB', 'AAA']);
// tiles[4] = new Tile(tileImages[4], ['ABA', 'ABA', 'AAA', 'AAA']);
// tiles[5] = new Tile(tileImages[5], ['ABA', 'AAA', 'ABA', 'AAA']);
// tiles[6] = new Tile(tileImages[6], ['ABA', 'ABA', 'ABA', 'ABA']);
// Loaded and created the tiles
tiles[0] = new Tile(tileImages[0], ['AAA', 'AAA', 'AAA', 'AAA']);
tiles[1] = new Tile(tileImages[1], ['BBB', 'BBB', 'BBB', 'BBB']);
tiles[2] = new Tile(tileImages[2], ['BBB', 'BCB', 'BBB', 'BBB']);
tiles[3] = new Tile(tileImages[3], ['BBB', 'BDB', 'BBB', 'BDB']);
tiles[4] = new Tile(tileImages[4], ['ABB', 'BCB', 'BBA', 'AAA']);
tiles[5] = new Tile(tileImages[5], ['ABB', 'BBB', 'BBB', 'BBA']);
tiles[6] = new Tile(tileImages[6], ['BBB', 'BCB', 'BBB', 'BCB']);
tiles[7] = new Tile(tileImages[7], ['BDB', 'BCB', 'BDB', 'BCB']);
tiles[8] = new Tile(tileImages[8], ['BDB', 'BBB', 'BCB', 'BBB']);
tiles[9] = new Tile(tileImages[9], ['BCB', 'BCB', 'BBB', 'BCB']);
tiles[10] = new Tile(tileImages[10], ['BCB', 'BCB', 'BCB', 'BCB']);
tiles[11] = new Tile(tileImages[11], ['BCB', 'BCB', 'BBB', 'BBB']);
tiles[12] = new Tile(tileImages[12], ['BBB', 'BCB', 'BBB', 'BCB']);
for (let i = 0; i < 12; i++) {
tiles[i].index = i;
}
const initialTileCount = tiles.length;
for (let i = 0; i < initialTileCount; i++) {
let tempTiles = [];
for (let j = 0; j < 4; j++) {
tempTiles.push(tiles[i].rotate(j));
}
tempTiles = removeDuplicatedTiles(tempTiles);
tiles = tiles.concat(tempTiles);
}
console.log(tiles.length);
// Generate the adjacency rules based on edges
for (let i = 0; i < tiles.length; i++) {
const tile = tiles[i];
tile.analyze(tiles);
}
startOver();
}
function startOver() {
// Create cell for each spot on the grid
for (let i = 0; i < DIM * DIM; i++) {
grid[i] = new Cell(tiles.length);
}
}
function checkValid(arr, valid) {
//console.log(arr, valid);
for (let i = arr.length - 1; i >= 0; i--) {
// VALID: [BLANK, RIGHT]
// ARR: [BLANK, UP, RIGHT, DOWN, LEFT]
// result in removing UP, DOWN, LEFT
let element = arr[i];
// console.log(element, valid.includes(element));
if (!valid.includes(element)) {
arr.splice(i, 1);
}
}
// console.log(arr);
// console.log("----------");
}
function mousePressed() {
redraw();
}
function draw() {
background(0);
const w = width / DIM;
const h = height / DIM;
for (let j = 0; j < DIM; j++) {
for (let i = 0; i < DIM; i++) {
let cell = grid[i + j * DIM];
if (cell.collapsed) {
let index = cell.options[0];
image(tiles[index].img, i * w, j * h, w, h);
} else {
noFill();
stroke(51);
rect(i * w, j * h, w, h);
}
}
}
// Pick cell with least entropy
let gridCopy = grid.slice();
gridCopy = gridCopy.filter((a) => !a.collapsed);
// console.table(grid);
// console.table(gridCopy);
if (gridCopy.length == 0) {
return;
}
gridCopy.sort((a, b) => {
return a.options.length - b.options.length;
});
let len = gridCopy[0].options.length;
let stopIndex = 0;
for (let i = 1; i < gridCopy.length; i++) {
if (gridCopy[i].options.length > len) {
stopIndex = i;
break;
}
}
if (stopIndex > 0) gridCopy.splice(stopIndex);
const cell = random(gridCopy);
cell.collapsed = true;
const pick = random(cell.options);
if (pick === undefined) {
startOver();
return;
}
cell.options = [pick];
const nextGrid = [];
for (let j = 0; j < DIM; j++) {
for (let i = 0; i < DIM; i++) {
let index = i + j * DIM;
if (grid[index].collapsed) {
nextGrid[index] = grid[index];
} else {
let options = new Array(tiles.length).fill(0).map((x, i) => i);
// Look up
if (j > 0) {
let up = grid[i + (j - 1) * DIM];
let validOptions = [];
for (let option of up.options) {
let valid = tiles[option].down;
validOptions = validOptions.concat(valid);
}
checkValid(options, validOptions);
}
// Look right
if (i < DIM - 1) {
let right = grid[i + 1 + j * DIM];
let validOptions = [];
for (let option of right.options) {
let valid = tiles[option].left;
validOptions = validOptions.concat(valid);
}
checkValid(options, validOptions);
}
// Look down
if (j < DIM - 1) {
let down = grid[i + (j + 1) * DIM];
let validOptions = [];
for (let option of down.options) {
let valid = tiles[option].up;
validOptions = validOptions.concat(valid);
}
checkValid(options, validOptions);
}
// Look left
if (i > 0) {
let left = grid[i - 1 + j * DIM];
let validOptions = [];
for (let option of left.options) {
let valid = tiles[option].right;
validOptions = validOptions.concat(valid);
}
checkValid(options, validOptions);
}
// I could immediately collapse if only one option left?
nextGrid[index] = new Cell(options);
}
}
}
grid = nextGrid;
}