An open API service indexing awesome lists of open source software.

https://github.com/kanugurajesh/kociemba

Cube solver using kociemba
https://github.com/kanugurajesh/kociemba

fastapi html-css-javascript kociemba python3

Last synced: about 2 months ago
JSON representation

Cube solver using kociemba

Awesome Lists containing this project

README

          

# Rubik's Cube 3D Visualizer & Solver

A modern, interactive 3D Rubik's Cube web application featuring real-time manipulation, elegant animations, and integrated solving capabilities. Built with Three.js for frontend visualization and FastAPI backend for cube logic and solving algorithms.

## ✨ Features

### 🎮 Interactive 3D Experience
- **Immersive 3D Visualization**: Fully interactive 3D cube rendered with Three.js
- **Smooth Animations**: Fluid face rotations and transitions with customizable timing
- **Intuitive Controls**: Keyboard-based manipulation using standard Rubik's cube notation
- **Modern UI**: Clean, dark-themed interface with animated backgrounds and tooltips

### 🧩 Cube Manipulation
- **Standard Notation Support**: Full support for F, R, U, L, D, B face rotations
- **Direction Control**: Toggle between clockwise and counterclockwise rotations
- **Middle Layer Moves**: M-slice rotations for advanced solving techniques
- **Move History**: Undo functionality to reverse any move sequence

### 🔧 Solving Capabilities
- **Kociemba Algorithm**: Optimal solving using the two-phase Kociemba algorithm
- **Real-time Solutions**: Get step-by-step solutions for any valid cube state
- **Solution Display**: Visual presentation of move sequences with progress tracking
- **Auto-solve Mode**: Watch the cube solve itself with animated move execution

### 🎨 Enhanced User Interface
- **Responsive Design**: Optimized for both desktop and mobile devices
- **Visual Feedback**: Real-time action display and move notifications
- **Status Indicators**: Clear feedback for solving progress and cube state
- **Accessibility**: Keyboard shortcuts and ARIA-compliant interface elements

## 🚀 Quick Start

### Prerequisites
- **Frontend**: Modern web browser with WebGL support
- **Backend**: Python 3.7+ with pip

### Installation

1. **Clone the repository**
```bash
git clone https://github.com/kanugurajesh/kociemba
cd kociemba
```

2. **Set up the backend**
```bash
cd backend
pip install -r requirements.txt
python server.py
```

3. **Launch the frontend**
- Open `index.html` in your web browser
- Or serve using a local HTTP server:
```bash
python -m http.server 5500
```

## 🎯 How to Use

### Basic Controls

| Key | Action | Description |
|-----|--------|-------------|
| `F` | Front face | Rotate front face |
| `R` | Right face | Rotate right face |
| `U` | Up face | Rotate top face |
| `L` | Left face | Rotate left face |
| `D` | Down face | Rotate bottom face |
| `B` | Back face | Rotate back face |
| `M` | Middle slice | Rotate middle vertical slice |

