-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniqueDistancingScript.js
More file actions
182 lines (168 loc) · 4.46 KB
/
Copy pathuniqueDistancingScript.js
File metadata and controls
182 lines (168 loc) · 4.46 KB
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
var grid = 64;
var margin = Math.floor(grid / 16);
var size = 6;
//set up the canvas
var stage = document.getElementById("gameCanvas");
stage.width = (grid + margin) * size + margin;
stage.height = (grid + margin) * size + margin;
var ctx = stage.getContext("2d");
var black = 'rgb(0 0 0)';
var grey = 'rgb(128 128 128)';
var white = 'rgb(255 255 255)';
var red = 'rgb(255 0 0)';
var green = 'rgb(0 255 0)';
var blue = 'rgb(0 0 255)';
var yellow = 'rgb(255 255 0)';
var lblue = 'rgb(0 255 255)';
var magenta = 'rgb(255 0 255)';
var clickX = -1;
var clickY = -1;
var counters = 0;
var tiles = [];
var cut = false;
var won = false;
var solution = [];
var solutions = [];
var duplicate = false;
function distance(p1, p2) {
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
function illegal() {
for (let tile of tiles) {
if (tile.color == red) {
tile.color = white;
}
}
counters = 0;
for (let tile of tiles) {
if (tile.color == green) {
counters++;
}
}
if (counters > 1) {
for (let tile0 of tiles) {
if (tile0.color != green) {
for (let tile1 of tiles) {
if (tile1.color == green) {
for (let tile2 of tiles) {
if (tile2.color == green) {
if (tile1 != tile2 && distance(tile0, tile1) == distance(tile0, tile2)) {
tile0.color = red;
}
for (let tile3 of tiles) {
if (tile3.color == green && distance(tile2, tile3) == distance(tile1, tile0)) {
tile0.color = red;
}
}
}
}
}
}
}
}
}
}
function solve() {
let layers = 1;
for (let tile of tiles) {
if (tile.color == white) {
tile.color = green;
illegal();
requestAnimationFrame(render);
//alert();
layers++;
solve();
if (counters == size) {
/*solution = [];
for (let tile of tiles) {
if (tile.color == green) {
solution[solution.length] = [tile.x, tile.y];
}
}
if (solutions.length > 0) {
duplicate = true;
for (i = 0; i < solutions.length; i++) {
for (j = 0; j < solutions[i].length; j++) {
if (solution[j][0] != solutions[i][j][0] || solution[j][1] != solutions[i][j][1]) {
duplicate = false;
}
}
}
if (duplicate == false) {
solutions[solutions.length] = solution;
solution = [];
}
} else if (solutions.length == 0) {
solutions[solutions.length] = solution;
solution = [];
}
if (solutions.length >= 2) {
alert(solutions);
return;
}*/
return;
}
tile.color = white;
illegal();
requestAnimationFrame(render);
}
}
}
function setup() {
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
tiles[tiles.length] = {x: i, y: j, color: white};
}
}
}
function update() {
//Math.floor((clickX - margin)/(grid + margin)) is the scaled x-coord of the mouse
illegal();
for (let tile of tiles) {
if (tile.color == green && tile.x == Math.floor((clickX - margin)/(grid + margin)) && tile.y == Math.floor((clickY - margin)/(grid + margin))) {
tile.color = white;
clickX = -1;
clickY = -1;
}
}
for (let tile of tiles) {
if (tile.color == white && tile.x == Math.floor((clickX - margin)/(grid + margin)) && tile.y == Math.floor((clickY - margin)/(grid + margin))) {
tile.color = green;
clickX = -1;
clickY = -1;
}
}
}
function render() {
ctx.fillStyle = black;
ctx.fillRect(0, 0, stage.width, stage.height);
for (let tile of tiles) {
ctx.fillStyle = tile.color;
ctx.fillRect(tile.x * (grid + margin) + margin, tile.y * (grid + margin) + margin, grid, grid);
}
}
function main() {
update();
render();
requestAnimationFrame(main);
counters = 0;
for (let tile of tiles) {
if (tile.color == green) {
counters++;
}
}
if (counters == size && won == false) {
won = true;
alert('YOU WON!');
} else if (counters != size) {
won = false;
}
}
var canvasClick = function (event) {
clickX = event.clientX - stage.offsetLeft + document.body.scrollLeft;
clickY = event.clientY - stage.offsetTop + document.body.scrollTop;
}
addEventListener("click", canvasClick, false);
setup();
//solve();
main();