- Home /
Check if all elements of an array contain the same value?
Hello,
I have an array of CapturePoints which can be owned by a Team and the game should be won or lost if all the capturePoints are captured by the same team, but how can I solve this?
void OnCapture(){
for(int i = 0; i < cps.Length; i++){
if(cps[i].owner == cps[i+1].owner){
if(cps[i].owner == "RED"){
Lost();
} else if(cps[i].owner == "BLUE") {
Won();
}
}
}
}
This seems to be inefficient and don't works. Any help is appreciated
Answer by saschandroid · Apr 04, 2016 at 07:53 AM
Using predicates:
List<CapturePoint> m_capPoints = new List<CapturePoint>();
private void OnCapture()
{
if ( m_capPoints.TrueForAll(IsTeamRed))
{
Debug.Log("RED wins");
}
else if ( m_capPoints.TrueForAll(IsTeamBlue))
{
Debug.Log("BLUE wins");
}
else
{
Debug.Log("NOBODY wins");
}
}
private static bool IsTeamRed(CapturePoint f_capPoint)
{
return f_capPoint.owner == "RED";
}
private static bool IsTeamBlue(CapturePoint f_capPoint)
{
return f_capPoint.owner == "BLUE";
}
Answer by phxvyper · Apr 02, 2016 at 11:18 PM
This is the basic idea of how I would handle it, code below.
I would have a list like you do, loop through the list and compare pairs of CapturePoints. You don't have to compare every single point with every other point because you're only checking for equality.
Basically: if a == b, and b == c, then you know a == c without ever actually checking it.
List<CapturePoint> CapturePoints = new List<CapturePoint>();
enum Team
{
Blue,
Red
}
void OnCapture()
{
bool allSame = true;
Team winningTeam = Team.Blue;
for (int i = 0; i < CapturePoints.Count; i++)
{
if (i == 0)
{
continue;
}
CapturePoint currentCP = CapturePoints[i];
CapturePoint previousCP = CapturePoints[i - 1];
if (currentCP.team != previousCP.team)
{
allSame = false;
break;
}
WinningTeam = currenCP.team;
}
if (allSame)
{
TeamWin(winningTeam);
}
}
By association, if a == b and a == c, then you know b == c as well. So, elements 1 through _.Count - 1 can all simply be compared with element 0 for the same effect.
Thanks, but it don't works properly, because if cp[0] and cp[1] have Blue as owner while cp[2], cp[3], ... have red as owner, it will say that blue has won.
Why did you downvote the answer? It does answer the question and it does work pretty well. Downvoting is ment for misleading or wrong information. It seems you simply did not understand the answer.
The loop compares item by item in pairs of their previous item. So it compares:
0 with 1
1 with 2
2 with 3
...
As soon as one of those pairs are not equal "allSame" is set to false so no $$anonymous$$m will win as not all CPs belong to the same $$anonymous$$m.
As Eno $$anonymous$$haon said it's simpler to just compare all items to the first one but the sample phxvyper provided does work as well.
@phxvyper: Ins$$anonymous$$d of that "continue" to skip the first iteration, why not simply start the loop at "1" ins$$anonymous$$d of "0" ^^.
It could be even simplified with an early exit:
void CheckWinningCondition()
{
Team winningTeam = CapturePoints[0].$$anonymous$$m;
for (int i = 1; i < CapturePoints.Count; i++)
{
if (CapturePoints[i].$$anonymous$$m != winningTeam)
{
return; // exit the whole method as at least two are not equal
}
}
// if we reach this point, all are the same as "winningTeam"
TeamWin(winningTeam);
}
Answer by Txguug · Apr 04, 2016 at 04:12 AM
A 2d array maybe?
float[,]data = new float[numberofcapturepoints, 3]
float[0,0] = x position of capture point 1
float[0,1] = y position of capture point 1
float[0,2] = color of capture point 1 ( 1 for blue, 2 for red or whatever you fancy)
float[1,0] = x position of capture point 2
float[1,1] = y position of capture point 2
float[1,2] = color of capture point 2 ( 1 for blue, 2 for red or whatever you fancy)
etc..
then you can check the number of capture points per color
for (int i = 0; i < numberofcapturepoints; i++)
{
float color = data[i,3]; // get color of this capture point
if (color == 1) blue++;
if (color ==2) red++;
}
if either red or blue is numberofcapturepoints that team wins
Your answer
Follow this Question
Related Questions
Array value not changing? 1 Answer
How to Show Random.Range in the Console when used in an Array? 0 Answers
How to get the key from Array value 1 Answer
Adding array values together 3 Answers
Add values to Int[] (SetTriangles) 1 Answer