v5: кубик инициализируется сразу перемешанным
This commit is contained in:
parent
8afae1cff1
commit
2f64bf86e1
159
index.html
159
index.html
@ -719,12 +719,165 @@ document.addEventListener('touchend', () => {
|
||||
isDragging = false;
|
||||
});
|
||||
|
||||
initCube();
|
||||
setTimeout(() => scramble(), 500);
|
||||
|
||||
let moveHistory = [];
|
||||
let hintTimeout = null;
|
||||
|
||||
function initAndScramble() {
|
||||
initCube();
|
||||
|
||||
const layers = ['U', 'D', 'L', 'R', 'F', 'B'];
|
||||
const moves = 20;
|
||||
|
||||
for (let i = 0; i < moves; i++) {
|
||||
const layer = layers[Math.floor(Math.random() * layers.length)];
|
||||
const direction = Math.random() > 0.5 ? 1 : -1;
|
||||
|
||||
applyQuickMove(layer, direction);
|
||||
}
|
||||
|
||||
moveHistory = [];
|
||||
}
|
||||
|
||||
function applyQuickMove(layer, direction) {
|
||||
const affectedCubies = [];
|
||||
|
||||
for (let x = 0; x < 3; x++) {
|
||||
for (let y = 0; y < 3; y++) {
|
||||
for (let z = 0; z < 3; z++) {
|
||||
let include = false;
|
||||
if (layer === 'U' && cube[x][y][z].y === 1) include = true;
|
||||
if (layer === 'D' && cube[x][y][z].y === -1) include = true;
|
||||
if (layer === 'L' && cube[x][y][z].x === -1) include = true;
|
||||
if (layer === 'R' && cube[x][y][z].x === 1) include = true;
|
||||
if (layer === 'F' && cube[x][y][z].z === 1) include = true;
|
||||
if (layer === 'B' && cube[x][y][z].z === -1) include = true;
|
||||
|
||||
if (include) {
|
||||
affectedCubies.push(cube[x][y][z]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
affectedCubies.forEach(cubie => {
|
||||
let newX = cubie.x;
|
||||
let newY = cubie.y;
|
||||
let newZ = cubie.z;
|
||||
|
||||
if (layer === 'U' || layer === 'D') {
|
||||
const temp = newX;
|
||||
if (direction > 0) {
|
||||
newX = -newZ;
|
||||
newZ = temp;
|
||||
} else {
|
||||
newX = newZ;
|
||||
newZ = -temp;
|
||||
}
|
||||
} else if (layer === 'L' || layer === 'R') {
|
||||
const temp = newY;
|
||||
if (direction > 0) {
|
||||
newY = -newZ;
|
||||
newZ = temp;
|
||||
} else {
|
||||
newY = newZ;
|
||||
newZ = -temp;
|
||||
}
|
||||
} else if (layer === 'F' || layer === 'B') {
|
||||
const temp = newX;
|
||||
if (direction > 0) {
|
||||
newX = -newY;
|
||||
newY = temp;
|
||||
} else {
|
||||
newX = newY;
|
||||
newY = -temp;
|
||||
}
|
||||
}
|
||||
|
||||
const tempColors = { ...cubie.colors };
|
||||
if (layer === 'U' || layer === 'D') {
|
||||
if (direction > 0) {
|
||||
cubie.colors.front = tempColors.right;
|
||||
cubie.colors.right = tempColors.back;
|
||||
cubie.colors.back = tempColors.left;
|
||||
cubie.colors.left = tempColors.front;
|
||||
} else {
|
||||
cubie.colors.front = tempColors.left;
|
||||
cubie.colors.left = tempColors.back;
|
||||
cubie.colors.back = tempColors.right;
|
||||
cubie.colors.right = tempColors.front;
|
||||
}
|
||||
} else if (layer === 'L' || layer === 'R') {
|
||||
if (direction > 0) {
|
||||
cubie.colors.front = tempColors.top;
|
||||
cubie.colors.top = tempColors.back;
|
||||
cubie.colors.back = tempColors.bottom;
|
||||
cubie.colors.bottom = tempColors.front;
|
||||
} else {
|
||||
cubie.colors.front = tempColors.bottom;
|
||||
cubie.colors.bottom = tempColors.back;
|
||||
cubie.colors.back = tempColors.top;
|
||||
cubie.colors.top = tempColors.front;
|
||||
}
|
||||
} else if (layer === 'F' || layer === 'B') {
|
||||
if (direction > 0) {
|
||||
cubie.colors.top = tempColors.left;
|
||||
cubie.colors.left = tempColors.bottom;
|
||||
cubie.colors.bottom = tempColors.right;
|
||||
cubie.colors.right = tempColors.top;
|
||||
} else {
|
||||
cubie.colors.top = tempColors.right;
|
||||
cubie.colors.right = tempColors.bottom;
|
||||
cubie.colors.bottom = tempColors.left;
|
||||
cubie.colors.left = tempColors.top;
|
||||
}
|
||||
}
|
||||
|
||||
cubie.x = newX;
|
||||
cubie.y = newY;
|
||||
cubie.z = newZ;
|
||||
|
||||
updateCubiePosition(cubie.element, cubie.x, cubie.y, cubie.z);
|
||||
|
||||
const faceElements = cubie.element.querySelectorAll('.face');
|
||||
faceElements.forEach(face => {
|
||||
const faceClass = Array.from(face.classList).find(c => c.startsWith('face-') && !c.startsWith('face-face'));
|
||||
if (faceClass) {
|
||||
const faceName = faceClass.replace('face-', '');
|
||||
const colorName = cubie.colors[faceName] || 'black';
|
||||
face.className = `face face-${faceName} face-${colorName}`;
|
||||
if (faceName === 'front') face.style.transform = 'rotateY(0deg) translateZ(48px)';
|
||||
if (faceName === 'back') face.style.transform = 'rotateY(180deg) translateZ(48px)';
|
||||
if (faceName === 'right') face.style.transform = 'rotateY(90deg) translateZ(48px)';
|
||||
if (faceName === 'left') face.style.transform = 'rotateY(-90deg) translateZ(48px)';
|
||||
if (faceName === 'top') face.style.transform = 'rotateX(90deg) translateZ(48px)';
|
||||
if (faceName === 'bottom') face.style.transform = 'rotateX(-90deg) translateZ(48px)';
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const newCube = [];
|
||||
for (let x = 0; x < 3; x++) {
|
||||
newCube[x] = [];
|
||||
for (let y = 0; y < 3; y++) {
|
||||
newCube[x][y] = [];
|
||||
for (let z = 0; z < 3; z++) {
|
||||
newCube[x][y][z] = cube[x][y][z];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let x = 0; x < 3; x++) {
|
||||
for (let y = 0; y < 3; y++) {
|
||||
for (let z = 0; z < 3; z++) {
|
||||
const cubie = newCube[x][y][z];
|
||||
cube[cubie.x + 1][cubie.y + 1][cubie.z + 1] = cubie;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
initAndScramble();
|
||||
|
||||
function showHint() {
|
||||
const moveNames = {
|
||||
'U': { name: 'Верх (U)', axis: 'Y' },
|
||||
|
||||
Loading…
Reference in New Issue
Block a user