Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Noxury · Apr 02, 2016 at 10:31 PM · arrayvaluesame

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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";
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
     }
 }
 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eno-Khaon · Apr 02, 2016 at 11:33 PM 1
Share

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.

avatar image Noxury · Apr 03, 2016 at 02:29 PM 0
Share

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.

avatar image Bunny83 Noxury · Apr 03, 2016 at 10:10 PM 0
Share

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);
  }
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

47 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges