Rubik's Cube scramble algorithm
I'm making a very simple Rubik's cube timer and I'm having trouble getting the logic right for the scramble message. I keep getting multiple of the same move in a row and I've just run out of ideas
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class scramble : MonoBehaviour
{
public Text text;
private int size;
//String arrays to hold the notations for each axis
//and the scramble as a whole
private string[] notation;
private string[] x;
private string[] y;
private string[] z;
private string value;
private string note;
void Start()
{
size = 0;
x = new string[] { "R, ", "R', ", "R2, ", "L, ", "L', ", "L2, " };
y = new string[] { "U, ", "U', ", "U2, ", "D, ", "D', ", "D2, " };
z = new string[] { "F, ", "F', ", "F2, ", "B, ", "B', ", "B2, " };
notation = new string[27]; //Size set to 27 because any less and I get a null reference error
setScramble();
text.text = note;
}
//Sets the values of the array from the arrays for each axis
string setScramble()
{
for(int i = 0; i < 25; i++)
{
notation[i] = setX();
if (i == 25)
break;
notation[i + 1] = setY();
if (i == 25)
break;
notation[i + 2] = setZ();
if (i == 25)
break;
i += 2;
}
for(int i = 0; i < 25; i++) //Adds each string to the end of the current string
{
note += notation[i];
}
return note;
}
//Sets value for the X axis
string setX()
{
size++;
if (Random.Range(0, 5) == 0)
value = x[0];
if (Random.Range(0, 5) == 1)
value = x[1];
if (Random.Range(0, 5) == 2)
value = x[2];
if (Random.Range(0, 5) == 3)
value = x[3];
if (Random.Range(0, 5) == 4)
value = x[4];
if (Random.Range(0, 5) == 5)
value = x[5];
return value;
}
//Sets value for the Y axis
string setY()
{
size++;
if (Random.Range(0, 5) == 0)
value = y[0];
if (Random.Range(0, 5) == 1)
value = y[1];
if (Random.Range(0, 5) == 2)
value = y[2];
if (Random.Range(0, 5) == 3)
value = y[3];
if (Random.Range(0, 5) == 4)
value = y[4];
if (Random.Range(0, 5) == 5)
value = y[5];
return value;
}
//Sets value for the Z axis
string setZ()
{
size++;
if (Random.Range(0, 5) == 0)
value = z[0];
if (Random.Range(0, 5) == 1)
value = z[1];
if (Random.Range(0, 5) == 2)
value = z[2];
if (Random.Range(0, 5) == 3)
value = z[3];
if (Random.Range(0, 5) == 4)
value = z[4];
if (Random.Range(0, 5) == 5)
value = z[5];
return value;
}
}
scramble.png
(171.3 kB)
Comment
Your answer
Follow this Question
Related Questions
Text box not displaying a character split string generated at runtime 0 Answers
Making a in editor public writing prompt 0 Answers
How To Load TTF Font From External File 0 Answers
How to link up instantiated text in list to allow buttons to adjust number shown, c# 1 Answer
Removing an item from Inventory and updating item count on HUD 0 Answers