# Rubix Cube Randomization

I’ve created this Rubix Cube Test Sketch, and it works totally fine for first. When I started creating a Randomizer Function to randomize all the little Tiles on each side, it went kind of buggy, and I can’t figure out why it is… it’d be really nice to get some help, and I’m really thankfull on getting help and learning. When you look at the Code, you’ll notice that I made this function “randomize” in the class “cube”, and that’s where the problem is. It always changed up the tiles, but then they kinda all go black except of a few?!

``````Cube cube;

void setup() {
size(600, 450);
cube = new Cube();
cube.randomize();
}

void draw() {
background(0);
stroke(0);
for (int i = 0; i < cube.sides.length; i++) {
for (int j = 0; j < cube.sides[i].tiles.length; j++) {
color temp = check(cube.sides[i].tiles[j].col);
fill(temp);
rect(cube.sides[i].tiles[j].self.x*50, cube.sides[i].tiles[j].self.y*50, 50, 50);
}
}
}

color check(String c) {
color col = color(0);

switch (c) {
case "orange":
col = color(244, 182, 66);
break;
case "white":
col = color(255);
break;
case "green":
col = color(0, 255, 0);
break;
case "yellow":
col = color(251, 255, 63);
break;
case "red":
col = color(255, 0, 0);
break;
case "blue":
col = color(0, 0, 255);
break;
}

return col;
}

class Cube {
Side[] sides = new Side[6];

Cube() {
for (int i = 0; i < 6; i++) {
PVector input = new PVector(0, 0);
String temp = "";
switch (i) {
case 0:
temp = "orange";
input = new PVector(0, 3);
break;
case 1:
temp = "white";
input = new PVector(3, 0);
break;
case 2:
temp = "green";
input = new PVector(3, 3);
break;
case 3:
temp = "yellow";
input = new PVector(3, 6);
break;
case 4:
temp = "red";
input = new PVector(6, 3);
break;
case 5:
temp = "blue";
input = new PVector(9, 3);
break;
}

sides[i] = new Side(temp, input);
}
}

void randomize() {
int numberOfTiles = sides.length * sides[0].tiles.length;
for (int i = 0; i < numberOfTiles; i++) {
int s1 = round(random(0, 5));
int s2 = round(random(0, 5));
if (s2 == s1) {
while (s2 == s1) {
s2 = round(random(0, 5));
}
}

int t1 = round(random(0, 8));
int t2 = round(random(0, 8));
if (t2 == t1) {
while (t2 == t1) {
t2 = round(random(0, 8));
}
}

println(s1, t1, ":", s2, t2);

String tempCol = sides[s1].tiles[t1].col;
PVector tempS = new PVector(sides[s1].tiles[t1].self.x,sides[s1].tiles[t1].self.y);
sides[s2].tiles[t2].self = tempS;
sides[s2].tiles[t2].col = tempCol;
}
}
}

class Side {
Tile[] tiles = new Tile[9];
PVector self;

Side(String col, PVector input) {
self = input;
int y = 0;
for (int i = 0; i < tiles.length; i++) {
PVector s = new PVector(input.x+(i%3), input.y+y);
tiles[i] = new Tile(col, s);
if (i%3 == 2) {
y++;
}
}
}
}

class Tile {
String col;
PVector self;

Tile(String c, PVector s) {
col = c;
self = new PVector(0,0);
self = s;
}
}
``````
1 Like

Your problem is complicated! Break it down into smaller steps and make sure each one works on its own. Normally you would randomize a cube by making ~20 random turns. But that’s a lot! Can you just make one turn? Hint: What would a completed cube that has had a single turn made look like?

You might then realize that the way you are randomizing your cube is, well, wrong. You don’t want to shuffle the tiles around! That’s like pulling all the stickers off and rearranging them! … And that might leave you with a cube you can’t even solve!

Don’t think about it in terms of moving tiles. Thing of it in terms of rotating sides! Figure out a way to rotate one face. Which tiles go where? Then another. Then the other 4. How about making them rotate 180 degrees instead of 90? How about -90? How can your user pick a face to rotate?

Once you have face rotations working, randomizing the cube is easy…

2 Likes

