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 /
  • Help Room /
avatar image
0
Question by ComradeVanti · Mar 12, 2016 at 06:18 PM · listsintcombinecompare

Get every possible combination of elements in a list

Hey Community. So here is what I wanna do.

I have a List of ints and I want to compare every possible combination of the elements to an int.

For example. my List contains 2 ints, x and y. I also have an int z.

What I wanna do is check if:

z = x

or

z = y

or

z = x + y

or

z = y + x.

And if one of these returns true i wanna do some stuff.

Of cours my list does not always contain 2 elements but ranges from 1 to 6 elements... so yeah...

Little note: If my Lists contains 3 Elements, for example a, b and c I also wanna check the possibility:

z = a + b ...without c...

Any ideas?

Comment
Add comment · Show 2
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 OllysCoding · Mar 12, 2016 at 06:51 PM 0
Share

In what context is this required, just incase there is a better approach?

avatar image ComradeVanti OllysCoding · Mar 12, 2016 at 06:56 PM 0
Share

Basically my situation is: Im making a card game On my field are 1 - 6 cards witch each have a value from 1 - 10 In my hand is one card. also with a value from 1 - 10 Now what i wanna do is check if there is any combinations of the cards on the field in witch the sum of their values is equal to the value of the card in my hand and the do stuff with those cards.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by toddisarockstar · Mar 12, 2016 at 07:32 PM

you need a loop inside a loop ... something like this

 var i:int;
 var i2:int;
 
 i=yourlist.length;
 while(i>0){i--;
  
 i2=yourlist.length;
 while(i2>0){i2--;

 // compare your suff here however ya like!!!!
 if(yourlist[i].z==yourlist[i2].x+yourlist[i2].y){
 print("one of your z's adds to x and y");}
                                   
  if(yourlist[i].x==yourlist[i2].y){
   print("one of your x's equals one of your y's" );}

} }

Comment
Add comment · Show 4 · 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 ComradeVanti · Mar 12, 2016 at 07:42 PM 0
Share

thanks for the answer... i dont think this is what i need though. Did you read the comment I wrote above? There I write more detailed what I need. :)

avatar image toddisarockstar ComradeVanti · Mar 12, 2016 at 07:59 PM 0
Share

i didnt see the comment when i seen the first answer.

 var cardinhand:int;
     cardinhand=Random.Range(1,11);
 var field:int[];
     field=new int[Random.Range(1,7)];
     
 for(var shuffle:int in field){shuffle=Random.Range(1,11);}
 i=field.Length;
 while(i>0){i--;
 i2=field.Length;
 while(i2>0){i2--;
     
     if(i!=i2){if(cardinhand==field[i]+field[i2]){
     print("two cards on the table = card in hand");}}}}

if you wanted to check to see if three cards add up you would add a third loop and so on..

  var i:int;
 var i2:int;
 var i3:int;
 
 var cardinhand:int;
     cardinhand=Random.Range(1,11);
 var field:int[];
     field=new int[Random.Range(1,7)];
     
 for(var shuffle:int in field){shuffle=Random.Range(1,11);}i=field.Length;
 
 while(i>0){i--;
 i2=field.Length;
 while(i2>0){i2--;
     
     if(i!=i2){if(cardinhand==field[i]+field[i2]){
     print("card number "+i+"+ cardnumber"+i2+"= card in hand");}
     i3=field.Length;
     while(i3>0){i3--;
     if(i!=i3){if(i2!=i3){
     if(field[i]+field[i2]+field[i3]==cardinhand){print("card numbers "+i+","+i2+"and "+i3+" = card in hand");}}
     }}}
    
     }}
avatar image ComradeVanti toddisarockstar · Mar 12, 2016 at 09:16 PM 0
Share

Thanks a lot... I'll dig into that. Looks promising... Ehm... im sure I will manage to translate it anyway but would you $$anonymous$$d writing it in C# if you are familiar with it? :D

Show more comments

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

50 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 avatar image avatar image avatar image

Related Questions

Trying to compare an input field with an int 0 Answers

I have to compare the int of 2 Lists 0 Answers

How do I compare the same int in two different GameObjects? 1 Answer

Combine meshes with different materials 0 Answers

"roughly" equals check between two vector3? 0 Answers


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