forked from borgbean/euler-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile.html
259 lines (236 loc) · 7.49 KB
/
tile.html
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE HTML>
<html>
<head>
<title>Test area</title>
<link rel="stylesheet" href="stylesheet.css">
<link rel="stylesheet" href="tilesheet.css">
<style id="tile_styles"></style>
</head>
<body>
<script src="jquery-2.1.1.min.js"></script>
<script src="jquery-ui-1.10.4.min.js"></script>
<script>
//assuming 500x500 for now
var tiles;
var empty;
var rows, cols;
var width, height;
var image;
function log(message) {
console.log(message);
}
//TODO check if tile we're mousing over is adjacent to empty, choose highlighting depending
//animate highlighting when mousing over
function gameEnd() {
var tiles = $("#tiles");
tiles.empty();
tiles.css("background", image);
tiles.css("background-size", "500px 500px");
}
function isAdjacent(tile1, tile2) {
var diff = tile2 - tile1;
if(Math.abs(diff) == 1) { //check if it's next to the tile
var col = tile1 % cols;
if(col < (cols-1) && diff == 1) {
return true;
}
if(col > 0 && diff == -1) {
return true;
}
}
else if(Math.abs(diff) == cols) { //check if it's above or below
var row = Math.floor(tile1 / cols);
if(row < (rows-1) && diff == cols) {
return true;
}
if(row > 0 && diff == -cols) {
return true;
}
}
return false;
}
function getLeft(tile) {
<!-- var width = $("#tiles").width(); -->
var col = tile % cols;
return Math.ceil((width / cols) * col);
}
function getTop(tile) {
<!-- var height = $("#tiles").height(); -->
var row = Math.floor(tile / cols);
return Math.ceil((height / rows) * row);
}
function is_solved() {
for(var i = 0; i < tiles.length; ++i) {
if(tiles[i] != i) {
return false;
}
}
return true;
}
//takes the tile object directly
function moveTile(tile, noAnimation) {
if(noAnimation) {
/*tile.css({
left: getLeft(empty) + "px",
top: getTop(empty) + "px",
});*/
var style = tile[0];
style.left = getLeft(empty);
style.top = getTop(empty);
<!-- style.top = getTop(empty) + "px"; -->
} else {
tile.finish().animate({
left: getLeft(empty) + "px",
top: getTop(empty) + "px",
queue: false
}, 100);
}
var tilePos = tile[0].curPos;
var temp = tiles[empty];
tiles[empty] = tiles[tilePos];
tiles[tilePos] = temp;
tile[0].curPos = empty;
empty = tilePos;
}
function tileClicked(tile) {
var tile = $("#tile" + tile);
var tilePos = tile[0].curPos;
if(isAdjacent(tilePos, empty)) {
moveTile(tile);
if(is_solved()) {
gameEnd();
}
}
}
function tileHovered(tile) {
var tileDiv = $("#tile" + tile);
if(tileDiv.hasClass("hover")) {
tileDiv.removeClass("hover");
tileDiv.finish().animate({
borderTopColor : "black", borderBottomColor: "black", borderLeftColor : "black", borderRightColor: "black"
}, 150);
} else {
if(!isAdjacent(tileDiv[0].curPos, empty)) return;
tileDiv.addClass("hover");
tileDiv.finish().animate({
borderTopColor : "red", borderLeftColor : "red", borderRightColor : "red", borderBottomColor : "red"
}, 150);
}
}
function prepareTiles() {
var tilesDiv = $("#tiles");
width = tilesDiv.css("width"), height = tilesDiv.css("height");
width = parseInt(width.substring(0, width.length - 2));
height = parseInt(height.substring(0, height.length - 2));
var config = $("#config")[0];
cols = parseInt(config.dimX.value);
rows = parseInt(config.dimY.value);
if(cols <= 0 || rows <=0 || isNaN(cols) || isNaN(rows) || (cols * rows) > 2500) return;
image = "URL('" + config.imageURL.value + "')";
var html = "";
for(var row = 0; row < rows; ++row) {
for(var col = 0; col < cols && !(row == (rows-1) && col == (cols-1)); ++col) {
var idx = (col + (row*cols));
var position = "top: " + getTop(idx) + "px; left: " + getLeft(idx) + "px;";
var background = "background-position: -" + (100/cols)*col*cols + "% -" + (100/rows)*row*rows + "%; background-image: " + image;
var style = "style=\"" + position + " " + background + "\"";
var events = "onclick=\"tileClicked(" + idx + ")\" onmouseover=\"tileHovered(" + idx + ")\" onmouseout=\"tileHovered(" + idx + ")\"";
html += "<div class=\"tile\" id=\"tile" + idx + "\" " + style + " " + events + "></div>\n";
}
}
tilesDiv.empty();
tilesDiv.append(html);
tiles = new Array(cols * rows);
for(i = 0; i < (cols * rows) - 1; ++i) {
tiles[i] = i;
$("#tile" + i)[0].curPos = i;
}
tiles[(cols * rows) - 1] = (cols * rows) - 1;
empty = cols * rows - 1;
var tileStyles = $("#tile_styles");
$(".tile").css({
"background-size": width + "px " + height + "px",
"width": width / cols + "px",
"height": height / rows + "px"
});
}
function shuffle() {
$("#tiles").css("background", "");
prepareTiles();
var numTiles = tiles.length;
//detach tiles
var tilesCopy = new Array(numTiles);
for(var i = 0; i < numTiles; ++i) {
var tile = $("#tile" + i);
tilesCopy[i] = tile;
tile.detach(); //TODO this shouldn't be necessary anymore
}
//TODO move 10 or so tiles, in the callback do the detached tile moves, then at the end move 10 tiles again while attached
var moves = new Array(4);
for(var i = 0; i < 1000*500; ++i) {
var added = 0
if((empty - cols) >= 0 && isAdjacent(empty - cols, empty)) {
moves[added++] = empty - cols;
}
if(numTiles > (empty + cols) && isAdjacent(empty + cols, empty)) {
moves[added++] = empty + cols;
}
if((empty - 1) >= 0 && isAdjacent(empty - 1, empty)) {
moves[added++] = empty - 1;
}
if(numTiles > (empty + 1) && isAdjacent(empty + 1, empty)) {
moves[added++] = empty + 1;
}
var move = moves[Math.floor(Math.random() * added)];
moveTile(tilesCopy[tiles[move]], true);
}
//reattach
var tilesDiv = $("#tiles");
for(var i = 0; i < (numTiles - 1); ++i) {
var tile = tilesCopy[i];
if(tile[0].top !== 'undefined') {
tile[0].style.top = tile[0].top + "px";
tile[0].style.left = tile[0].left + "px";
}
tilesDiv.append(tilesCopy[i]);
}
for(var i = 0; i < 15; ++i) {
var moves = [];
if((empty - cols) >= 0 && isAdjacent(empty - cols, empty)) {
moves.push(empty - cols);
}
if(numTiles > (empty + cols) && isAdjacent(empty + cols, empty)) {
moves.push(empty + cols);
}
if((empty - 1) >= 0 && isAdjacent(empty - 1, empty)) {
moves.push(empty - 1);
}
if(numTiles > (empty + 1) && isAdjacent(empty + 1, empty)) {
moves.push(empty + 1);
}
var move = moves[Math.floor(Math.random() * moves.length)];
moveTile($("#tile" + tiles[move]));
}
}
$(document).ready(function() {
prepareTiles();
});
</script>
<form id="config">
<table>
<tr>
<!-- todo only accept positive integer input -->
<td>X</td><td><input type="text" class="dim" id="dimX" value="3" onchange="prepareTiles()"></td>
<td>Y</td><td><input type="text" class="dim" id="dimY" value="3" onchange="prepareTiles()"></td>
</tr>
<tr>
<td>Image</td><td colspan="3"><input type="text" id="imageURL" value="img/cat.jpg" onchange="prepareTiles()"></td>
</tr>
<tr>
<td colspan="3"><input type="button" value="Shuffle" onclick="shuffle()"></td>
</tr>
</table>
</form>
<div id="tiles"></div>
</body>
</html>