Oh, yes, that was kind of a problem, really… But I still don’t understand what the randomizer failure was just there. Anyways, I’m thankfull you helped me, again. Hope you have a great day!

So I changed a lot of things up now, and with the “working” randomizer, I went to ‘https://ruwix.com/online-rubiks-cube-solver-program/’ to check if it works, and if it was solvable. But the Website Solver told me it was not, and I had to “add every edge once” or something. Here is the current code:

``````Cube cube;

void setup() {
size(600, 450);
cube = new Cube();
cube.randomize();
}

void draw() {
background(0);
stroke(0);
for (int i = 0; i < cube.sides.length; i++) {
for (int j = 0; j < cube.sides[i].tiles.length; j++) {
color temp = check(cube.sides[i].tiles[j].col);
fill(temp);
rect(cube.sides[i].tiles[j].self.x*50, cube.sides[i].tiles[j].self.y*50, 50, 50);
}
}
}

color check(String c) {
color col = color(0);

switch (c) {
case "orange":
col = color(244, 182, 66);
break;
case "white":
col = color(255);
break;
case "green":
col = color(0, 255, 0);
break;
case "yellow":
col = color(251, 255, 63);
break;
case "red":
col = color(255, 0, 0);
break;
case "blue":
col = color(0, 0, 255);
break;
}

return col;
}

void keyPressed() {
if (key == 'q') {
cube.turnMiddleRight(0,3);
} else if (key == 'w') {
cube.turnMiddleRight(3,6);
} else if (key == 'e') {
cube.turnMiddleRight(6,9);
} else if (key == 'a') {
cube.turnMiddleLeft(0,3);
} else if (key == 's') {
cube.turnMiddleLeft(3,6);
} else if (key == 'd') {
cube.turnMiddleLeft(6,9);
} else if (key == 't') {
cube.turnTopDown(0,7);
} else if (key == 'z') {
cube.turnTopDown(1,8);
} else if (key == 'u') {
cube.turnTopDown(2,9);
} else if (key == 'g') {
cube.turnTopUp(0,7);
} else if (key == 'h') {
cube.turnTopUp(1,8);
} else if (key == 'j') {
cube.turnTopUp(2,9);
}
}

