-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
193 lines (170 loc) · 4.9 KB
/
index.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
<html>
<head>
<title>
Number Shuffle game
</title>
<style>
.data_shuffle,.blank{
width : 100px;
border : solid;
text-align:center;
}
.tab_shuffle{
border : solid;
}
table {
width:50%;
height:50%;
}
.blank{
background-color:#dcdcdc;
}
ul{
list-style:none;
}
</style>
</head>
<body onLoad='numShuffle.createGrid(0);'>
<span id="playButton" >
<label>Select Level: </label>
<button onClick="numShuffle.createGrid(0);" value="Easy">Easy</button>
<button onClick="numShuffle.createGrid(1);" value="Intermediate">Intermediate</button>
<button onClick="numShuffle.createGrid(2);" value="Hard">Hard</button>
</span>
<button id="resetButton" style="display:none;" value="Reset">Reset</button>
<hr/>
<h4>Number of Moves : <span id="mvesCnt"></span><h4>
<hr/>
<table id="grid" class="tab_shuffle">
</table>
<ul>
<li>
<strong>Instructions:</strong>
</li>
</ul>
<ol>
<li>
Movable cells can be horizontally or vertically adjacent to empty(Blank) cell.
</li>
<li>
Arrange all the numbers in ascending order to win the game.
</li>
</ol>
</body>
</html>
<style>
</style>
<script type="text/javascript">
var numShuffle =
{
data : {
neighbours : [],
mvesCnt : 0
},
getNeighbours : function(arrCnt,blnkIndex)
{
numShuffle.data.neighbours = [];
var blankElement = document.getElementsByClassName("blank")[0];
var blankID = blankElement.id;
var previous = blankElement.previousSibling;
var left = previous ? previous.innerHTML : -1;
var next = blankElement.nextSibling;
var top = parseInt(blankID,10)-level;
var bottom = (arrCnt > parseInt(blankID,10)+level) ? parseInt(blankID,10)+level : -1;
var right = next ? next.innerHTML : -1;
if(right !== -1){
numShuffle.data.neighbours.push(right);
}
if(left !== -1){
numShuffle.data.neighbours.push(left);
}
if(top > -1) {
numShuffle.data.neighbours.push(document.getElementById(top).innerHTML);
}
if(bottom > -1) {
numShuffle.data.neighbours.push(document.getElementById(bottom).innerHTML);
}
},
swap : function(event)
{
var target = event.target;
var id = target.id;
var value = target.innerHTML;
if(numShuffle.data.neighbours.indexOf(value) !== -1){
var blankElement= document.getElementsByClassName("blank")[0];
document.getElementById(blankElement.id).innerHTML = value;
document.getElementById(id).innerHTML = "blank";
document.getElementById(blankElement.id).className ="data_shuffle";
document.getElementById(id).className ="blank";
document.getElementById('mvesCnt').innerHTML = ++numShuffle.data.mvesCnt;
}
if( ! numShuffle.validate()){
numShuffle.getNeighbours(arrCnt,parseInt(document.getElementsByClassName("blank")[0].id,10));
}else{
alert( 'You completed Level: ' + userLevel + ' -- Starting Level: ' + userLevel);
numShuffle.createGrid(userLevel +1);
}
},
validate : function()
{
var valid = [], isValid = true,element;
for(var i=0;i< arrCnt;i++){
elemnt = '';
elemnt = parseInt(document.getElementById(i).innerHTML,10);
if(isNaN(elemnt)){
valid.push(9999);
}else{
valid.push(elemnt);
}
}
var sortedArr = JSON.parse(JSON.stringify(valid));
sortedArr.sort(function(a,b){
return a-b;
});
for(i=0;i< sortedArr.length;i++){
if(sortedArr[i] !== valid[i]){
isValid = false;
break;
}
}
return isValid;
},
createGrid : function(ll){
var tableGrid = '';
var blankId;
userLevel = ll;
level = parseInt(ll,10)+3;
grids = (level)*(level);
numShuffle.data.mvesCnt = 0; //Resetting moves count
var arrayGrid = [];
for(var i=0;i< grids;i++){
arrayGrid.push(i);
}
arrayGrid.sort(function(){
return Math.round(Math.random()) - 0.5;
});
document.getElementById('mvesCnt').innerHTML = numShuffle.data.mvesCnt;
tableGrid += '<tr class="row_shuffle">';
arrCnt = arrayGrid.length;
for(i=0;i< arrCnt;i++){
if(i%(level) === 0 && i !== 0){
tableGrid+= '</tr><tr class="row_shuffle">';
}
if((arrayGrid[i]) === 0){
tableGrid += '<td value="'+ (i) +'" id="'+ i +'" onClick="numShuffle.swap(event)" class="blank">blank</td>';
blankId = i;
} else{
tableGrid += '<td value='+ (i) +' id="'+ i +'" onClick="numShuffle.swap(event)" class="data_shuffle">'+ arrayGrid[i] +'</td>';
}
}
tableGrid += '</tr>';
document.getElementById('grid').innerHTML = tableGrid;
document.getElementById('resetButton').style = 'display:block';
var fn = function(){
numShuffle.createGrid(ll);
};
document.getElementById('resetButton').addEventListener('click',fn,false);
numShuffle.getNeighbours(arrCnt,blankId);
}
};
</script>