Enforce Level generation to minimum of numberOfColors

pull/14/head
Dennis Bücker 2021-04-15 18:12:23 +02:00
parent 6c889a8587
commit 3174c7e08f
1 changed files with 78 additions and 72 deletions

View File

@ -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(" ");
}
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];
}
/*
// 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
/*
// 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;
int i, k;
for (i = 0; i < currentSequenceLength; i++)
{
// Iterate through active color sequence
int flag = 0;
for (k = 0; k <= numberOfUniqueColors; k++)
// 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++)
{
// 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;
}
currentSequence[i] = random(numberOfColors);
Serial.print(currentSequence[i]);
Serial.print(" ");
}
if (flag != 1)
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++)
{
// add only if it is no duplicat and advance counter
arrayOfUniqueColors[numberOfUniqueColors++] = activeSequenceColors[i]; // assign to unique elements array
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();
*/
// 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();
*/
// 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;
for (i = 0; i < currentSequenceLength; i++)
{
// Iterate through active color sequence
int flag = 0;
for (k = 0; k <= numberOfUniqueColors; k++)
{
// 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
}
}
}