Last active
February 19, 2026 05:45
-
-
Save dsetzer/a2d967e813eed0a2df7dfb4b465fcd9c to your computer and use it in GitHub Desktop.
Stores the state for a 4x4x4 number cube.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| from collections import defaultdict | |
| class CubeState: | |
| def __init__(self): | |
| self.reset_state() | |
| def reset_state(self): | |
| # Create a default unscrambled state (each face has tiles numbered 1-16) | |
| self.state = [[i for i in range(1, 17)] for _ in range(6)] | |
| def scramble(self): | |
| # Define the different types of pieces | |
| corners = [1, 4, 13, 16] | |
| edges_a = [2, 8, 9, 15] | |
| edges_b = [3, 5, 12, 14] | |
| centers = [6, 7, 10, 11] | |
| # Initialize the scrambled state | |
| scrambled_state = [[] for _ in range(6)] | |
| piece_counts = defaultdict(int) | |
| # Helper function to get valid positions for a piece type | |
| def get_valid_positions(piece_type): | |
| if piece_type in corners: | |
| return corners | |
| elif piece_type in edges_a: | |
| return edges_a | |
| elif piece_type in edges_b: | |
| return edges_b | |
| else: | |
| return centers | |
| # Fill the scrambled state | |
| for face in range(6): | |
| available_positions = set(range(1, 17)) | |
| for position in range(1, 17): | |
| if position in available_positions: | |
| valid_pieces = get_valid_positions(position) | |
| valid_pieces = [p for p in valid_pieces if piece_counts[p] < 6] | |
| if not valid_pieces: | |
| continue # Skip if no valid pieces available | |
| chosen_piece = random.choice(valid_pieces) | |
| scrambled_state[face].append(chosen_piece) | |
| piece_counts[chosen_piece] += 1 | |
| available_positions.remove(position) | |
| # Fill any remaining positions | |
| for face in range(6): | |
| while len(scrambled_state[face]) < 16: | |
| remaining_pieces = [p for p in range(1, 17) if piece_counts[p] < 6] | |
| if not remaining_pieces: | |
| break # No more pieces to add | |
| chosen_piece = random.choice(remaining_pieces) | |
| scrambled_state[face].append(chosen_piece) | |
| piece_counts[chosen_piece] += 1 | |
| self.state = scrambled_state | |
| def get_state(self): | |
| return self.state | |
| def debug_info(self): | |
| # Print the current state | |
| print("Cube State:") | |
| for face in self.state: | |
| print(face) | |
| # Verify piece counts | |
| piece_counts = defaultdict(int) | |
| for face in self.state: | |
| for piece in face: | |
| piece_counts[piece] += 1 | |
| # Print tile placement counts in a compact format | |
| print("\nTile Placement Counts:") | |
| print("|".join(f"{piece}:{count}" for piece, count in sorted(piece_counts.items()))) | |
| # Verify total pieces | |
| total_pieces = sum(len(face) for face in self.state) | |
| print(f"Total pieces: {total_pieces}") | |
| def get_standard_state(self): | |
| # Convert the state to a standard state representation | |
| standard_state = "" | |
| for face in self.state: | |
| for piece in face: | |
| standard_state += str(piece) | |
| return standard_state | |
| # Example usage and testing | |
| # Example usage | |
| cube = CubeState() | |
| print("Initial State:") | |
| cube.debug_info() | |
| print("\nScrambling the cube...") | |
| cube.scramble() | |
| cube.debug_info() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment