• 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 Silx · Apr 04, 2012 at 07:31 PM · c#arraysobjectsarraylist

Getting data from Arraylist within Arraylist

I'm sure the first response will be 'wtf, don't use arraylists!', but I'm really just trying to get this working in any way that I can.

It's basically the 'match looker' for a match 3 game. I'm having trouble getting access to the match data within the match list. See below..

     public void FindAndRemoveMatches() {
 
     ArrayList foundMatches = LookForMatches();
     
     //just checking if we're getting the right amount for now
     Debug.Log("We found " + foundMatches.Count + " 'Match 3's");
     
     foreach(Object el in foundMatches){
        //    Debug.Log(el.ToString());
     }
     
 }
 
 ArrayList LookForMatches(){
 
     //List<int> matchList = new List<int>();
     ArrayList matchList = new ArrayList();
     
     // search for horizontal matches
     // note that we're subtracting two rows here.
     // We don't need to check the last two rows because we're matching 3.
     for (int i = 0; i < BOARD_WIDTH; i++){
         for (int j = 0; j < BOARD_HEIGHT-2; j++){
             ArrayList match = GetMatchHoriz(i,j);
             if (match.Count > 2) {
                 matchList.Add(match);
                 i += match.Count-1;
             }
         }
     }
     
     // search for vertical matches
     for (int i = 0; i < BOARD_WIDTH; i++){
         for (int j = 0; j < BOARD_HEIGHT-2; j++){
             ArrayList match = GetMatchVert(i,j);
             if (match.Count > 2) {
                 matchList.Add(match);
                 j += match.Count-1;
             }
                 
         }
     }
     
     
     return matchList;
 }
 
 
 // look for horizontal matches starting at this point
 ArrayList GetMatchHoriz(int col,int row){
     ArrayList match = new ArrayList();
     match.Add(mBoard[col,row]);
     
     for(int i = 1; (col+i)<8; i++) {
         if (mBoard[col,row] == mBoard[col+i,row]) {
             if(mBoard[col+i,row] > mPieces.GetNumPieceTypes()) match.Add(mBoard[col+i,row]);
         } else {
             return match;
         }
     }
     return match;
 }    
 
 // look for horizontal matches starting at this point
 ArrayList GetMatchVert(int col,int row){
     ArrayList match = new ArrayList();
     match.Add(mBoard[col,row]);
     
     for(int i = 1; (row+i)<8; i++) {
         if (mBoard[col,row] == mBoard[col,row+i]) {
             if(mBoard[col,row+i] > mPieces.GetNumPieceTypes()) match.Add(mBoard[col,row+i]);
         } else {
             return match;
         }
     }
     return match;
 }    

Good news is, I know that it's finding the matches correctly. The number of matches it's finding using the debug log correlates with what's happening on the screen. Yay! But, I need to get access to that data so I can compare it to the board (mBoard[col,row]) and remove those objects from the game board.

The 'foreach' loop in findandremovematches gives an error about casting. Any help with this?

thanks!

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 Eric5h5 · Apr 04, 2012 at 09:20 PM 2
Share

wtf, don't use ArrayList. ;) Seriously, why? Just use List, then you won't have any errors about casting.

avatar image cregox · Apr 04, 2012 at 10:05 PM 0
Share

@Eric maybe that's supposed to be the answer?! :P

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Array empties values on RunTime? 1 Answer

I'm already an experienced programmer, is there anywhere great to go to get familiar with the libraries available in unity? 2 Answers

Using a Parameterized arraylist (C#)??? 1 Answer

getting an object to stick to terrain in c# 1 Answer

Identifier Expected Error ArrayList -1 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges