-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
193 lines (155 loc) · 3.7 KB
/
scripts.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
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
var grid1 = [
["white", 6],
["blue", 6],
["blue", 2],
["red", 3],
["yellow", 4],
["white", 3],
["blue", 3],
["green", 4],
["white", 5]
];
var grid2 = [
["pink", 6],
["yellow", 5],
["pink", 1],
["red", 1],
["blue", 5],
["yellow", 6],
["green", 1],
["red", 5],
["yellow", 1]
];
var grid3 = [
["pink", 5],
["pink", 2],
["pink", 3],
["red", 6],
["red", 4],
["yellow", 3],
["blue", 4],
["green", 5],
["green", 3]
];
var grid0 = [
["yellow", 2],
["white", 1],
["blue", 1],
["white", 2],
["green", 2],
["red", 2],
["pink", 4],
["green", 6],
["white", 4]
];
var screenWidth = $(window).width();
var screenHeight = $(window).height();
var cellSize = 0;
if (screenWidth > screenHeight) {
cellSize = screenHeight / 6;
} else {
cellSize = screenWidth / 6;
}
cellSize = cellSize * .95;
$('th').height(cellSize + 'px');
$('th').width(cellSize + 'px');
var rotateGrid = function(target){
var newGrid = [];
var rowLength = Math.sqrt(target.length);
newGrid.length = target.length
for (var i = 0; i < target.length; i++) {
//convert to x/y
var x = i % rowLength;
var y = Math.floor(i / rowLength);
//find new x/y
var newX = rowLength - y - 1;
var newY = x;
//convert back to index
var newPosition = newY * rowLength + newX;
newGrid[newPosition] = target[i];
}
return newGrid;
}
var randomRotate = function(target){
var count = Math.floor(Math.random() * 4);
for(var i=0;i<count;i++) {
target = rotateGrid(target);
}
return target;
}
grid0 = randomRotate(grid0);
grid1 = randomRotate(grid1);
grid2 = randomRotate(grid2);
grid3 = randomRotate(grid3);
var grids = [];
grids.push(grid1);
grids.push(grid2);
grids.push(grid3);
grids.push(grid0);
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var order = [1,2,3,0];
shuffle(order);
for (var i = 0; i < grid0.length; i++) {
$('table:eq('+ order[0]+') th:eq(' + i + ')')
.addClass(grid0[i][0])
.append('<span>' + grid0[i][1] + '</span>');
}
for (var i = 0; i < grid1.length; i++) {
$('table:eq('+ order[1]+') th:eq(' + i + ')')
.addClass(grid1[i][0])
.append('<span>' + grid1[i][1] + '</span>');
}
for (var i = 0; i < grid2.length; i++) {
$('table:eq('+ order[2]+') th:eq(' + i + ')')
.addClass(grid2[i][0])
.append('<span>' + grid2[i][1] + '</span>');
}
for (var i = 0; i < grid3.length; i++) {
$('table:eq('+ order[3]+') th:eq(' + i + ')')
.addClass(grid3[i][0])
.append('<span>' + grid3[i][1] + '</span>');
}
$('span').css('font-size', cellSize * .6 + 'px');
//randomize start and start/goal
function reset(){
$('span').each(function(){
$(this).removeClass('goal');
});
while($('.goal').length < 3) {
var position = Math.floor(Math.random() * $('span').length);
$('span:eq('+position+')').addClass('goal');
}
}
reset();
$('.timer').width(cellSize * 6 + 'px');
$('.refresher').width(cellSize * 6 + 'px');
var count = 60;
var timer = setInterval(function() {
count--;
$(".timer").width(count / 60 * cellSize * 6 +'px');
if(count == 1) {reset(); count = 60;}
}, 1000);
var hrCounter = 60 * 3;
var hrTimer = setInterval(function() {
hrCounter--;
$(".refresher").width(hrCounter / 60 / 3 * cellSize * 6 +'px');
if(hrCounter == 1) {location.reload();};
}, 1000);
$('body').click(function(){
count = 60;
reset();
});
$(window).resize(function(){location.reload();});