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 NGKrush1 · May 07, 2013 at 05:57 PM · c#array2d array

check occurence of int in 2darray c#

Hello,

I've got a simple game made up of a 3x4 2darray (with variable int) like so:

[][][][]

[][][][]

[][][][]

After each turn I want to check if the win conditions are met by comparing if 3 of the 4 int's in only the top row are equal (these can range from 0 to 200, so it's impossible to check for each #). So if the top row would be 1,2,2,3 the game would not be won, but it would like this: 1,4,1,1 as this row now has 3 1's.

I've been trying to figure out how to write this out, but haven't found any good ideas within my beginning c# scope. Could someone point me in the right direction?

Thanks 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

3 Replies

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

Answer by hoy_smallfry · May 07, 2013 at 06:37 PM

This problem is easily solveable if you simplify it into groups of the same number. For instance, in your example {1,2,2,3} the groups are: 1 with 1 times it appears, 2 with 2 appearances, and 3 with 1 appearance. For {1,4,1,1}, the groups are 1 with 3 count, and 4 with 1 count. This is true no matter what the order of the array, right? If it was {1,1,1,4} it would still be 1 with 3 count, and 4 with 1 count.

Now, assuming that 4-of-a-kind is also considered a win, the the array is {X, X, X, X} with X being any number. In this case, there is only one group, X, with a count of 4. In the minimum win case, {X, X, X, Y} (and all other permutations of it), there is will always be an X with a count of 3, and a Y with a count of 1. Both win conditions can be generalized like this: One group will always have a count of 3 or more and there will always be 2 groups or less.

You can pull off this concept of groups with a System.Collections.Generic.Dictionary. With the dictionary, the element's key would be a number in your array, and the value associated would represent how many times that number appears in your array.

The code would go something like this: You would loop through each element in the top array, and if it doesn't already exist in the dictionary, add it to the dictionary with count of 1. If the number already exists in the dictionary, just increase the value by 1. After looping through the array, it would then be a simple loop through the dictionary to see if there are any elements who's values are greater or equal to 3.

For the array {1,4,1,1}, the dictionary's elements would be {(1,3), (4,1)}. If you do not know how to use Dictionary, there are plenty of resources out there, including the this one and this one.

Comment
Add comment · Show 2 · 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 hoy_smallfry · May 07, 2013 at 07:08 PM 0
Share

At the worst case, you'll have 4 elements in the dictionary you have to loop through, and at the best case it'll only be 1, but you can cut it having to loop through 3 and 4 elements altogether, because no matter what a dictionary count of 3 or 4 is an automatically a lose condition, because there's no way any of those groups could have a count of 3 or more. You can probably also have it consider a dictionary count of 1 to be an automatic win, since that would be guaranteed to have a count of 3 or more.

avatar image NGKrush1 · May 10, 2013 at 10:43 AM 1
Share

thanks for your idea, I've been trying to work this out and have writtin this piece of code as the solution:

         Dictionary<int,int> dic = new Dictionary<int, int>();
         //---check p1        
         for (int d=0;d<5;d++)
         {
             if (holders[d,0] != 0)
             {
                 if (!dic.Contains$$anonymous$$ey(holders[d,0]))
                 {
                     dic[holders[d,0]] = 0;
                 }
                 dic[holders[d,0]]++;
             }
         }
         
         foreach (int k in dic.$$anonymous$$eys)
         {
             if (k>=3)
             {
                 Awins = true;
             }
         }

thanks so much for your help!

edit: i asked a question not related to this, but found a solution, so deleted that q to keep this thread clean.

avatar image
1

Answer by robertbu · May 07, 2013 at 07:56 PM

This assumes column/row order, but there are only four winning combinations in the row which can easily be checked with an if statement:

 if (   a[0,0] == a[0,1] && a[0,1] == a[0,2])
     || a[0,1] == a[0,2] && a[0,2] == a[0,3])
     || a[0,0] == a[0,2] && a[0,2] == a[0,3])
     || a[0,0] == a[0,1] && a[0,1] == a[0,3])) {
     Debug.Log("You've Won!");
 }

Note if four of a kind is not a winning combination, you will need to filter that out first.

If you need to check all the rows, put it in a for loop:

 if (   a[i,0] == a[i,1] && a[i,1] == a[i,2])
     || a[i,1] == a[i,2] && a[i,2] == a[i,3])
     || a[i,0] == a[i,2] && a[i,2] == a[i,3])
     || a[i,0] == a[i,1] && a[i,1] == a[i,3])) {
     Debug.Log("You've Won!");
     break;
 }
Comment
Add comment · Show 1 · 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 NGKrush1 · May 10, 2013 at 10:44 AM 0
Share

thanks, this actually solved my problem, but created a new one as it would also accept 4 0's as a win (something which I didn't mention in my question, so the fault is on my side) but the reason why I chose another answer as the solution! thanks a lot!

avatar image
0

Answer by MeLight · May 07, 2013 at 06:24 PM

If you need to check only one row of 4 for 3 instances of the same number you can do this: Check is the first number in the row (index 0) repeats 3 times at least, and if not, do the same for the second number (index 1). If neither of them repeats at least 3 times you don't have numbers repeating 3 times.

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

14 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Values in Array Automatically Revert 1 Answer

Determining groups in a 2D array by checking neighbours (and their neighbours etc) 2 Answers

C# - IndexOutOfRangeException - Array index is out of range 2 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