Rubik's Cube Simulation in Java

UM CSCI 152- Java

Assignment Requirements

  • Simulate a Rubik's cube as a 3D array (6 faces, each 3×3).
  • Allow rotation of faces in both clockwise and counterclockwise directions.
  • Accept user input or command-line arguments for moves.
  • Track all moves made onto a stack.
  • Print a sequence to reverse all moves (solve the cube).
  • Provide a visual output of the cube after operations.

Project Overview

This program simulates a 3×3 Rubik's Cube using a 3-dimensional char array in Java. It supports single and multiple face rotations, tracks all moves made during the session, and can output a solution sequence to undo all previous moves.

Key Features

  • Full cube initialization with color coding: white, yellow, blue, green, red, orange.
  • Clockwise and counterclockwise rotation methods for each face.
  • Move history stack to track and solve based on performed moves.
  • Support for live input and automated testing via command-line arguments.
  • Formatted console printing for visualizing cube state.

Sample Code: Rotating a Face


public static void rotateFaceClockwise(char[][] face) {
    char[][] temp = new char[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            temp[j][2 - i] = face[i][j];
        }
    }
    for (int i = 0; i < 3; i++) {
        System.arraycopy(temp[i], 0, face[i], 0, 3);
    }
}
                

Sample Code: Stacking Moves and Solving


ArrayList<String> stack = new ArrayList<>();

public static void solve(ArrayList<String> stack) {
    System.out.print("Solution: ");
    for (int i = stack.size() - 1; i >= 0; i--) {
        System.out.print(stack.get(i) + " ");
    }
    System.out.println();
}
                

Technologies Used

  • Java 17
  • Multidimensional arrays
  • Scanner input handling
  • ArrayList-based move tracking

Reflection

This project challenged my understanding of 3D spatial reasoning and array indexing. It also provided valuable practice handling real-time input and state management in an object-based simulation. Possible improvements include adding scrambling, move optimizations, and full cube solving algorithms.