-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCube.js
More file actions
275 lines (259 loc) · 7.73 KB
/
Cube.js
File metadata and controls
275 lines (259 loc) · 7.73 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// 已绕晕自己的变量定义
// data=[
// {colors:[],
// up:1,
// down:2,
// left:3,
// right:4}
// ];
// front 前面的整面信息
// frontIndex 前面的编号
// frontColors 前面的颜色数据
// frontDirect 前面看各面的方位
// flank 侧面的整面信息
// flankIndex 侧面的编号
// flankColors 侧面的颜色数据
// flankDirect 侧面看前面的方位
// side 没有特指的一面
function Cube(n) {
this.n = n;
this.data = [];
this.initData();
}
function create2DArray(n, initValue) {
var arr = [];
for (var i = 0; i < n; i++) {
var rowArr = new Array(n);
for (var j = 0; j < n; j++) {
// rowArr[j]=initValue;
rowArr[j] = '(' + i + ',' + j + ',' + initValue + ')';
}
arr.push(rowArr);
}
return arr;
}
Cube.prototype.directionMap = ['up', 'right', 'down', 'left'];
/**
* [initData 初始化cube]
* @return {[type]} [description]
*/
Cube.prototype.initData = function() {
//各面数组的初始化
var side;
for (var i = 0; i < 6; i++) {
side = {};
side.colors = create2DArray(this.n, i);
this.data.push(side);
}
// 各面指针的初始化
// 0和5对面
for (i = 0; i < 6; i++) {
var sideIndexs = [];
for (var j = 0; j < 3; j++) {
if (j !== i && j !== (5 - i)) {
sideIndexs.push(j);
}
}
side = this.data[i];
side.up = sideIndexs[0];
side.down = 5 - sideIndexs[0];
if (i % 2) {
side.right = sideIndexs[1];
side.left = 5 - sideIndexs[1];
} else {
side.left = sideIndexs[1];
side.right = 5 - sideIndexs[1];
}
}
};
/**
* [rotateClockWise 顺时针转某一面]
* @param {int} frontIndex [前面的编号]
* @return {[type]} [description]
*/
Cube.prototype.rotateClockWise = function(frontIndex) {
this.rotateFront(frontIndex);
this.rotateFlank(frontIndex);
};
/**
* [rotateAntiClockWise 逆时针转某一面]
* @param {int} frontIndex [前面的编号]
* @return {[type]} [description]
*/
Cube.prototype.rotateAntiClockWise = function(frontIndex) {
this.rotateFront(frontIndex,true);
this.rotateFlank(frontIndex,true);
};
/**
* [rotateFront Front转动]
* @param {int} frontIndex [前面的编号]
* @param {boolean} isAntiClock [是否逆时针]
*/
Cube.prototype.rotateFront = function(frontIndex,isAntiClock) {
// 转的那面自身转的结果
var frontColors = this.data[frontIndex].colors;
var resultArr = create2DArray(this.n);
for (var x = 0; x < this.n; x++) {
for (var y = 0; y < this.n; y++) {
var n=this.n-1;
if (isAntiClock) {
resultArr[n-y][x] = frontColors[x][y];
}else{
resultArr[y][n - x] = frontColors[x][y];
}
}
}
this.data[frontIndex].colors=resultArr;
};
/**
* [rotateFlank flank转动]
* @param {int} frontIndex [前面的编号]
* @param {boolean} isAntiClock [是否逆时针]
*/
Cube.prototype.rotateFlank = function(frontIndex,isAntiClock) {
var rotateItems=this.getSideItems(frontIndex);
rotateItems=this.rotateSideItems(rotateItems,isAntiClock);
this.setSideItems(rotateItems);
};
/**
* [getSideItems 获取侧面要转动的信息]
* @param {int} frontIndex [前面的编号]
* @return {Object} [侧面要转动的信息]
*/
Cube.prototype.getSideItems = function(frontIndex) {
var directionMap = this.directionMap;
var front = this.data[frontIndex];
var rotateItems = {
frontIndex:frontIndex,
sideInfos: [],
colors: []
};
for (var k=0; k<directionMap.length; k++) {
var frontDirect=directionMap[k];
var flankIndex=front[frontDirect];
var flank=this.data[flankIndex];
var flankDirect;
for (var i = 0; i < directionMap.length; i++) {
if (flank[directionMap[i]] === frontIndex) {
flankDirect = directionMap[i];
break;
}
}
var arr=[];
var flankColors = flank.colors;
// 获取flankColors中转动的那一列/行
if (flankDirect === 'up') {
for (i = 0; i < this.n; i++) {
arr[i] = flankColors[0][i];
}
} else if (flankDirect === 'right') {
for (i = 0; i < this.n; i++) {
arr[i] = flankColors[i][2];
}
} else if (flankDirect === 'down') {
for (i = 0; i < this.n; i++) {
arr[i] = flankColors[2][i];
}
} else if (flankDirect === 'left') {
for (i = 0; i < this.n; i++) {
arr[i] = flankColors[i][0];
}
}
var sideInfo = {
flankIndex: flankIndex,
flankDirect: flankDirect,
frontDirect: frontDirect
};
rotateItems.sideInfos.push(sideInfo);
rotateItems.colors.push(arr);
}
return rotateItems;
};
/**
* [rotateSideItems 转动侧面]
* @param {Object} rotateItems [侧面要转动的信息]
* @param {Boolean} isAntiClock [是否逆时针]
* @return {Object} [侧面转动后的信息]
*/
Cube.prototype.rotateSideItems = function(rotateItems,isAntiClock) {
var frontIndex=rotateItems.frontIndex;
var sideInfos=rotateItems.sideInfos;
var colors=rotateItems.colors;
for (var k = 0; k < sideInfos.length; k++) {
var sideInfo=sideInfos[k];
var isReverse=isAntiClock?true:false;
if (sideInfo.flankDirect==='up'||sideInfo.flankDirect==='right') {
isReverse=!isReverse;
}
sideInfo.isReverse=isReverse;
if(isReverse){
colors[k].reverse();
}
}
if(isAntiClock){
var firstColor=colors.shift();
colors.push(firstColor);
}else{
var lastColor = colors.pop();
colors.unshift(lastColor);
}
return rotateItems;
};
/**
* [setSideItems 设置data转动后侧面信息]
* @param {Object} rotateItems [侧面转动后的信息]
*/
Cube.prototype.setSideItems = function(rotateItems) {
var front = this.data[rotateItems.frontIndex];
var sideInfos=rotateItems.sideInfos;
var colors=rotateItems.colors;
for (var k = 0; k < sideInfos.length; k++) {
var sideInfo = sideInfos[k];
var flank = this.data[sideInfo.flankIndex];
var flankColors = flank.colors;
var flankDirect = sideInfo.flankDirect;
var frontDirect = sideInfo.frontDirect;
var isReverse = sideInfo.isReverse;
var color = colors[k];
if (isReverse) {
color.reverse();
}
var n=this.n;
if (flankDirect === 'up') {
for (i = 0; i < n; i++) {
flankColors[0][i] = color[i];
}
} else if (flankDirect === 'right') {
for (i = 0; i < n; i++) {
flankColors[i][n-1] = color[i];
}
} else if (flankDirect === 'down') {
for (i = 0; i < n; i++) {
flankColors[n-1][i] = color[i];
}
} else if (flankDirect === 'left') {
for (i = 0; i < n; i++) {
flankColors[i][0] = color[i];
}
}
}
};
/**
* [logColors 打印各面的colors,调试用]
* @return {[type]} [description]
*/
Cube.prototype.logColors = function() {
var sides = this.data;
var n = this.n;
for (var k = 0; k < sides.length; k++) {
console.log(k);
var side = sides[k];
var sideColors = side.colors;
for (var i = 0; i < n; i++) {
console.log(sideColors[i].join(" "));
}
}
};
var cube = new Cube(3);
cube.rotateClockWise(4);
cube.logColors();