Nxnxn Rubik 39scube Algorithm Github Python Full May 2026
This implementation uses the layer-by-layer / reduction method for odd and even cubes.
"""
NxNxN Rubik's Cube Solver in Python
Supports any cube size N (N >= 2)
Uses reduction to 3x3 for N>3
Author: GitHub-Ready Implementation
"""
import math
import random
from copy import deepcopy nxnxn rubik 39scube algorithm github python full
class RubiksCubeNxN:
def init(self, n=3):
"""
Initialize an NxNxN Rubik's Cube.
Colors: U(white), D(yellow), F(green), B(blue), L(orange), R(red)
"""
self.n = n
self.cube = self._create_solved_cube() If you want to read the source code
def _create_solved_cube(self):
"""Create a solved NxNxN cube."""
n = self.n
# Face order: U, D, F, B, L, R
colors = ['W', 'Y', 'G', 'B', 'O', 'R']
cube = {}
for face, color in zip(['U', 'D', 'F', 'B', 'L', 'R'], colors):
cube[face] = [[color for _ in range(n)] for _ in range(n)]
return cube
def rotate_face(self, face, clockwise=True):
"""Rotate a face clockwise or counterclockwise."""
n = self.n
new_face = [[self.cube[face][n-1-j][i] if clockwise else self.cube[face][j][n-1-i]
for j in range(n)] for i in range(n)]
self.cube[face] = new_face
def rotate_layer(self, layer, direction='U', clockwise=True):
"""
Rotate a specific layer (U, D, F, B, L, R, or slice layers like M, E, S for odd cubes)
For NxNxN, layer index from 0 to N-1.
"""
n = self.n
if direction in ['U', 'D', 'F', 'B', 'L', 'R']:
self.rotate_face(direction, clockwise)
return
# Handle slice moves (for reduction)
if direction == 'M': # Middle layer (between L and R)
for i in range(n):
temp = self.cube['F'][i][layer]
self.cube['F'][i][layer] = self.cube['U'][i][layer]
self.cube['U'][i][layer] = self.cube['B'][i][layer]
self.cube['B'][i][layer] = self.cube['D'][i][layer]
self.cube['D'][i][layer] = temp
# Additional slice moves (E, S) can be added similarly
def scramble(self, moves=100):
"""Randomly scramble the cube."""
faces = ['U', 'D', 'F', 'B', 'L', 'R']
for _ in range(moves):
face = random.choice(faces)
clockwise = random.choice([True, False])
self.rotate_face(face, clockwise)
def is_solved(self):
"""Check if the cube is solved."""
n = self.n
for face in self.cube:
first_color = self.cube[face][0][0]
for row in self.cube[face]:
for color in row:
if color != first_color:
return False
return True
def to_string(self):
"""Return a string representation of the cube."""
result = []
n = self.n
# U face
result.append("Upper face:")
for row in self.cube['U']:
result.append(' '.join(row))
# Middle faces layout
for i in range(n):
line = []
for face in ['L', 'F', 'R', 'B']:
line.extend(self.cube[face][i])
result.append(' '.join(line))
# D face
result.append("Down face:")
for row in self.cube['D']:
result.append(' '.join(row))
return '\n'.join(result)
If you want to read the source code or use a library specifically designed for NxNxN, you should look at these repositories. They implement the advanced group theory logic required for larger cubes. moves):
# Parse moves like "U"
class NxNxNCube:
def __init__(self, n):
self.n = n
self.state = self._init_state()
def _init_state(self):
# state[face][row][col] = color index
colors = ['U','D','F','B','L','R']
state = []
for face in range(6):
face_state = [[colors[face]]*self.n for _ in range(self.n)]
state.append(face_state)
return state
def rotate_face(self, face, clockwise=True):
# Rotate one face and its adjacent layers
pass
def apply_moves(self, moves):
# Parse moves like "U", "U'", "U2", "2U", etc.
pass
def is_solved(self):
# Check if each face has uniform color
pass