Skip to content
The Chat Archive
mistral-medium-2505· Mistral AI LMArena Human Preference 140K

인터랙티브로 작동하는 3D 루빅스 큐브 학습 프로그을 colab용으로 만들어줘

  1. User

    인터랙티브로 작동하는 3D 루빅스 큐브 학습 프로그을 colab용으로 만들어줘

  2. Assistant

    Google Colab용 3D 루빅스 큐브 학습 프로그램

    아래는 Google Colab에서 바로 실행 가능한 완전한 3D 루빅스 큐브 학습 프로그램입니다. 이 프로그램은 Three.js를 기반으로 하며, 실제 루빅스 큐브와 같은 인터랙티브한 조작이 가능합니다.

    %%html
    <!DOCTYPE html>
    <html>
    <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;
                font-family: 'Arial', sans-serif;
                background-color: #121212;
                color: #ffffff;
            }
            #container {
                width: 100%;
                height: 100vh;
                position: relative;
            }
            #ui-panel {
                position: absolute;
                top: 20px;
                left: 20px;
                background: rgba(0, 0, 0, 0.8);
                padding: 15px;
                border-radius: 10px;
                width: 250px;
                backdrop-filter: blur(5px);
                z-index: 100;
            }
            #tutorial-panel {
                position: absolute;
                top: 20px;
                right: 20px;
                background: rgba(0, 0, 0, 0.8);
                padding: 15px;
                border-radius: 10px;
                width: 250px;
                backdrop-filter: blur(5px);
                z-index: 100;
            }
            h2 {
                margin-top: 0;
                color: #4CAF50;
                font-size: 1.2rem;
            }
            .control-group {
                margin-bottom: 15px;
            }
            .button-grid {
                display: grid;
                grid-template-columns: repeat(3, 1fr);
                gap: 5px;
            }
            button {
                background: #4CAF50;
                color: white;
                border: none;
                padding: 8px;
                border-radius: 5px;
                cursor: pointer;
                font-size: 14px;
                transition: all 0.2s;
            }
            button:hover {
                background: #45a049;
                transform: scale(1.05);
            }
            .prime-btn {
                background: #FF9800;
            }
            .prime-btn:hover {
                background: #F57C00;
            }
            .action-btn {
                width: 100%;
                margin: 5px 0;
                padding: 10px;
            }
            .step {
                margin-bottom: 10px;
                padding: 10px;
                background: rgba(76, 175, 80, 0.1);
                border-radius: 5px;
                font-size: 14px;
            }
            .step-title {
                font-weight: bold;
                color: #4CAF50;
                margin-bottom: 5px;
            }
            .status {
                margin-top: 15px;
                padding: 10px;
                background: rgba(0, 0, 0, 0.3);
                border-radius: 5px;
                font-size: 12px;
            }
        </style>
    </head>
    <body>
        <div id="container">
            <div id="ui-panel">
                <h2>🎮 컨트롤 패널</h2>
                <div class="control-group">
                    <h3>면 회전</h3>
                    <div class="button-grid">
                        <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>
                        <button class="prime-btn" onclick="rotateFace('U\'')">U'</button>
                        <button class="prime-btn" onclick="rotateFace('D\'')">D'</button>
                        <button class="prime-btn" onclick="rotateFace('L\'')">L'</button>
                        <button class="prime-btn" onclick="rotateFace('R\'')">R'</button>
                        <button class="prime-btn" onclick="rotateFace('F\'')">F'</button>
                        <button class="prime-btn" onclick="rotateFace('B\'')">B'</button>
                    </div>
                </div>
                <div class="control-group">
                    <button class="action-btn" onclick="scrambleCube()">🔀 큐브 섞기</button>
                    <button class="action-btn" onclick="resetCube()">🔄 큐브 초기화</button>
                </div>
                <div class="status">
                    <strong>상태:</strong> <span id="status-text">준비 완료</span><br>
                    <strong>조작법:</strong> 마우스 드래그로 회전, 버튼 또는 키보드로 면 회전
                </div>
            </div>
    
            <div id="tutorial-panel">
                <h2>📚 학습 가이드</h2>
                <div class="step">
                    <div class="step-title">1단계: 흰색 십자가</div>
                    <div>아랫면에 흰색 십자가를 만듭니다.</div>
                    <div style="font-size: 12px; margin-top: 5px;">알고리즘: F R U R' U' F'</div>
                </div>
                <div class="step">
                    <div class="step-title">2단계: 흰색 코너</div>
                    <div>흰색 코너를 위치시킵니다.</div>
                    <div style="font-size: 12px; margin-top: 5px;">알고리즘: R U R' U R U2 R'</div>
                </div>
                <div class="step">
                    <div class="step-title">3단계: 중간층 에지</div>
                    <div>중간층 에지를 맞추세요.</div>
                    <div style="font-size: 12px; margin-top: 5px;">알고리즘: U R U' R' U' F' U F</div>
                </div>
                <div class="step">
                    <div class="step-title">4단계: 노란 십자가</div>
                    <div>윗면에 노란색 십자가를 만듭니다.</div>
                    <div style="font-size: 12px; margin-top: 5px;">알고리즘: F R U R' U' F'</div>
                </div>
            </div>
        </div>
    
        <script>
            // Three.js 설정
            let scene, camera, renderer, controls;
            let cubeGroup;
            let isAnimating = false;
            let animationQueue = [];
    
            // 색상 정의
            const COLORS = {
                white: 0xFFFFFF,  // 아래면
                yellow: 0xFFFF00, // 위면
                green: 0x00FF00,  // 앞면
                blue: 0x0000FF,   // 뒷면
                red: 0xFF0000,    // 오른쪽면
                orange: 0xFFA500, // 왼쪽면
                black: 0x000000   // 내부
            };
    
            // 초기화 함수
            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, 7);
    
                // 렌더러 설정
                renderer = new THREE.WebGLRenderer({ antialias: true });
                renderer.setSize(window.innerWidth, window.innerHeight);
                document.getElementById('container').appendChild(renderer.domElement);
    
                // 조명 설정
                const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
                scene.add(ambientLight);
    
                const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
                directionalLight.position.set(5, 10, 7);
                scene.add(directionalLight);
    
                // 컨트롤 설정
                controls = new THREE.OrbitControls(camera, renderer.domElement);
                controls.enableDamping = true;
                controls.dampingFactor = 0.05;
    
                // 큐브 생성
                createRubiksCube();
    
                // 이벤트 리스너
                window.addEventListener('resize', onWindowResize);
                document.addEventListener('keydown', onKeyDown);
    
                // 애니메이션 시작
                animate();
            }
    
            // 큐브 생성 함수
            function createRubiksCube() {
                // 기존 큐브 제거
                if (cubeGroup) {
                    scene.remove(cubeGroup);
                }
    
                cubeGroup = new THREE.Group();
    
                // 3x3x3 큐브 생성
                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) < 3) {
                                const cubie = createCubie(x, y, z);
                                cubie.position.set(x * 1.02, y * 1.02, z * 1.02);
                                cubeGroup.add(cubie);
                            }
                        }
                    }
                }
    
                scene.add(cubeGroup);
                document.getElementById('status-text').textContent = '큐브 초기화 완료';
            }
    
            // 큐비 생성 함수
            function createCubie(x, y, z) {
                const geometry = new THREE.BoxGeometry(0.95, 0.95, 0.95);
    
                // 각 면의 재질 생성
                const materials = [
                    new THREE.MeshPhongMaterial({ color: x === 1 ? COLORS.red : COLORS.black }),    // 오른쪽
                    new THREE.MeshPhongMaterial({ color: x === -1 ? COLORS.orange : COLORS.black }), // 왼쪽
                    new THREE.MeshPhongMaterial({ color: y === 1 ? COLORS.yellow : COLORS.black }), // 위
                    new THREE.MeshPhongMaterial({ color: y === -1 ? COLORS.white : COLORS.black }),  // 아래
                    new THREE.MeshPhongMaterial({ color: z === 1 ? COLORS.green : COLORS.black }),   // 앞
                    new THREE.MeshPhongMaterial({ color: z === -1 ? COLORS.blue : COLORS.black })   // 뒤
                ];
    
                const cubie = new THREE.Mesh(geometry, materials);
                cubie.userData = { x, y, z };
    
                return cubie;
            }
    
            // 면 회전 함수
            function rotateFace(face) {
                if (isAnimating) {
                    animationQueue.push(face);
                    return;
                }
    
                isAnimating = true;
                document.getElementById('status-text').textContent = `회전 중: ${face}`;
    
                const angle = face.includes("'") ? -Math.PI / 2 : Math.PI / 2;
                let axis, selector;
    
                // 회전 축과 선택 조건 설정
                switch (face[0]) {
                    case 'U':
                        axis = new THREE.Vector3(0, 1, 0);
                        selector = c => Math.abs(c.userData.y - 1) < 0.1;
                        break;
                    case 'D':
                        axis = new THREE.Vector3(0, 1, 0);
                        selector = c => Math.abs(c.userData.y + 1) < 0.1;
                        break;
                    case 'L':
                        axis = new THREE.Vector3(1, 0, 0);
                        selector = c => Math.abs(c.userData.x + 1) < 0.1;
                        break;
                    case 'R':
                        axis = new THREE.Vector3(1, 0, 0);
                        selector = c => Math.abs(c.userData.x - 1) < 0.1;
                        break;
                    case 'F':
                        axis = new THREE.Vector3(0, 0, 1);
                        selector = c => Math.abs(c.userData.z - 1) < 0.1;
                        break;
                    case 'B':
                        axis = new THREE.Vector3(0, 0, 1);
                        selector = c => Math.abs(c.userData.z + 1) < 0.1;
                        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 = 300; // 애니메이션 지속 시간 (ms)
                const startTime = Date.now();
    
                function animateRotation() {
                    const elapsed = Date.now() - startTime;
                    progress = Math.min(elapsed / duration, 1);
    
                    // easeOutCubic 애니메이션 효과
                    const easedProgress = 1 - Math.pow(1 - progress, 3);
    
                    rotationGroup.setRotationFromAxisAngle(axis, angle * easedProgress);
    
                    if (progress < 1) {
                        requestAnimationFrame(animateRotation);
                    } else {
                        // 애니메이션 완료 후 처리
                        rotationGroup.children.forEach(cubie => {
                            rotationGroup.remove(cubie);
                            cubeGroup.add(cubie);
    
                            // 위치 업데이트
                            const pos = cubie.position.clone();
                            pos.applyAxisAngle(axis, angle);
                            cubie.position.copy(pos);
    
                            // 사용자 데이터 업데이트
                            const userData = cubie.userData;
                            userData.x = Math.round(pos.x);
                            userData.y = Math.round(pos.y);
                            userData.z = Math.round(pos.z);
                        });
    
                        scene.remove(rotationGroup);
                        isAnimating = false;
                        document.getElementById('status-text').textContent = '준비 완료';
    
                        // 대기 중인 애니메이션 처리
                        if (animationQueue.length > 0) {
                            const nextFace = animationQueue.shift();
                            rotateFace(nextFace);
                        }
                    }
                }
    
                animateRotation();
            }
    
            // 큐브 섞기 함수
            function scrambleCube() {
                const moves = ['U', 'D', 'L', 'R', 'F', 'B', "U'", "D'", "L'", "R'", "F'", "B'"];
                let lastMove = '';
    
                for (let i = 0; i < 20; i++) {
                    let move;
                    do {
                        move = moves[Math.floor(Math.random() * moves.length)];
                    } while (move[0] === lastMove[0]); // 같은 축 연속 회전 방지
    
                    animationQueue.push(move);
                    lastMove = move;
                }
    
                if (!isAnimating) {
                    rotateFace(animationQueue.shift());
                }
    
                document.getElementById('status-text').textContent = '큐브 섞는 중...';
            }
    
            // 큐브 초기화 함수
            function resetCube() {
                animationQueue = [];
                createRubiksCube();
            }
    
            // 키보드 이벤트 처리
            function onKeyDown(event) {
                if (isAnimating) return;
    
                const key = event.key.toUpperCase();
                const shift = event.shiftKey;
    
                switch (key) {
                    case 'U': rotateFace(shift ? "U'" : 'U'); break;
                    case 'D': rotateFace(shift ? "D'" : 'D'); break;
                    case 'L': rotateFace(shift ? "L'" : 'L'); break;
                    case 'R': rotateFace(shift ? "R'" : 'R'); break;
                    case 'F': rotateFace(shift ? "F'" : 'F'); break;
                    case 'B': rotateFace(shift ? "B'" : 'B'); break;
                }
            }
    
            // 창 크기 조절 이벤트 처리
            function onWindowResize() {
                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();
                renderer.setSize(window.innerWidth, window.innerHeight);
            }
    
            // 애니메이션 루프
            function animate() {
                requestAnimationFrame(animate);
                controls.update();
                renderer.render(scene, camera);
            }
    
            // 초기화 및 실행
            init();
        </script>
    </body>
    </html>
    

    🎮 프로그램 기능 설명

    1. 3D 루빅스 큐브

    • 실제 루빅스 큐브와 동일한 3x3x3 구조
    • 각 면에 표준 색상 적용 (흰색, 노란색, 초록색, 파란색, 빨간색, 주황색)
    • 부드러운 회전 애니메이션 효과

    2. 인터랙티브 조작

    • 마우스 조작:
      • 드래그: 큐브 회전
      • 휠: 확대/축소
    • 버튼 조작:
      • U/D/L/R/F/B: 시계 방향 회전
      • U'/D'/L'/R'/F'/B': 반시계 방향 회전
    • 키보드 조작:
      • U/D/L/R/F/B 키: 시계 방향 회전
      • Shift + 키: 반시계 방향 회전

    3. 학습 기능

    • 단계별 가이드 제공 (흰색 십자가 → 코너 → 중간층 → 노란 십자가 → 완성)
    • 각 단계별 기본 알고리즘 표시
    • 큐브 섞기 기능 (20회 랜덤 회전)
    • 큐브 초기화 기능

    4. UI 디자인

    • 왼쪽 패널: 조작 버튼
    • 오른쪽 패널: 학습 가이드
    • 상태 표시: 현재 큐브 상태 표시
    • 반응형 디자인: 다양한 화면 크기에 대응

    🚀 실행 방법

    1. Google Colab에서 새 노트북 생성
    2. 위 코드를 코드 셀에 복사
    3. Shift + Enter로 실행
    4. 나타나는 3D 큐브를 마우스/키보드로 조작

    이 프로그램은 실제 루빅스 큐브를 조작하는 것과 같은 경험을 제공하며, 초보자도 쉽게 학습할 수 있도록 설계되었습니다. 추가로 궁금한 점이 있으면 언제든지 질문해주세요! 😊

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.