Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 katie88 · Jul 15, 2012 at 11:18 PM · arraychecknumberscontains

Check if an array contains certain number combos?

I am making a simple game for Android in JavaScript. In order for a player to win the game, an array holding their score numbers has to match one of eight winning number combinations.

My idea was to create a multidimensional array to hold the eight winning number combinations. Then I would have the game compare the players' score arrays with those combinations to see if anyone has won yet. This is what my multidimensional array looks like:

 var winCom1 = [1,2,3];
 var winCom2 = [4,5,6];
 var winCom3 = [7,8,9];
 var winCom4 = [1,4,7];
 var winCom5 = [2,5,8];
 var winCom6 = [3,6,9];
 var winCom7 = [1,5,9];
 var winCom8 = [7,5,3];
 
 var winComArray = [winCom1, winCom2, winCom3, winCom4, winCom5, winCom6, winCom7, winCom8];

Possible issue: My players' score arrays could have between 3 and 5 elements, so they may not have the same length as the winning combo array I'm trying to compare them to.

Also, it shouldn't matter what order the score numbers are in. Whether the player's score array looks like "2,3,1", or "2,7,1,3", etc... I still want that player to win because their array contains "winCom1".

Is there a good way to go about this method, if it's even possible? And if anyone has any alternate (i.e. simpler) ideas on how to check for winning number combos, I'd love to hear them.

Thank you in advance!

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

1 Reply

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

Answer by whydoidoit · Jul 15, 2012 at 11:22 PM

  import System.Linq;

  ...


 function Start()
 {
  var winCom1 = [1,2,3];
  var winCom2 = [4,5,6];
  var winCom3 = [7,8,9];
  var winCom4 = [1,4,7];
  var winCom5 = [2,5,8];
  var winCom6 = [3,6,9];
  var winCom7 = [1,5,9];
  var winCom8 = [7,5,3];
 
  var winComArray = [winCom1, winCom2, winCom3, winCom4, winCom5, winCom6, winCom7, winCom8];
  
  var playerScoreArray = [10,11,1,3,2];
  
  if(winComArray.Cast.<int[]>().Any(function(combo){return combo.Cast.<int>().Intersect(playerScoreArray.Cast.<int>()).Count()>=3;}))
  { 
  }
 }
Comment
Add comment · Show 7 · 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 whydoidoit · Jul 15, 2012 at 11:27 PM 1
Share

An explanation:

  • Linq is used to work with sets of data

  • The if says take each entry in the winComArray and intersect the elements with the playerScoreArray, then count it - if any one returns>=3 then the player won.

avatar image katie88 · Jul 16, 2012 at 01:30 AM 0
Share

Thank you for your response! I tried that out, but the compiler gives 2 errors:

'Any' is not a member of 'Array' 'Intersect' is not a member of 'Object'.

I will look deeper into Linq, as I've never heard of it. And I hadn't thought of using Intersect. So I will have to look into that, as well.

avatar image whydoidoit · Jul 16, 2012 at 01:41 AM 0
Share

Ah sorry about that I have modified the answer (I forgot damn JS needs casts on in built arrays).

avatar image katie88 · Jul 16, 2012 at 02:19 AM 0
Share

Ahh that works perfectly! Thank you so much!!

avatar image whydoidoit · Jul 16, 2012 at 06:33 AM 0
Share

Could you mark the questions as answered for me? Get's it off the unsolved list :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

i need a player list help please 0 Answers

Looping through array to find a specific class? 0 Answers

if a string has certain letter(s) or number(s) 2 Answers

Check array slot against corresponding slot in another array? 1 Answer

Get Material of Normal? 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