Je suis sûr que la première réponse sera "wtf, n'utilisez pas d'arraylists!", Mais j'essaie juste de faire fonctionner ça de toutes les façons possibles.Obtenir l'accès à un ArrayList dans une ArrayList
C'est fondamentalement le "match looker" pour un jeu de match 3. J'ai des difficultés à accéder aux données de match dans la liste de match. Voir ci-dessous ..
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;
}
Bonne nouvelle, je sais qu'il trouve les correspondances correctement. Le nombre de correspondances détectées à l'aide du journal de débogage correspond à ce qui se passe à l'écran. Yay! Mais, j'ai besoin d'avoir accès à ces données pour pouvoir les comparer au tableau (mBoard [col, ligne]) et retirer ces objets du plateau de jeu.
La boucle 'foreach' dans findandremovematches donne une erreur sur le moulage. Toute aide avec ça?
merci!
Il serait utile d'avoir le message d'erreur exact. – mobiGeek
Vous avez eu tort à propos de la première réponse :) –
wtf, n'utilisez pas d'arraylists! –