How do I compare two lists in C#
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++;
}
Answer by AurimasBlazulionis · Sep 15, 2016 at 05:44 AM
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[i] != l2[i])
return false;
}
return true;
}
$$anonymous$$ight also want to check for null, in case one or both of the lists are not initialized, maybe something along these lines:
bool Check$$anonymous$$atch() {
if(l1 == null && l2 == null)
{
return true;
}
else if(l1 == null || l2 == null)
{
return false;
}
if (l1.Count != l2.Count)
return false;
for (int i = 0; i < l1.Count; I++) {
if (l1[i] != l2[i])
return false;
}
return true;
}
A lottle bit wrong. You need to check only if one of them is null, then return false, but do not return true, since it ends the whole function.
Well, I'd expect a comparison between null and null to return true.
If that is not the case (the else-if part), if one of them is null they must be different, therefore I return false.
But yes, if you need a function that returns false if both lists are null it's even easier.
Answer by szimmermann · Sep 15, 2016 at 08:47 AM
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;
}
Your answer
Follow this Question
Related Questions
How to Destroy game object horizontally and vertically , when hit by a Raycast 0 Answers
Always wondered how to sync animations with attack effects 0 Answers
Adding a Highscore to game 1 Answer
SPACE SHOOTER SHOOTING SHOTS TUTORIAL HELP (UNITY 5 C#) 3 Answers
C# Script not working as expected 2 Answers