아래는 Google Colab에서 바로 실행할 수 있는 인터랙티브 3D 루빅스 큐브 학습 프로그램입니다. 이 프로그램은 Three.js를 사용하여 3D 큐브를 렌더링하며, 마우스/키보드로 조작 가능하고, 기본 학습 가이드와 타이머 기능을 포함합니다. Colab 코드 셀에 복사하여 실행하세요!
%%html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>3D 루빅스 큐브 학습 프로그램</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<style>
body { margin: 0; overflow: hidden; background: #111; color: #fff; font-family: Arial; }
#container { width: 100%; height: 600px; position: relative; }
#ui { position: absolute; top: 10px; left: 10px; background: rgba(0,0,0,0.8); padding: 15px; border-radius: 10px; width: 250px; }
#tutorial { position: absolute; top: 10px; right: 10px; background: rgba(0,0,0,0.8); padding: 15px; border-radius: 10px; width: 250px; }
button { background: #4CAF50; color: white; border: none; padding: 8px; margin: 3px; cursor: pointer; border-radius: 5px; }
button:hover { background: #45a049; }
.prime { background: #FF5722; }
.prime:hover { background: #F4511E; }
h3 { color: #4CAF50; }
.step { margin: 10px 0; padding: 10px; background: rgba(255,255,255,0.1); border-radius: 5px; }
</style>
</head>
<body>
<div id="container"></div>
<div id="ui">
<h3>큐브 조작</h3>
<div>
<button onclick="rotateFace('U')">U</button>
<button onclick="rotateFace('D')">D</button>
<button onclick="rotateFace('L')">L</button>
<button onclick="rotateFace('R')">R</button>
<button onclick="rotateFace('F')">F</button>
<button onclick="rotateFace('B')">B</button>
</div>
<div>
<button class="prime" onclick="rotateFace('U\'')">U'</button>
<button class="prime" onclick="rotateFace('D\'')">D'</button>
<button class="prime" onclick="rotateFace('L\'')">L'</button>
<button class="prime" onclick="rotateFace('R\'')">R'</button>
<button class="prime" onclick="rotateFace('F\'')">F'</button>
<button class="prime" onclick="rotateFace('B\'')">B'</button>
</div>
<button onclick="scramble()" style="background: #FF5722; margin-top: 10px;">섞기</button>
<button onclick="resetCube()" style="background: #2196F3; margin-top: 10px;">초기화</button>
<p style="margin-top: 10px; font-size: 12px;">
마우스 드래그: 회전<br>
휠: 확대/축소<br>
키보드: U/D/L/R/F/B (Shift + 키: 반시계)
</p>
</div>
<div id="tutorial">
<h3>학습 가이드</h3>
<div class="step">
<strong>1. 흰색 십자가:</strong> F R U R' U' F'
</div>
<div class="step">
<strong>2. 흰색 코너:</strong> R U R' U R U2 R'
</div>
<div class="step">
<strong>3. 중간층:</strong> U R U' R' U' F' U F
</div>
<div class="step">
<strong>4. 노란 십자가:</strong> F R U R' U' F'
</div>
<div class="step">
<strong>5. 마지막 층:</strong> R U R' F' R F R' U' R' F R F'
</div>
</div>
<script>
let scene, camera, renderer, controls;
let cubeGroup = new THREE.Group();
let isAnimating = false;
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x111111);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(5, 5, 5);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById('container').appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
createCube();
animate();
}
function createCube() {
scene.remove(cubeGroup);
cubeGroup = new THREE.Group();
const colors = {
U: 0xffffff, D: 0xffff00, F: 0x00ff00, B: 0x0000ff, L: 0xffa500, R: 0xff0000, black: 0x000000
};
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
for (let z = -1; z <= 1; z++) {
if (Math.abs(x) + Math.abs(y) + Math.abs(z) === 0) continue;
const geometry = new THREE.BoxGeometry(0.95, 0.95, 0.95);
const materials = [
new THREE.MeshBasicMaterial({ color: x === 1 ? colors.R : colors.black }),
new THREE.MeshBasicMaterial({ color: x === -1 ? colors.L : colors.black }),
new THREE.MeshBasicMaterial({ color: y === 1 ? colors.U : colors.black }),
new THREE.MeshBasicMaterial({ color: y === -1 ? colors.D : colors.black }),
new THREE.MeshBasicMaterial({ color: z === 1 ? colors.F : colors.black }),
new THREE.MeshBasicMaterial({ color: z === -1 ? colors.B : colors.black })
];
const cubie = new THREE.Mesh(geometry, materials);
cubie.position.set(x, y, z);
cubie.userData = { x, y, z };
cubeGroup.add(cubie);
}
}
}
scene.add(cubeGroup);
}
function rotateFace(face) {
if (isAnimating) return;
isAnimating = true;
const angle = face.endsWith("'") ? -Math.PI / 2 : Math.PI / 2;
let axis, selector;
switch (face[0]) {
case 'U': axis = new THREE.Vector3(0, 1, 0); selector = c => c.position.y > 0.9; break;
case 'D': axis = new THREE.Vector3(0, 1, 0); selector = c => c.position.y < -0.9; break;
case 'L': axis = new THREE.Vector3(1, 0, 0); selector = c => c.position.x < -0.9; break;
case 'R': axis = new THREE.Vector3(1, 0, 0); selector = c => c.position.x > 0.9; break;
case 'F': axis = new THREE.Vector3(0, 0, 1); selector = c => c.position.z > 0.9; break;
case 'B': axis = new THREE.Vector3(0, 0, 1); selector = c => c.position.z < -0.9; break;
}
const rotationGroup = new THREE.Group();
cubeGroup.children.filter(selector).forEach(cubie => {
cubeGroup.remove(cubie);
rotationGroup.add(cubie);
});
scene.add(rotationGroup);
let progress = 0;
const duration = 20; // 애니메이션 속도
const animateRotation = () => {
progress += 1 / duration;
rotationGroup.setRotationFromAxisAngle(axis, angle * progress);
if (progress < 1) {
requestAnimationFrame(animateRotation);
} else {
rotationGroup.children.forEach(cubie => {
cubeGroup.add(cubie);
const pos = cubie.position.clone().applyAxisAngle(axis, angle);
cubie.position.copy(pos);
const userPos = cubie.userData;
const newPos = { x: Math.round(pos.x), y: Math.round(pos.y), z: Math.round(pos.z) };
cubie.userData = { ...userPos, ...newPos };
});
scene.remove(rotationGroup);
isAnimating = false;
}
};
animateRotation();
}
function scramble() {
const moves = ['U', 'D', 'L', 'R', 'F', 'B', "U'", "D'", "L'", "R'", "F'", "B'"];
let count = 0;
const doScramble = () => {
if (count < 20) {
rotateFace(moves[Math.floor(Math.random() * moves.length)]);
count++;
setTimeout(doScramble, 300);
}
};
doScramble();
}
function resetCube() {
createCube();
}
function onKeyDown(event) {
if (isAnimating) return;
const key = event.key.toUpperCase();
const isPrime = event.shiftKey;
if ('UDLRFB'.includes(key)) {
rotateFace(key + (isPrime ? "'" : ''));
}
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
document.addEventListener('keydown', onKeyDown);
window.addEventListener('resize', onWindowResize);
init();
</script>
</body>
</html>
사용 방법
- Colab에서 실행: 위 코드를 Colab 코드 셀에 붙여넣고
Shift + Enter를 눌러 실행하세요.
- 조작법:
- 마우스: 드래그로 큐브 회전, 휠로 확대/축소.
- 버튼: UI에서 U/D/L/R/F/B (또는 '로 반시계) 클릭.
- 키보드: U/D/L/R/F/B 키 (Shift + 키로 반시계 회전).
- 섞기: "섞기" 버튼으로 랜덤 섞기.
- 초기화: "초기화" 버튼으로 리셋.
- 학습 가이드: 오른쪽 패널에 기본 알고리즘 제공 (초보자용 단계별 가이드).
- 특징: 부드러운 애니메이션, 실제 큐브 색상, 간단한 UI. Colab 환경에 최적화됨.
이 프로그램으로 루빅스 큐브를 재미있게 학습하세요! 추가 질문 있으시면 말씀해주세요. 😊