class Cube {
Side[] sides = new Side[6];

Cube() {
for (int i = 0; i < 6; i++) {
PVector input = new PVector(0, 0);
String temp = "";
switch (i) {
case 0:
temp = "green";
input = new PVector(0, 3);
break;
case 1:
temp = "yellow";
input = new PVector(3, 0);
break;
case 2:
temp = "orange";
input = new PVector(3, 3);
break;
case 3:
temp = "white";
input = new PVector(3, 6);
break;
case 4:
temp = "blue";
input = new PVector(6, 3);
break;
case 5:
temp = "red";
input = new PVector(9, 3);
break;
}

sides[i] = new Side(temp, input);
}
}

void turnMiddleLeft(int start, int end) {
for (int i = start; i < end; i++) {
String tempCol1 = sides[5].tiles[i].col;
sides[5].tiles[i].col = sides[0].tiles[i].col;
String tempCol2 = sides[4].tiles[i].col;
sides[4].tiles[i].col = tempCol1;
tempCol1 = sides[2].tiles[i].col;
sides[2].tiles[i].col = tempCol2;
sides[0].tiles[i].col = tempCol1;
}
}

void turnMiddleRight(int start, int end) {
for (int i = start; i < end; i++) {
String tempCol1 = sides[0].tiles[i].col;
sides[0].tiles[i].col = sides[5].tiles[i].col;
String tempCol2 = sides[2].tiles[i].col;
sides[2].tiles[i].col = tempCol1;
tempCol1 = sides[4].tiles[i].col;
sides[4].tiles[i].col = tempCol2;
sides[5].tiles[i].col = tempCol1;
}
}

void turnTopUp(int start, int end) {
for (int i = start; i < end; i+=3) {
String tempCol1 = sides[5].tiles[i].col;
sides[5].tiles[i].col = sides[1].tiles[i].col;
String tempCol2 = sides[3].tiles[i].col;
sides[3].tiles[i].col = tempCol1;
tempCol1 = sides[2].tiles[i].col;
sides[2].tiles[i].col = tempCol2;
sides[1].tiles[i].col = tempCol1;
}
}

void turnTopDown(int start, int end) {
for (int i = start; i < end; i+=3) {
String tempCol1 = sides[1].tiles[i].col;
sides[1].tiles[i].col = sides[5].tiles[i].col;
String tempCol2 = sides[2].tiles[i].col;
sides[2].tiles[i].col = tempCol1;
tempCol1 = sides[3].tiles[i].col;
sides[3].tiles[i].col = tempCol2;
sides[5].tiles[i].col = tempCol1;
}
}

void randomize() {
//turnMiddleRight(0, 3); // Turns Middle Top Row to the Right
//turnMiddleLeft(0,3);   // Turns Middle Top Row to the Left
//turnMiddleRight(3, 6); // Turns Middle Middle Row to the Right
//turnMiddleLeft(3,6);   // Turns Middle Middle Row to the Left
//turnMiddleRight(6, 9); // Turns Middle Bottom Row to the Right
//turnMiddleLeft(6,9);   // Turns Middle Bottom Row to the Left
//turnTopDown(0, 7);     // Turns Top Left Row Downwards
//turnTopUp(0, 7);       // Turns Top Left Row Upwards
//turnTopDown(1, 8);     // Turns Top Middle Row Downwards
//turnTopUp(1, 8);       // Turns Top Middle Row Upwards
//turnTopDown(2, 9);     // Turns Top Right Row Downwards
//turnTopUp(2, 9);       // Turns Top Right Row Upwards

int times = round(random(20,40));
for (int i = 0; i < times; i++) {
int dec = round(random(1,2));
if (dec == 1) {
dec = round(random(1,2));
if (dec == 1) {
//Left
dec = round(random(0,2));
int c = 0;
switch (dec) {
case 1:
c = 3;
break;
case 2:
c = 6;
break;
}
turnMiddleLeft(c,c+3);
} else {
//Right
dec = round(random(0,2));
int c = 0;
switch (dec) {
case 1:
c = 3;
break;
case 2:
c = 6;
break;
}
turnMiddleRight(c,c+3);
}
} else {
dec = round(random(1,2));
if (dec == 1) {
dec = round(random(0,2));
turnTopDown(dec,dec+7);
} else {
dec = round(random(0,2));
turnTopUp(dec,dec+7);
}
}
}
}
}

class Side {
Tile[] tiles = new Tile[9];
PVector self;

Side(String col, PVector input) {
self = input;
int y = 0;
for (int i = 0; i < tiles.length; i++) {
PVector s = new PVector(input.x+(i%3), input.y+y);
tiles[i] = new Tile(col, s);
if (i%3 == 2) {
y++;
}
}
}
}

class Tile {
String col;
PVector self;

Tile(String c, PVector s) {
col = c;
self = new PVector(0,0);
self = s;
}
}
``````

“Every edge must be added once.” is the error I go when I input a cube that didn’t have one of each edge pieces. That is, there are 12 cubes in a cube that have two sides colored - these are the 12 edge pieces. The two colored sides on them are always different, and there are four sides of each color on them in total.

It’s possible that your cube got scrambled wrong. Or - more likely - it’s possibly you entered the scrambled cube incorrectly into the checker.

Show up a picture of the scrambled cube you generated and we’ll see if we can point out the problem.

