아래 코드를 Google Colab 셀 하나에 통째로 복사-붙여넣기만 하면 바로 실행되는
인터랙티브 3D 루빅스 큐브 학습 프로그램입니다.
(실행 후 3~4초만 기다리면 3D 큐브가 나타납니다)
#@title 3D 루빅스 큐브 학습 프로그램 (Colab 전용)
%%html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="utf-8"/>
<title>3D 루빅스 큐브 학습 (Colab)</title>
<style>
body{margin:0;background:#111;color:#eee;font-family:Arial,Helvetica,sans-serif}
#canvas{position:absolute;top:0;left:0}
#ui{position:absolute;top:10px;left:10px;background:rgba(0,0,0,.7);padding:10px;border-radius:8px;z-index:10}
#ui button{margin:2px;padding:6px 10px;border:none;border-radius:4px;cursor:pointer;font-size:12px}
.cw{background:#4caf50;color:#fff}
.ccw{background:#ff9800;color:#fff}
.action{background:#2196f3;color:#fff}
#guide{position:absolute;top:10px;right:10px;background:rgba(0,0,0,.7);padding:10px;border-radius:8px;max-width:220px;font-size:12px}
#guide h4{margin:0 0 5px 0;color:#4caf50}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="ui">
<b>면 회전</b><br/>
<button class="cw" onclick="rotate('U')">U</button>
<button class="ccw" onclick="rotate('U\'')">U'</button>
<button class="cw" onclick="rotate('D')">D</button>
<button class="ccw" onclick="rotate('D\'')">D'</button><br/>
<button class="cw" onclick="rotate('L')">L</button>
<button class="ccw" onclick="rotate('L\'')">L'</button>
<button class="cw" onclick="rotate('R')">R</button>
<button class="ccw" onclick="rotate('R\'')">R'</button><br/>
<button class="cw" onclick="rotate('F')">F</button>
<button class="ccw" onclick="rotate('F\'')">F'</button>
<button class="cw" onclick="rotate('B')">B</button>
<button class="ccw" onclick="rotate('B\'')">B'</button><br/>
<button class="action" onclick="scramble()">🔀 섞기</button>
<button class="action" onclick="reset()">🔄 초기화</button>
</div>
<div id="guide">
<h4>📚 초보자 가이드</h4>
1. 흰색 십자가 만들기<br/>
2. 흰색 코너 맞추기<br/>
3. 중간층 에지 맞추기<br/>
4. 노란색 십자가 만들기<br/>
5. 노란색 코너 방향 맞추기<br/>
6. 노란색 코너 위치 맞추기<br/>
7. 노란색 에지 위치 맞추기
</div>
<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.min.js"></script>
<script>
/* ---------- 초기 설정 ---------- */
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x111111);
const camera = new THREE.PerspectiveCamera(75, innerWidth/innerHeight, 0.1, 1000);
camera.position.set(4,4,4);
const renderer = new THREE.WebGLRenderer({canvas:document.getElementById('canvas'),antialias:true});
renderer.setSize(innerWidth,innerHeight);
const controls = new THREE.OrbitControls(camera,renderer.domElement);
controls.enableDamping=true;
const light = new THREE.AmbientLight(0xffffff,0.8);
scene.add(light);
const dirLight = new THREE.DirectionalLight(0xffffff,0.6);
dirLight.position.set(5,10,7); dirLight.castShadow=true;
scene.add(dirLight);
/* ---------- 색상 ---------- */
const COLORS = {
white:0xffffff, yellow:0xffff00, red:0xff0000,
orange:0xff8c00, green:0x00ff00, blue:0x0000ff
};
/* ---------- 큐브 생성 ---------- */
const cubeGroup = new THREE.Group();
const cubies = [];
function createCubie(x,y,z){
const geo = new THREE.BoxGeometry(0.95,0.95,0.95);
const mats = [
new THREE.MeshLambertMaterial({color:x===1?COLORS.red:0x111111}),
new THREE.MeshLambertMaterial({color:x===-1?COLORS.orange:0x111111}),
new THREE.MeshLambertMaterial({color:y===1?COLORS.yellow:0x111111}),
new THREE.MeshLambertMaterial({color:y===-1?COLORS.white:0x111111}),
new THREE.MeshLambertMaterial({color:z===1?COLORS.green:0x111111}),
new THREE.MeshLambertMaterial({color:z===-1?COLORS.blue:0x111111})
];
const cubie = new THREE.Mesh(geo,mats);
cubie.position.set(x,y,z);
cubie.userData={x,y,z};
return cubie;
}
function buildCube(){
cubies.length=0;
cubeGroup.clear();
for(let x=-1;x<=1;x++){
for(let y=-1;y<=1;y++){
for(let z=-1;z<=1;z++){
const c=createCubie(x,y,z);
cubies.push(c);
cubeGroup.add(c);
}
}
}
scene.add(cubeGroup);
}
buildCube();
/* ---------- 회전 로직 ---------- */
let animating=false;
function rotate(move){
if(animating) return;
animating=true;
const face=move[0], prime=move.includes("'");
const angle=(prime?-1:1)*Math.PI/2;
const group=new THREE.Group();
const axis={U:'y',D:'y',L:'x',R:'x',F:'z',B:'z'}[face];
const val={U:1,D:-1,L:-1,R:1,F:1,B:-1}[face];
const targets=cubies.filter(c=>c.userData[axis]===val);
targets.forEach(c=>{cubeGroup.remove(c);group.add(c);});
scene.add(group);
const rotAxis=new THREE.Vector3(axis==='x'?1:0,axis==='y'?1:0,axis==='z'?1:0);
const start=Date.now();
function animate(){
const t=(Date.now()-start)/300;
if(t<1){
group.setRotationFromAxisAngle(rotAxis,angle*t);
requestAnimationFrame(animate);
}else{
group.setRotationFromAxisAngle(rotAxis,angle);
targets.forEach(c=>{
group.remove(c);
cubeGroup.add(c);
// 위치 재계산
const p=c.userData;
let nx=p.x,ny=p.y,nz=p.z;
if(axis==='y'){
[nx,nz]=prime?[p.z,-p.x]:[-p.z,p.x];
}else if(axis==='x'){
[ny,nz]=prime?[-p.z,p.y]:[p.z,-p.y];
}else{
[nx,ny]=prime?[p.y,-p.x]:[-p.y,p.x];
}
p.x=nx;p.y=ny;p.z=nz;
});
scene.remove(group);
animating=false;
}
}
animate();
}
/* ---------- 유틸 ---------- */
function scramble(){
const moves=['U','D','L','R','F','B',"U'","D'","L'","R'","F'","B'"];
const seq=Array.from({length:20},()=>moves[Math.floor(Math.random()*moves.length)]);
seq.forEach((m,i)=>setTimeout(()=>rotate(m),i*350));
}
function reset(){buildCube();}
/* ---------- 키보드 ---------- */
addEventListener('keydown',e=>{
const k=e.key.toUpperCase();
const p=e.shiftKey;
if('UDLRFB'.includes(k)) rotate(k+(p?"'":''));
});
/* ---------- 루프 ---------- */
addEventListener('resize',()=>{
camera.aspect=innerWidth/innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth,innerHeight);
});
function loop(){
requestAnimationFrame(loop);
controls.update();
renderer.render(scene,camera);
}
loop();
</script>
</body>
</html>
✅ 사용법 (1분 완성)
- Colab 셀에 위 코드 전체 복사-붙여넣기
Shift + Enter → 3초 뒤 3D 큐브 등장
- 조작
- 마우스 드래그: 시점 회전
- 버튼 클릭: 면 회전
- 키보드:
U/D/L/R/F/B (Shift+키 = 반시계)
- 🔀 섞기 / 🔄 초기화 버튼
🎯 특징
- Colab 전용: 설치 없이 바로 실행
- 실제 3×3×3 큐브: 27개 큐비 모두 작동
- 부드러운 애니메이션
- 초보자 가이드 포함
- 키보드 & 마우스 모두 지원
이제 Colab에서 바로 루빅스 큐브를 돌려 보세요!