# 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*.owner == cps[i+1].owner){*
``````

_ if(cps*.owner == “RED”){_
_
Lost();_
_ } else if(cps.owner == “BLUE”) {
Won();
}
}
}
}*
This seems to be inefficient and don’t works. Any help is appreciated_

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";
}
``````

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*;*
``````
• `````` CapturePoint previousCP = CapturePoints[i - 1];*
``````
• `````` if (currentCP.team != previousCP.team)*
``````
• `````` {*
``````
• `````` 	allSame = false;*
``````
• `````` 	break;*
``````
• `````` }*
``````
• `````` WinningTeam = currenCP.team;*
``````
• }*

• if (allSame)*

• {*

• `````` TeamWin(winningTeam);*
``````
• }*
}

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