2012-04-04 3 views
0

첫 번째 응답은 'wtf, arraylists를 사용하지 마십시오!'라고 확신하지만, 실제로는 가능한 모든 방식으로이 작업을 수행하려고합니다.ArrayList 내 ArrayList에 대한 액세스 얻기

기본적으로 일치하는 3 경기를위한 '경기 찾는 사람'입니다. 성냥 목록에서 성냥 데이터에 액세스하는 데 문제가 있습니다. 아래를 참조하십시오.

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; 
} 

좋은 소식은 정확하게 일치를 찾는 것입니다. 디버그 로그를 사용하여 찾은 일치 항목의 수는 화면의 상황과 관련이 있습니다. 예! 그러나 보드 (mBoard [col, row])와 비교하여 게임 보드에서 해당 데이터를 제거 할 수 있도록 해당 데이터에 액세스해야합니다.

findandremovematches의 'foreach'루프가 캐스팅에 대한 오류를 제공합니다. 어떤 도움이 필요합니까?

감사합니다.

+0

정확한 오류 메시지가 나타나는 데 도움이됩니다. – mobiGeek

+0

첫 번째 응답에 대해 틀 렸습니다 :) –

+3

wtf, arraylists를 사용하지 마십시오! –

답변

2

나는 내가 제안 내가 바로 그것을 짐작 경우 제대로

foreach(Objecct obj in list) 
{ 
ArrayList inner = obj as ArrayList; 
if(inner != null) 
{ 
    //this is what you are looking for 
    //you can then iterate the inner array list and get the int[,] you saved 
} 
} 

그러나, List<ArrayList> 또는 List<List<int[,]>>를 사용하여 질문을 이해 바랍니다.

2

당신이 쉽게 생각하기 때문에 ArrayLists를 사용한다면, 그렇게하지 마십시오. 목록을 사용하십시오. 당신이 가지고 있고 (I 의심) 목록을 사용할 수있는 방법이 없기 때문에 당신은 당신은 다음과 같이해야합니다 ArrayLists를 사용하는 경우 :

foreach(ArrayList innerList in foundMatches) 
{ 
    foreach(SomeClassThatYouAddToInnerLists item in innerlist) 
    { 
    //use item; 
    } 
} 

유형이이다 무엇 이건 SomeClassThatYouAddToInnerLists 교체를 내부 ArrayList에 추가하고 있습니다. (mBoard의 유형입니다.)

목록을 사용하면 목록의 모든 유형이 목록에 포함되어 있는지 분명합니다. List<List<int>>은 작업하기가 쉽습니다. 숫자 목록입니다.

관련 문제