Enforce Level generation to minimum of numberOfColors

pull/14/head
Dennis Bücker 1 year ago
parent 6c889a8587
commit 3174c7e08f
  1. 134
      src/SimonSays.cpp

@ -153,81 +153,87 @@ void SimonSays::generateNewSequence() {
break;
}
Serial.print("Generated sequence ");
for (int i = 0; i < currentSequenceLength; i++)
{
currentSequence[i] = random(numberOfColors);
Serial.print(currentSequence[i]);
Serial.print(" ");
}
// Repeat generation until enough colors are generated for level.
// Initially reset number of unique colors
numberOfUniqueColors = 0; // otherwise no new sequence might be generated
while (numberOfUniqueColors < numberOfColors)
{
Serial.print("Generated sequence ");
for (int i = 0; i < currentSequenceLength; i++)
{
currentSequence[i] = random(numberOfColors);
Serial.print(currentSequence[i]);
Serial.print(" ");
}
Serial.print(" with ");
Serial.print(currentSequenceLength);
Serial.print(" members for level ");
Serial.print(currentLevel);
Serial.println(".");
Serial.print(" with ");
Serial.print(currentSequenceLength);
Serial.print(" members for level ");
Serial.print(currentLevel);
Serial.println(".");
// Generate array with only active colors
int activeSequenceColors[currentSequenceLength];
for (int i = 0; i < currentSequenceLength; i++)
{
activeSequenceColors[i] = currentSequence[i];
}
// Generate array with only active colors
int activeSequenceColors[currentSequenceLength];
for (int i = 0; i < currentSequenceLength; i++)
{
activeSequenceColors[i] = currentSequence[i];
}
/*
// Debug unsorted Array
Serial.print("UNsorted Array: ");
for (int i = 0;i<simonSays.currentSequenceLength;i++)
{
Serial.print(activeSequenceColors[i]);
Serial.print(" ");
}
Serial.println();
*/
/*
// Debug unsorted Array
Serial.print("UNsorted Array: ");
for (int i = 0;i<simonSays.currentSequenceLength;i++)
{
Serial.print(activeSequenceColors[i]);
Serial.print(" ");
}
Serial.println();
*/
// Sort the array (high to low)
qsort(activeSequenceColors, currentSequenceLength, sizeof(activeSequenceColors[0]), sort_desc);
// Sorting is not necessary at this time, but makes highlight sequence in good order
// Sort the array (high to low)
qsort(activeSequenceColors, currentSequenceLength, sizeof(activeSequenceColors[0]), sort_desc);
// Sorting is not necessary at this time, but makes highlight sequence in good order
/*
// Debug sorted Array
Serial.print("Sorted Array: ");
for (int i = 0; i < simonSays.currentSequenceLength; i++)
{
Serial.print(activeSequenceColors[i]);
Serial.print(" ");
}
Serial.println();
*/
/*
// Debug sorted Array
Serial.print("Sorted Array: ");
for (int i = 0; i < simonSays.currentSequenceLength; i++)
{
Serial.print(activeSequenceColors[i]);
Serial.print(" ");
}
Serial.println();
*/
// Determine the unique elements in the array (for display) and their number
for (int i = 0; i < MCUInfo::maxColorArrayLength; i++)
{
arrayOfUniqueColors[i] = 99999; // set array to high value that is not in color sequence
}
//int arrayOfUniqueColors[MCUInfo::maxColorArrayLength];
numberOfUniqueColors = 0;
// Determine the unique elements in the array (for display) and their number
for (int i = 0; i < MCUInfo::maxColorArrayLength; i++)
{
arrayOfUniqueColors[i] = 99999; // set array to high value that is not in color sequence
}
//int arrayOfUniqueColors[MCUInfo::maxColorArrayLength];
numberOfUniqueColors = 0;
int i, k;
int i, k;
for (i = 0; i < currentSequenceLength; i++)
{
// Iterate through active color sequence
int flag = 0;
for (k = 0; k <= numberOfUniqueColors; k++)
for (i = 0; i < currentSequenceLength; i++)
{
// iterate through saved elements if it is a duplicate
if (activeSequenceColors[i] == arrayOfUniqueColors[k])
// Iterate through active color sequence
int flag = 0;
for (k = 0; k <= numberOfUniqueColors; k++)
{
// Serial.println("set flag");
flag = 1; // this is a duplicat
break;
// iterate through saved elements if it is a duplicate
if (activeSequenceColors[i] == arrayOfUniqueColors[k])
{
// Serial.println("set flag");
flag = 1; // this is a duplicat
break;
}
}
if (flag != 1)
{
// add only if it is no duplicat and advance counter
arrayOfUniqueColors[numberOfUniqueColors++] = activeSequenceColors[i]; // assign to unique elements array
}
}
if (flag != 1)
{
// add only if it is no duplicat and advance counter
arrayOfUniqueColors[numberOfUniqueColors++] = activeSequenceColors[i]; // assign to unique elements array
}
}

Loading…
Cancel
Save