C
C#2y ago
Nuc

❔ Key Shuffle Logic Error

x
6 Replies
Nuc
NucOP2y ago
Ok so I have a project where there are 8 keys that shuffle through different movements. All the keys are in the array "keys" The keys in the positions (1-8) are in an array called "keySlots" Movements "Shuffle_Up" and "Shuffle_Down" work perfectly fine. However, if it does a "Diagonal_Top", then one of the shuffles, it will completely break the keySlots table with the slots being completely incorrect. Code shown below:
public class Movement {
public string movementName { get; set; }
public List<int> keyTable { get; set; }
}

public class GameManager : MonoBehaviour {
List<Movement> movements = new List<Movement>()
{
new Movement()
{
movementName = "Shuffle_Up",
keyTable = new List<int>()
{
2,
4,
1,
6,
3,
8,
5,
7
}
},

/*new Movement()
{
movementName = "Shuffle_Down",
keyTable = new List<int>()
{
3,
1,
5,
2,
7,
4,
8,
6,
}
},*/

new Movement()
{
movementName = "Diagonal_Top",
keyTable = new List<int>()
{
8,
7,
3,
4,
5,
6,
2,
1,
}
}
};


public GameObject keySet;

public int shuffleCount;
public float shuffleTime;
public float shuffleSpeed;

List<int> keySlots = new List<int>()
{
0,
1,
2,
3,
4,
5,
6,
7
};

List<GameObject> keys = new List<GameObject>();

Color32 normalColour = new Color32(255, 255, 255, 255);
Color32 indicateColour = new Color32(0, 255, 0, 255);

void Start() {
for (int i = 0; i < keySet.transform.childCount; i++)
{
keys.Add(keySet.transform.GetChild(i).gameObject);
}

StartCoroutine(GameStart());
}

IEnumerator GameStart() {
yield return new WaitForSecondsRealtime(1f);

GameObject key = keys[Random.Range(0, keys.Count - 1)];

StartCoroutine(Lerp("colour", key, .5f, normalColour, indicateColour));

yield return new WaitForSecondsRealtime(1f);

StartCoroutine(Lerp("colour", key, .5f, indicateColour, normalColour));

yield return new WaitForSecondsRealtime(1f);

for (int i = 0; i < shuffleCount; i++)
{
Movement selectedMovement;
//Movement selectedMovement = movements[Random.Range(0, movements.Count)];
if (i % 2 == 0)
{
selectedMovement = movements[1];
}
else
{
selectedMovement = movements[0];
}

print(selectedMovement.movementName);

for (int c = 0; c < keys.Count; c++)
{
int nextPos = selectedMovement.keyTable[c] - 1;

//StartCoroutine(Lerp("movement", keys[keySlots[c]], shuffleSpeed, keys[keySlots[c]].transform.position, keys[keySlots[nextPos]].transform.position));
}

for (int c = 0; c < keySlots.Count; c++)
{
int newKey = selectedMovement.keyTable.IndexOf(keySlots[c] + 1);

keySlots[c] = newKey;

print(i + " " + (newKey + 1));
}

yield return new WaitForSecondsRealtime(shuffleTime);
}
}
public class Movement {
public string movementName { get; set; }
public List<int> keyTable { get; set; }
}

public class GameManager : MonoBehaviour {
List<Movement> movements = new List<Movement>()
{
new Movement()
{
movementName = "Shuffle_Up",
keyTable = new List<int>()
{
2,
4,
1,
6,
3,
8,
5,
7
}
},

/*new Movement()
{
movementName = "Shuffle_Down",
keyTable = new List<int>()
{
3,
1,
5,
2,
7,
4,
8,
6,
}
},*/

new Movement()
{
movementName = "Diagonal_Top",
keyTable = new List<int>()
{
8,
7,
3,
4,
5,
6,
2,
1,
}
}
};


public GameObject keySet;

public int shuffleCount;
public float shuffleTime;
public float shuffleSpeed;

List<int> keySlots = new List<int>()
{
0,
1,
2,
3,
4,
5,
6,
7
};

List<GameObject> keys = new List<GameObject>();

Color32 normalColour = new Color32(255, 255, 255, 255);
Color32 indicateColour = new Color32(0, 255, 0, 255);

void Start() {
for (int i = 0; i < keySet.transform.childCount; i++)
{
keys.Add(keySet.transform.GetChild(i).gameObject);
}

StartCoroutine(GameStart());
}

IEnumerator GameStart() {
yield return new WaitForSecondsRealtime(1f);

GameObject key = keys[Random.Range(0, keys.Count - 1)];

StartCoroutine(Lerp("colour", key, .5f, normalColour, indicateColour));

yield return new WaitForSecondsRealtime(1f);

StartCoroutine(Lerp("colour", key, .5f, indicateColour, normalColour));

yield return new WaitForSecondsRealtime(1f);

for (int i = 0; i < shuffleCount; i++)
{
Movement selectedMovement;
//Movement selectedMovement = movements[Random.Range(0, movements.Count)];
if (i % 2 == 0)
{
selectedMovement = movements[1];
}
else
{
selectedMovement = movements[0];
}

print(selectedMovement.movementName);

for (int c = 0; c < keys.Count; c++)
{
int nextPos = selectedMovement.keyTable[c] - 1;

//StartCoroutine(Lerp("movement", keys[keySlots[c]], shuffleSpeed, keys[keySlots[c]].transform.position, keys[keySlots[nextPos]].transform.position));
}

for (int c = 0; c < keySlots.Count; c++)
{
int newKey = selectedMovement.keyTable.IndexOf(keySlots[c] + 1);

keySlots[c] = newKey;

print(i + " " + (newKey + 1));
}

yield return new WaitForSecondsRealtime(shuffleTime);
}
}
This is how this current code is attempting to shuffle the keys after the "Shuffle_Up" (the second movement)
Nuc
NucOP2y ago
Nuc
NucOP2y ago
The left is after the Diagonal_Top, and the right is after the Shuffle_Up as you can see these values are completely incorrect Movements are based off a list of integers of where each key should move to.
Nuc
NucOP2y ago
Nuc
NucOP2y ago
E.g. in this case, key 1 is moved to where key 2 is, and key 6 is moved to where key 8 is. Does anyone know why after doing a Diagonal_Top "keySlots" is breaking?
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server