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 ez06 · Mar 07, 2014 at 09:08 PM · arraysmath

How can I code a pattern detector in an array of integers?

I have an array of integers:

 var theArray = int[10,10] ;

This 10x10 array is randomly populated with integers: 1, 2 or 3.

I would like to loop through this array and detect the following patterns: "1 2 3" and "1 2 1", either horizontally or vertically.

What's the most efficient and cleanest way of achieving that? Using strings? Calculations?

Here is the sample code you can use if you wish:

 for (var x: int = 0 ; x < 10 ; x++) {
         for (var y : int = 0 ; y < 10 ; y++) {

           // theArray[x,y] = ...

          } 
 }
Comment
Add comment · Show 4
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 SirCrazyNugget · Mar 07, 2014 at 09:51 PM 0
Share

I'd start off by finding all the values of the first value (looking for integers rather than strings), then have a method to check neighbors in each direction, then continue in that direction until the length of the pattern your searching for is correct.

If it's true you'll be able to return the start location point of the pattern and the direction it's headed, you can also store the length or add the output to a list of what was found.

Bear in $$anonymous$$d though it could start getting confusing if you've already found a pattern which is a palindrome, "1 2 1" will end up being listed twice.

avatar image ez06 · Mar 07, 2014 at 11:29 PM 0
Share

Hi there, thanks for your response. It gave me some good ideas. Feel free to submit it as an answer. Thanks

avatar image ShadoX · Mar 07, 2014 at 11:58 PM 0
Share

if the arrays are sorted, then perhaps http://en.wikipedia.org/wiki/Binary_search_algorithm would work ? starts off @ center and depending on if the center value is higher, lower, it just either checks the left or right side of the array and starts at that sides center..

avatar image ez06 · Mar 08, 2014 at 12:19 AM 0
Share

Hi, thanks for your suggestion. But in this case, it wouldn't work because this array can't be sorted. Thanks

1 Reply

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

Answer by SirCrazyNugget · Mar 09, 2014 at 03:33 PM

I'd start off by finding all the values of the first value (looking for integers rather than strings), then have a method to check neighbors in each direction, then continue in that direction until the length of the pattern your searching for is correct.

If it's true you'll be able to return the start location point of the pattern and the direction it's headed, you can also store the length or add the output to a list of what was found.

Bear in mind though it could start getting confusing if you've already found a pattern which is a palindrome, "1 2 1" will end up being listed twice.

(Glad it helps)

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

21 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

A problem with intersection detection 1 Answer

Array empties values on RunTime? 1 Answer

Trying to stop a float at a certain value. 2 Answers

For loop not running correctly inside start () 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