So, I have changed a little code because I found a little failure, but it still gives me the same Failure. I’m also adding two Pictures from my Cube, and two from the Website, so that you could see the scarmbled and standard Version. (tho, since I’m a “new User” I can only post 1 Image and only 2 Links per Post, I’m going to put them on my Website for you, on http://baipyrus.bplaced.net/images/)

``````Cube cube;

void setup() {
size(600, 450);
cube = new Cube();
//cube.randomize();
}

void draw() {
background(0);
stroke(0);
for (int i = 0; i < cube.sides.length; i++) {
for (int j = 0; j < cube.sides[i].tiles.length; j++) {
color temp = check(cube.sides[i].tiles[j].col);
fill(temp);
rect(cube.sides[i].tiles[j].self.x*50, cube.sides[i].tiles[j].self.y*50, 50, 50);
}
}
}

color check(String c) {
color col = color(0);

switch (c) {
case "orange":
col = color(244, 182, 66);
break;
case "white":
col = color(255);
break;
case "green":
col = color(0, 255, 0);
break;
case "yellow":
col = color(251, 255, 63);
break;
case "red":
col = color(255, 0, 0);
break;
case "blue":
col = color(0, 0, 255);
break;
}

return col;
}

void keyPressed() {
if (key == 'q') {
cube.turnMiddleRight(0,3);
} else if (key == 'w') {
cube.turnMiddleRight(3,6);
} else if (key == 'e') {
cube.turnMiddleRight(6,9);
} else if (key == 'a') {
cube.turnMiddleLeft(0,3);
} else if (key == 's') {
cube.turnMiddleLeft(3,6);
} else if (key == 'd') {
cube.turnMiddleLeft(6,9);
} else if (key == 't') {
cube.turnTopDown(0,7);
} else if (key == 'z') {
cube.turnTopDown(1,8);
} else if (key == 'u') {
cube.turnTopDown(2,9);
} else if (key == 'g') {
cube.turnTopUp(0,7);
} else if (key == 'h') {
cube.turnTopUp(1,8);
} else if (key == 'j') {
cube.turnTopUp(2,9);
}
}

class Cube {
Side[] sides = new Side[6];

Cube() {
for (int i = 0; i < 6; i++) {
PVector input = new PVector(0, 0);
String temp = "";
switch (i) {
case 0:
temp = "green";
input = new PVector(0, 3);
break;
case 1:
temp = "yellow";
input = new PVector(3, 0);
break;
case 2:
temp = "orange";
input = new PVector(3, 3);
break;
case 3:
temp = "white";
input = new PVector(3, 6);
break;
case 4:
temp = "blue";
input = new PVector(6, 3);
break;
case 5:
temp = "red";
input = new PVector(9, 3);
break;
}

sides[i] = new Side(temp, input);
}
}

void turnMiddleLeft(int start, int end) {
for (int i = start; i < end; i++) {
String tempCol1 = sides[5].tiles[i].col;
sides[5].tiles[i].col = sides[0].tiles[i].col;
String tempCol2 = sides[4].tiles[i].col;
sides[4].tiles[i].col = tempCol1;
tempCol1 = sides[2].tiles[i].col;
sides[2].tiles[i].col = tempCol2;
sides[0].tiles[i].col = tempCol1;
}

if (start == 0) {
//Turn Upper Part
String[] tiles = new String[9];
for (int i = 0; i < tiles.length; i++) {
tiles[i] = sides[1].tiles[i].col;
}

sides[1].tiles[2].col = tiles[0];
sides[1].tiles[5].col = tiles[1];
sides[1].tiles[8].col = tiles[2];
sides[1].tiles[1].col = tiles[3];
sides[1].tiles[7].col = tiles[5];
sides[1].tiles[0].col = tiles[6];
sides[1].tiles[3].col = tiles[7];
sides[1].tiles[6].col = tiles[8];
} else if (start == 6) {
//Turn Lower Part
String[] tiles = new String[9];
for (int i = 0; i < tiles.length; i++) {
tiles[i] = sides[3].tiles[i].col;
}

sides[3].tiles[6].col = tiles[0];
sides[3].tiles[3].col = tiles[1];
sides[3].tiles[0].col = tiles[2];
sides[3].tiles[7].col = tiles[3];
sides[3].tiles[8].col = tiles[6];
sides[3].tiles[5].col = tiles[7];
sides[3].tiles[2].col = tiles[8];
sides[3].tiles[1].col = tiles[5];
}
}

void turnMiddleRight(int start, int end) {
for (int i = start; i < end; i++) {
String tempCol1 = sides[0].tiles[i].col;
sides[0].tiles[i].col = sides[5].tiles[i].col;
String tempCol2 = sides[2].tiles[i].col;
sides[2].tiles[i].col = tempCol1;
tempCol1 = sides[4].tiles[i].col;
sides[4].tiles[i].col = tempCol2;
sides[5].tiles[i].col = tempCol1;
}

if (start == 0) {
//Turn Upper Part
String[] tiles = new String[9];
for (int i = 0; i < tiles.length; i++) {
tiles[i] = sides[1].tiles[i].col;
}

sides[1].tiles[6].col = tiles[0];
sides[1].tiles[3].col = tiles[1];
sides[1].tiles[0].col = tiles[2];
sides[1].tiles[7].col = tiles[3];
sides[1].tiles[8].col = tiles[6];
sides[1].tiles[5].col = tiles[7];
sides[1].tiles[2].col = tiles[8];
sides[1].tiles[1].col = tiles[5];
} else if (start == 6) {
//Turn Lower Part
String[] tiles = new String[9];
for (int i = 0; i < tiles.length; i++) {
tiles[i] = sides[3].tiles[i].col;
}

sides[3].tiles[2].col = tiles[0];
sides[3].tiles[5].col = tiles[1];
sides[3].tiles[8].col = tiles[2];
sides[3].tiles[1].col = tiles[3];
sides[3].tiles[7].col = tiles[5];
sides[3].tiles[0].col = tiles[6];
sides[3].tiles[3].col = tiles[7];
sides[3].tiles[6].col = tiles[8];
}
}

void turnTopUp(int start, int end) {
for (int i = start; i < end; i+=3) {
String tempCol1 = "";
if (start == 0) {
tempCol1 = sides[5].tiles[i+2].col;
sides[5].tiles[i+2].col = sides[1].tiles[i].col;
} else if (start == 2) {
tempCol1 = sides[5].tiles[i-2].col;
sides[5].tiles[i-2].col = sides[1].tiles[i].col;
} else {
tempCol1 = sides[5].tiles[i].col;
sides[5].tiles[i].col = sides[1].tiles[i].col;
}
String tempCol2 = sides[3].tiles[i].col;
sides[3].tiles[i].col = tempCol1;
tempCol1 = sides[2].tiles[i].col;
sides[2].tiles[i].col = tempCol2;
sides[1].tiles[i].col = tempCol1;
}

if (start == 0) {
//Turn Left Part
String[] tiles = new String[9];
for (int i = 0; i < tiles.length; i++) {
tiles[i] = sides[0].tiles[i].col;
}

sides[0].tiles[6].col = tiles[0];
sides[0].tiles[3].col = tiles[1];
sides[0].tiles[0].col = tiles[2];
sides[0].tiles[7].col = tiles[3];
sides[0].tiles[1].col = tiles[5];
sides[0].tiles[8].col = tiles[6];
sides[0].tiles[5].col = tiles[7];
sides[0].tiles[2].col = tiles[8];
} else if (start == 2) {
//Turn Right Part
String[] tiles = new String[9];
for (int i = 0; i < tiles.length; i++) {
tiles[i] = sides[4].tiles[i].col;
}

sides[4].tiles[2].col = tiles[0];
sides[4].tiles[5].col = tiles[1];
sides[4].tiles[8].col = tiles[2];
sides[4].tiles[1].col = tiles[3];
sides[4].tiles[7].col = tiles[5];
sides[4].tiles[0].col = tiles[6];
sides[4].tiles[3].col = tiles[7];
sides[4].tiles[6].col = tiles[8];
}
}

void turnTopDown(int start, int end) {
for (int i = start; i < end; i+=3) {
String tempCol1 = sides[1].tiles[i].col;
if (start == 0) {
sides[1].tiles[i].col = sides[5].tiles[i+2].col;
} else if (start == 2) {
sides[1].tiles[i].col = sides[5].tiles[i-2].col;
} else {
sides[1].tiles[i].col = sides[5].tiles[i].col;
}
String tempCol2 = sides[2].tiles[i].col;
sides[2].tiles[i].col = tempCol1;
tempCol1 = sides[3].tiles[i].col;
sides[3].tiles[i].col = tempCol2;
if (start == 0) {
sides[5].tiles[i+2].col = tempCol1;
} else if (start == 2) {
sides[5].tiles[i-2].col = tempCol1;
} else {
sides[5].tiles[i].col = tempCol1;
}
}

if (start == 0) {
//Turn Left Part
String[] tiles = new String[9];
for (int i = 0; i < tiles.length; i++) {
tiles[i] = sides[0].tiles[i].col;
}

sides[0].tiles[2].col = tiles[0];
sides[0].tiles[5].col = tiles[1];
sides[0].tiles[8].col = tiles[2];
sides[0].tiles[1].col = tiles[3];
sides[0].tiles[7].col = tiles[5];
sides[0].tiles[0].col = tiles[6];
sides[0].tiles[3].col = tiles[7];
sides[0].tiles[6].col = tiles[8];
} else if (start == 2) {
//Turn Right Part
String[] tiles = new String[9];
for (int i = 0; i < tiles.length; i++) {
tiles[i] = sides[4].tiles[i].col;
}

sides[4].tiles[6].col = tiles[0];
sides[4].tiles[3].col = tiles[1];
sides[4].tiles[0].col = tiles[2];
sides[4].tiles[7].col = tiles[3];
sides[4].tiles[1].col = tiles[5];
sides[4].tiles[8].col = tiles[6];
sides[4].tiles[5].col = tiles[7];
sides[4].tiles[2].col = tiles[8];
}
}

void L() {
turnTopDown(0, 7);
}

void L1() {
turnTopUp(0, 7);
}

void U() {
turnMiddleLeft(0, 3);
}

void U1() {
turnMiddleRight(0, 3);
}

void D() {
turnMiddleRight(6, 9);
}

void D1() {
turnMiddleLeft(6, 9);
}

void R() {
turnTopDown(2, 9);
}

void R1() {
turnTopUp(2, 9);
}

void randomize() {
//turnMiddleRight(0, 3); // Turns Middle Top Row to the Right
//turnMiddleRight(3, 6); // Turns Middle Middle Row to the Right
//turnMiddleLeft(3,6);   // Turns Middle Middle Row to the Left
//turnMiddleLeft(6,9);   // Turns Middle Bottom Row to the Left
//turnTopUp(0, 7);       // Turns Top Left Row Upwards
//turnTopDown(1, 8);     // Turns Top Middle Row Downwards
//turnTopUp(1, 8);       // Turns Top Middle Row Upwards
//turnTopUp(2, 9);       // Turns Top Right Row Upwards

int times = round(random(20, 40));
for (int i = 0; i < times; i++) {
int dec = round(random(1, 2));
if (dec == 1) {
dec = round(random(1, 2));
if (dec == 1) {
//Left
dec = round(random(0, 2));
int c = 0;
switch (dec) {
case 1:
c = 3;
break;
case 2:
c = 6;
break;
}
turnMiddleLeft(c, c+3);
} else {
//Right
dec = round(random(0, 2));
int c = 0;
switch (dec) {
case 1:
c = 3;
break;
case 2:
c = 6;
break;
}
turnMiddleRight(c, c+3);
}
} else {
dec = round(random(1, 2));
if (dec == 1) {
dec = round(random(0, 2));
turnTopDown(dec, dec+7);
} else {
dec = round(random(0, 2));
turnTopUp(dec, dec+7);
}
}
}
}
}

class Side {
Tile[] tiles = new Tile[9];
PVector self;

Side(String col, PVector input) {
self = input;
int y = 0;
for (int i = 0; i < tiles.length; i++) {
PVector s = new PVector(input.x+(i%3), input.y+y);
tiles[i] = new Tile(col, s);
if (i%3 == 2) {
y++;
}
}
}
}

class Tile {
String col;
PVector self;

Tile(String c, PVector s) {
col = c;
self = new PVector(0,0);
self = s;
}
}
``````

I see a problem with the scrambled cube:
On the far left side in the middle is a white tile.
On the far right side in the middle is a yellow tile.
But there is no edge piece that pairs a white tile with a yellow tile!
The white face and the yellow face are not adjacent!

Bonus problem: Your cube seems to have at least two green-red edges. Which edges are missing?

Some rotation must not be working right.
There are only really 6 operations you can do to a cube, often denoted by: L, R, U, D, F, and B.
That’s what the buttons down below are in the scrambler.
Does you cube change in the same way when you do each of these operations?
You need to do some debugging.

Thanks for these lots of help you gave me, I, again, fixed a lot of things so now I can press every button once (L,L’,R,R’,U,U’,D,D’,F,F’,B,B’) and the Cube is solvable! Though, somehow, when a lot of keys are getting pressed after one another, it gets confused again it is not solvable anymore … Got a lot of things done today, and i’m really thankfull you could help me!