-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.html
More file actions
185 lines (173 loc) · 4.65 KB
/
grid.html
File metadata and controls
185 lines (173 loc) · 4.65 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
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Cube</title>
<style>
div#cube {
margin: 0 auto;
}
div#cube table {
margin: 0 auto;
}
div#cube table tbody tr td {
border:1px solid black;
width: 100px;
height: 100px;
text-align:center;
font-size:28px;
}
div#cube table tbody tr:first-child td,
div#cube table tbody tr td:first-child,
div#cube table tbody tr td:last-child,
div#cube table tbody tr:last-child td {
background-color: #EEE;
}
div#cube table tbody tr:first-child td:first-child,
div#cube table tbody tr:first-child td:last-child,
div#cube table tbody tr:last-child td:first-child,
div#cube table tbody tr:last-child td:last-child {
visibility: hidden;
}
div#cube p {
text-align: center;
}
div#cube p button {
font-size: 40px;
}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var cells = [];
var cols;
var rows;
var color = function(){
for( var y=1; y<=rows; y++ ) {
for( var x=1; x<=cols; x++ ) {
$("div#cube table tbody tr td.row"+y+".col"+x).css( 'background-color', cells[y-1][x-1] );
}
}
}
var rotate_row_right = function(row){
cells[row].unshift( cells[row].pop() );
color();
};
var rotate_row_left = function(row){
cells[row].push( cells[row].shift() );
color();
};
var rotate_column_up = function(column){
tmp=cells[0][column];
for( var c=0; c<(rows-1); c++){
cells[c][column]=cells[c+1][column];
}
cells[rows-1][column]=tmp
color();
};
var rotate_column_down = function(column){
tmp=cells[rows-1][column];
for( var c=rows-1; c>0; c--){
cells[c][column]=cells[c-1][column];
}
cells[0][column]=tmp
color();
};
var scramble = function(){
for( var i=0; i<20; i++ ){
f=parseInt(Math.random()*4);
switch (f) {
case 0:
rotate_row_right( parseInt(Math.random()*rows) );
break;
case 1:
rotate_row_left( parseInt(Math.random()*rows) );
break;
case 2:
rotate_column_up( parseInt(Math.random()*cols) );
break;
case 3:
rotate_column_down( parseInt(Math.random()*cols) );
break;
}
}
}
var colors = ['red','yellow','blue','orange','green'];
$(function(){
// http://www.developerdrive.com/2013/08/turning-the-querystring-into-a-json-object-using-javascript/
var pairs = location.search.slice(1).split('&');
var result = {};
pairs.forEach(function(pair) {
pair = pair.split('=');
result[pair[0]] = decodeURIComponent(pair[1] || '');
});
cols = parseInt( (result['cols'] || 3), 10);
rows = parseInt( (result['rows'] || 4), 10);
for( var y=0; y<rows; y++ ) {
cells[y]=[];
for( var x=0; x<cols; x++ ) {
cells[y][x]=colors[y];
}
}
for( var y=0; y<(rows+2); y++ ) {
s="<tr class='row"+y+"'>";
for( var x=0; x<(cols+2); x++ ) {
s+="<td class='row"+y+" col"+x+"'> </td>";
}
s+="</tr>";
$('div#cube table tbody').append(s);
color();
}
$("div#cube button#scramble").click( scramble );
for( var y=0; y<(rows+2); y++ ) {
for( var x=0; x<(cols+2); x++ ) {
if( y==0 && x>0 && x<(cols+1) ){
$("div#cube table tbody tr td.row"+y+".col"+x).text( "^" );
$("div#cube table tbody tr td.row"+y+".col"+x).click({ row: y, column: x }, function(e){
rotate_column_up( e.data.column - 1 );
});
} else if( y==(rows+1) && x>0 && x<(cols+1) ){
$("div#cube table tbody tr td.row"+y+".col"+x).text( "v" );
$("div#cube table tbody tr td.row"+y+".col"+x).click({ row: y, column: x }, function(e){
rotate_column_down( e.data.column - 1 );
});
} else if( x==0 && y>0 && y<(rows+1) ){
$("div#cube table tbody tr td.row"+y+".col"+x).text( "<" );
$("div#cube table tbody tr td.row"+y+".col"+x).click({ row: y, column: x }, function(e){
rotate_row_left( e.data.row - 1 );
});
} else if( x==(cols+1) && y>0 && y<(rows+1) ){
$("div#cube table tbody tr td.row"+y+".col"+x).text( ">" );
$("div#cube table tbody tr td.row"+y+".col"+x).click({ row: y, column: x }, function(e){
rotate_row_right( e.data.row - 1 );
});
}
}
}
});
//]]>
</script>
</head>
<body>
<div id='cube'>
<table><tbody></tbody></table>
<p><button id='scramble'>Scramble</button></p>
<form>
<p><label for='rows'>Rows</label>
<select name='rows'>
<option>2</option>
<option selected='selected'>3</option>
<option>4</option>
<option>5</option>
</select></p>
<p><label for='cols'>Columns</label>
<select name='cols'>
<option>2</option>
<option selected='selected'>3</option>
<option>4</option>
<option>5</option>
</select></p>
<p><input type='submit' value='Resize and Restart'/></form></p>
</div>
</body>
</html>