-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (53 loc) · 1.72 KB
/
script.js
File metadata and controls
60 lines (53 loc) · 1.72 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
function swapTiles(cell1, cell2) {
var temp = document.getElementById(cell1).className;
document.getElementById(cell1).className = document.getElementById(cell2).className;
document.getElementById(cell2).className = temp;
}
function shuffle() {
//Use nested loops to access each cell of the 3x3 grid
for(var row=1; row <=3; row++) //foreach row of the 3x3 grid
{
//foreach column in this row
for (var column=1; column <=3; column++) {
//pick a random row from 1 to 3
var row2=Math.floor(Math.random()*3+1);
//pick a random column from 1 to 3
var column2=Math.floor(Math.random()*3+1);
swapTiles("cell"+row+column, "cell"+row2+column2); //swap the look & feel of both cells
}
}
}
function clickTile(row, column) {
var cell = document.getElementById("cell"+row+column);
var tile = cell.className;
if(tile!="tile9") {
//check if the white tile is on the right
if(column<3) {
if(document.getElementById("cell"+row+(column+1)).className=="tile9") {
swapTiles("cell"+row+column, "cell"+row+(column+1));
return;
}
}
//check if the white tile is on the left
if(column>1) {
if(document.getElementById("cell"+row+(column-1)).className=="tile9") {
swapTiles("cell"+row+column,"cell"+row+(column-1));
return;
}
}
//check if the white tile is above
if(row > 1) {
if(document.getElementById("cell"+(row-1)+column).className=="tile9") {
swapTiles("cell"+row+column, "cell"+(row-1)+column);
return;
}
}
//check if the white tile is below
if(row < 3) {
if(document.getElementById("cell"+(row+1)+column).className=="tile9") {
swapTiles("cell"+row+column, "cell"+(row+1)+column);
return;
}
}
}
}