Hi, I’ve been doing a pretty basic “Simon says” game and Im kinda stuck on a problem:
I’m saving the color order as numbers in an int list to compare them later with user color picks while is playing. I’ve tried to compare them through two for loops but it just doesn’t work.
Does exist a direct compare method for lists? I’m just looking for check if both lists have same elements and same order of those elements.
Like: {1,2,3} and {1,2,3}.
I’ve tried with Equals but doesn’t work either.
if (roundOrder.Equals (playerOrder))
{
nextlevel.text = "Correct! Next Round...";
currentRound++;
}
It is very simple. First, check of the lists have the same amount of elements, if they do not, then you already know the answer, return false. Then if they have the same number of elements, do a single for loop until the count of one of the lists. Just check if the elements match. If they do not, then return false, if everything goes fine, then return true.
bool CheckMatch() {
if (l1.Count != l2.Count)
return false;
for (int i = 0; i < l1.Count; I++) {
if (l1 _!= l2*)*_
How about this helper function. It counts the number of matches in 2 lists of int. It allows to process 2 lists with different sizes, in case you want to calculate the percentage of taken steps compared to required steps.
static public int CountMatches(List<int> required, List<int> taken )
{
int numMatches = 0;
int idx = 0;
while( idx < required.Count && idx < taken.Count )
{
if( required[idx] == taken[idx] )
{
numMatches++;
idx++;
}
else
{
break;
}
}
return numMatches;
}
Having this you could write:
int NumMatches = CountMatches(required,taken);
if( NumMatches == required.Count )
{
nextlevel.text = "Correct! Next Round...";
currentRound++;
}
else
{
// you can calculate the percentage of required vs. taken steps,
// and present it to the player
float pcntg = 100.0f * (float)NumMatches / (float)required.Count;
}