-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
74 lines (65 loc) · 1.84 KB
/
script.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
$(document).ready(function(){
//INIT BOARD
var xx = 0, yy = 0, xClicked = 0, yClicked = 0;
//create array
var x = new Array(4);
for (var i = 0; i < 4; i++) {
x[i] = new Array(4);
}
//put random numbers into array
for(var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
var ok = false;
while(!ok) {
var temp = Math.floor(Math.random()*16);
ok = true;
for(var ii = 0; ii < 4; ii++) {
for(var jj = 0; jj < 4; jj++) {
if(temp === x[ii][jj]) {
ok = false;
}
}
}
if(ok) {
x[i][j] = temp;
if(temp === 0) {
xx = i;
yy = j;
}
}
}
temp = '#'+i+j;
$(temp).find('p').text(x[i][j]);
}
}
//BLACK DIV
temp = "#"+xx+yy;
$(temp).css('background', 'black');
$(temp).css('border', '2px solid black');
$(temp).find('p').css('color', 'black');
//START GAME... :D
$('.square').on('click', function() {
temp = $(this).attr('id');
xClicked = Math.floor(temp / 10);
yClicked = temp % 10;
//VALID MOVE? xx, yy, xClicked, yClicked
if((xx === xClicked || yy === yClicked) && (Math.abs(xx - xClicked) === 1 || Math.abs(yy - yClicked) === 1)) {
console.log("Valid.");
x[xx][yy] = x[xClicked][yClicked];
x[xClicked][yClicked] = 0;
temp = '#' + xClicked + yClicked;
$(temp).css('background', 'black');
$(temp).css('border', '2px solid black');
$(temp).find('p').css('color', 'black');
temp = '#' + xx + yy;
$(temp).css('background', '#E6461A');
$(temp).css('border', '2px solid white');
$(temp).find('p').text(x[xx][yy]);
$(temp).find('p').css('color', 'white');
xx = xClicked;
yy = yClicked;
} else {
console.log(xx, xClicked, yy, yClicked);
}
});
});