### Direction Control
- `1` - Clockwise rotation (default)
- `2` - Counterclockwise rotation (adds ' to notation)

### Interface Actions
- **Solve Button**: Generate and display optimal solution
- **Undo Button**: Reverse the last move
- **Reset Button**: Return cube to solved state
- **Direction Toggle**: Switch between clockwise/counterclockwise modes

### Advanced Features
- **Auto-solve**: Click solve to see step-by-step solution execution
- **Move History**: Full undo/redo functionality for all moves
- **State Persistence**: Cube state maintained across browser sessions

## 🛠 Technical Architecture

### Frontend (Three.js)
```
modules/
├── main.js # Application entry point and update loop
├── sceneManager.js # 3D scene setup and rendering
├── rubik.js # Cube geometry and face rotations
├── animations.js # Animation timing and interpolation
├── keyHandler.js # Keyboard input processing
├── ui.js # User interface updates
├── api.js # Backend communication
├── modes.js # Application modes and states
├── motion.js # Movement mechanics
├── action_utils.js # User action processing
└── solutionService.js # Solution execution and display
```

### Backend (FastAPI + Python)
```
backend/
├── server.py # FastAPI application and CORS setup
├── cube_engine.py # Core cube logic and Kociemba integration
└── requirements.txt # Python dependencies
```

## 📡 API Reference

### Endpoints

#### `POST /move`
Execute a cube move and update internal state.

**Request:**
```json
{
"move": "R"
}
```

**Response:**
```json
{
"move": "R",
"cube_string": "UUUUUULLLURRURRURRFFFFFFFFFRRRDDDDDDLLDLLDLLDBBBBBBBBB"
}
```

#### `GET /solve`
Get optimal solution for current cube state.

**Response:**
```json
{
"solutionString": "R U R' U' R' F R2 U' R' U' R U R' F'",
"parsedMoves": ["R", "U", "R'", "U'", "R'", "F", ["R", "R"], "U'", "R'", "U'", "R", "U", "R'", "F'"]
}
```

#### `POST /reset-cube`
Reset cube to solved state.

**Response:**
```json
{}
```

## 🎨 Customization

### Cube Colors
Modify colors in `rubik.js`:
```javascript
let blueMaterial = new THREE.MeshBasicMaterial({ color: 0x003DA5 });
let greenMaterial = new THREE.MeshBasicMaterial({ color: 0x009A44 });
// ... additional materials
```

### Animation Timing
Adjust animation speed in `animations.js`:
```javascript
const ANIMATION_DURATION = 300; // milliseconds
```

### UI Theme
Customize visual theme in the CSS variables within `index.html`:
```css
:root {
--bg-primary: #0a0a0f;
--accent-primary: #00d4ff;
--accent-secondary: #7c3aed;
/* ... additional theme variables */
}
```

## 🔧 Development

### Adding New Moves
1. **Backend**: Add move logic to `cube_engine.py` in the `Polyhedron` class
2. **Frontend**: Update the move dispatcher in `keyHandler.js`
3. **Animation**: Define rotation parameters in `animations.js`

### Custom Algorithms
Extend the solution service in `solutionService.js` to support custom solving algorithms or move sequences.

### Performance Optimization
- Enable/disable animations based on device capabilities
- Implement level-of-detail for complex cube states
- Cache frequently used geometries and materials

## 🚀 Deployment

### Production Configuration
1. **Backend**: Set environment variables for production
```bash
export ENV=production
export PORT=8080
```

2. **Frontend**: Update API endpoints in `api.js` for production URLs

3. **CORS**: Configure allowed origins in `server.py`

### Docker Support
```dockerfile
# Example Dockerfile for backend
FROM python:3.9-slim
COPY backend/ /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "server.py"]
```

## 🤝 Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

### Code Style
- **JavaScript**: Use ES6+ modules and modern syntax
- **Python**: Follow PEP 8 conventions
- **Comments**: Document complex algorithms and cube transformations

## 📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

## 🙏 Acknowledgments

- **Three.js** - 3D graphics library powering the visualization
- **Kociemba Algorithm** - Optimal cube solving implementation
- **FastAPI** - Modern Python web framework for the backend
- **Bootstrap** - UI components and responsive design

## 📊 Technical Details

### Cube State Representation
- **Internal Format**: 6×3×3 NumPy array representing cube faces
- **Kociemba String**: 54-character string for solver integration
- **Face Order**: U (Up), R (Right), F (Front), D (Down), L (Left), B (Back)

### Performance Metrics
- **Solving Speed**: Average 20 moves for optimal solutions
- **Animation FPS**: 60fps smooth animations on modern browsers
- **Load Time**: < 2 seconds initial load with Three.js

### Browser Compatibility
- **Chrome**: 90+ ✅
- **Firefox**: 88+ ✅
- **Safari**: 14+ ✅
- **Edge**: 90+ ✅

## 🐛 Known Issues

- Mobile touch controls not yet implemented
- Large solution sequences may cause UI lag
- Backend requires restart after solving complex states (rare)

## 🔮 Future Enhancements

- [ ] Touch and swipe controls for mobile devices
- [ ] Multiple solving algorithms (CFOP, Roux, ZZ)
- [ ] Scramble generation with difficulty levels
- [ ] Multiplayer solving competitions
- [ ] 3D cube customization (textures, effects)
- [ ] Voice commands for hands-free operation
- [ ] AR/VR support for immersive experience