Move sequence analysis to main routine.

pull/14/head
Dennis Bücker 2021-04-15 18:03:03 +02:00
parent 9c818b730f
commit 6c889a8587
3 changed files with 103 additions and 106 deletions

View File

@ -15,27 +15,12 @@ Success: Nothing until final success: Show a green screen for a short time.
*/
// qsort requires you to create a sort function
int sort_desc(const void *cmp1, const void *cmp2)
{
// Need to cast the void * to int *
int a = *((int *)cmp1);
int b = *((int *)cmp2);
// The comparison
return a > b ? -1 : (a < b ? 1 : 0);
// A simpler, probably faster way:
//return b - a;
}
EnterColorSequence::EnterColorSequence() :
inputStarted{false},
inputSequencePosition{0},
inputActiveColor{0},
levelCompleted{false},
levelSuccess{false},
numberOfUniqueColors{0}, // Will be determined from the sequence
arrayOfUniqueColors{0,0,0,0,0,0,0,0,0,0} // length of MCUInfo::maxColorArrayLength
levelSuccess{false}
{}
void EnterColorSequence::enter(SimonSays & simonSays){
@ -47,91 +32,13 @@ void EnterColorSequence::enter(SimonSays & simonSays){
inputSequencePosition = 0;
inputActiveColor = 0;
// Determine the number of unique
// Generate array with only active colors
int activeSequenceColors[simonSays.currentSequenceLength];
for (int i=0;i<simonSays.currentSequenceLength;i++)
{
activeSequenceColors[i] = simonSays.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, simonSays.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 < simonSays.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
}
}
// Debug print outcome
Serial.print("Unique elements: ");
for (int i = 0; i < numberOfUniqueColors; i++)
{
Serial.print(arrayOfUniqueColors[i]);
Serial.print(" ");
}
Serial.println();
Serial.print("Number of unique elements: ");
Serial.println(numberOfUniqueColors);
// give information to display module
simonSays.simonDisplay.setNumberColors(numberOfUniqueColors);
simonSays.simonDisplay.setNumberColors(simonSays.numberOfUniqueColors);
simonSays.simonDisplay.setActiveColor(inputActiveColor);
simonSays.simonDisplay.setColorsToDisplay(arrayOfUniqueColors);
simonSays.simonDisplay.setColorsToDisplay(simonSays.arrayOfUniqueColors);
// Switch to correct color selection state
switch (numberOfUniqueColors)
switch (simonSays.numberOfUniqueColors)
{
case 1:
simonSays.simonDisplay.setDisplayMode(1);
@ -171,15 +78,15 @@ void EnterColorSequence::update(SimonSays &simonSays)
if (M5.Btn.wasReleased())
{
// Advance the input color
inputActiveColor = (inputActiveColor + 1) % numberOfUniqueColors;
inputActiveColor = (inputActiveColor + 1) % simonSays.numberOfUniqueColors;
// Debug help to see where input is in sequence and if next input is correct.
Serial.print("Next requested color is ");
Serial.print(simonSays.currentSequence[inputSequencePosition]);
Serial.print(" and current selected color is ");
Serial.print(arrayOfUniqueColors[inputActiveColor]);
Serial.print(simonSays.arrayOfUniqueColors[inputActiveColor]);
Serial.print(" an input click would be: ");
if (arrayOfUniqueColors[inputActiveColor] == simonSays.currentSequence[inputSequencePosition])
if (simonSays.arrayOfUniqueColors[inputActiveColor] == simonSays.currentSequence[inputSequencePosition])
{
Serial.println("CORRECT.");
}
@ -190,7 +97,7 @@ void EnterColorSequence::update(SimonSays &simonSays)
}
// Update the color to display every tick for failsave
simonSays.simonDisplay.setActiveColor(arrayOfUniqueColors[inputActiveColor]);
simonSays.simonDisplay.setActiveColor(simonSays.arrayOfUniqueColors[inputActiveColor]);
// check for long press for indication, if long enough
if (M5.Btn.wasPressedFor(MCUInfo::longButtonPress))
@ -214,9 +121,9 @@ void EnterColorSequence::update(SimonSays &simonSays)
// --> advance level
// Reset display mode.
simonSays.simonDisplay.setDisplayMode(numberOfUniqueColors);
simonSays.simonDisplay.setDisplayMode(simonSays.numberOfUniqueColors);
if (arrayOfUniqueColors[inputActiveColor] == simonSays.currentSequence[inputSequencePosition])
if (simonSays.arrayOfUniqueColors[inputActiveColor] == simonSays.currentSequence[inputSequencePosition])
{
// show green flash as correct input, maybe enter this earlier after pressdown already

View File

@ -1,7 +1,18 @@
#include "SimonSays.h"
// Anonymous namespace for states
// qsort requires you to create a sort function
int sort_desc(const void *cmp1, const void *cmp2)
{
// Need to cast the void * to int *
int a = *((int *)cmp1);
int b = *((int *)cmp2);
// The comparison
return a > b ? -1 : (a < b ? 1 : 0);
// A simpler, probably faster way:
//return b - a;
}
// Anonymous namespace for states
namespace {
ShowColorSequence showColorSequence;
EnterColorSequence enterColorSequence;
@ -20,6 +31,8 @@ SimonSays::SimonSays(SimonDisplay & _simonDisplay):
currentSequenceLength{1},
currentLevel{0},
currentSpeed{100},
numberOfUniqueColors{0}, // Will be determined from the sequence
arrayOfUniqueColors{0,0,0,0,0,0,0,0,0,0}, // length of MCUInfo::maxColorArrayLength
maxLevel{MCUInfo::maxLevel},
gameLifes{MCUInfo::initialGameLifes},
gameFinishedStatus{false},
@ -147,11 +160,88 @@ void SimonSays::generateNewSequence() {
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++)
{
// 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
}
}
// Debug print outcome
Serial.print("Unique elements: ");
for (int i = 0; i < numberOfUniqueColors; i++)
{
Serial.print(arrayOfUniqueColors[i]);
Serial.print(" ");
}
Serial.println();
Serial.print("Number of unique elements: ");
Serial.println(numberOfUniqueColors);
}
void SimonSays::reportError()

View File

@ -47,8 +47,6 @@ class EnterColorSequence final : public GameState {
bool levelCompleted; // color entering is finished?
bool levelSuccess; // This indicates if level was finished successfull
int ticksTillNextLevel; // time specified how long checkmark is shown
int numberOfUniqueColors; // Will be determined from the sequence
int arrayOfUniqueColors[MCUInfo::maxColorArrayLength]; // will contain an array of all unique colors
};
class GameResultState final : public GameState
@ -89,6 +87,8 @@ public:
int currentSequenceLength;
int currentLevel;
int currentSpeed;
int numberOfUniqueColors;
int arrayOfUniqueColors[MCUInfo::maxColorArrayLength];
int maxLevel;
int gameLifes;
bool gameFinishedStatus;