检查二维数组中的邻接C#Unity3D

时间:2016-12-01 20:52:53

标签: c# arrays unity3d

我在团结3D 4.7.2f上创建一个游戏,我有一个项目网格,我可以通过触摸它们“连接”。如果选择了3个或更多,他们会消失,如果你玩过最好的恶魔,你就会明白。我已经实现了大部分的游戏逻辑,我似乎无法撤出的唯一部分是检查游戏是否可玩的方式(I.E.彼此相邻的行至少有3个瓦片)。我需要这些信息,所以我可以将这些项目加入到一个可玩的集合中。无论如何,这是我的Coroutine检查董事会是否可玩:

IEnumerator isPlayable(){
    yield return new WaitForSeconds(0f);
        bool playable = false;
        TileObject first = null;
        TileObject next = null;
        int inLine = 0;
        foreach(GameObject GO in baseItems){
            if(playable)
                break;
            first = null;
            next = null;
            inLine = 0;
            for(int i = 0; i < rows; i++){
                for(int j = 0; j < columns; j++){
                    if(visibleItems[i,j].name.Equals(GO.gameObject.GetComponent<TileObject>().name)){
                        if(first == null){
                            first = visibleItems[i,j];
                            if(inLine == 0)
                                inLine = 1;
                        } else if(next == null){
                            next = visibleItems[i,j];
                            if(isAdjacent(first, false, next)){
                                inLine++;
                                if(inLine >= 3){
                                    i = rows + 1;
                                    j = columns + 1;
                                    playable = true;
                                    break;
                                } else {
                                    first = next;
                                    next = null;
                                }
                            } else {
                                first = next;
                                next = null;
                            }
                        }
                    }
                }
            }
            if(playable)
                Debug.Log(GO.name + " Yes");
            else
                Debug.Log(GO.name + " No");
        }
}

如您所见,我也称我的“isAdjacent”功能,如下所示:

public  bool isAdjacent(TileObject tileObj, bool checkChosen = true, TileObject next = null){
    bool meetsRequirements = false;
    if(checkChosen){
        if(chosenItems != null && chosenItems.Count > 0){
            if(chosenItems.ToArray()[0].name.Equals(tileObj.name)){
                foreach(TileObject item in chosenItems){
                    meetsRequirements =     verifyDown(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                            verifyUp(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                            verifyLeft(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                            verifyRight(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                            verifyUpLeft(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                            verifyUpRight(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                            verifyDownLeft(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                            verifyDownRight(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow());
                }
            } else {
                meetsRequirements = false;
            }
        } else {
            meetsRequirements = false;
        }
    } else {
        meetsRequirements =     verifyRight(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                verifyDownRight(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                verifyDown(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                verifyDownLeft(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                verifyLeft(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                verifyUpLeft(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                verifyUp(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                verifyUpRight(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow());
    }
    return meetsRequirements;
}

我知道“isAdjacent”功能正常工作,因为它与我用来检查所选(点击/点击)项目是否在同一类型的其他项目附近相同。 “baseItems”数组包含每个游戏内项目所基于的GameObjects(其中包含TileObject Script)。 TileObject包含它自己的名称,它们也有列和行号(用于isAdjacent函数)。我很确定我的逻辑问题在于“isPlayable”Coroutine,但我不知道为什么。基本上它需要检查3种(例如“蓝色项目”)是否在一行(在任何方向上,只要它们相邻),但是当它不是可玩的游戏时,它有时会显示“是”或它是“不”。事情并非总是如此,但确实会发生。有人能帮我吗?希望我能准确地解释自己。

编辑:作为示例,我添加此图片:

As you can see, green should have said "Yes"

In this other case, Blue should've said "No" and go all the way to "Yellow" to say "Yes"

1 个答案:

答案 0 :(得分:0)

已经有一段时间了,但为了以防万一,这是我的解决方案:

transferManager.download(downloadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in

if let error = task.error as? NSError {
    if error.domain == AWSS3TransferManagerErrorDomain, let code = AWSS3TransferManagerErrorType(rawValue: error.code) {
        switch code {
        case .cancelled, .paused:
            break
        default:
            print("Error downloading: \(downloadRequest.key) Error: \(error)")
        }
    } else {
        print("Error downloading: \(downloadRequest.key) Error: \(error)")
    }
    return nil
}
print("Download complete for: \(downloadRequest.key)")
let downloadOutput = task.result
return nil
})

检查邻接:

IEnumerator isPlayable(){
        yield return new WaitForEndOfFrame();
//      StartCoroutine(ClearConsole());
        string debugString = "";
        string debugLine = "En Linea: ";
        bool playable = false;
        // First tile to be compared
        TileObject first = null;
        // Next tile to be compared
        TileObject next = null;
        // Last tile in the 2D Array, used to determine if a loop should end or restart
        TileObject last = null;
        // Previous tile that was compared, saves the first tile after a next one was determined to be adjacent, this is to prevent cicled and false Trues
        TileObject previous = null;
        // Determines how many tiles are in line in the visible 2D Array
        int inLine = 0;
        foreach(GameObject GO in baseItems){
            if(playable)
                break;
            debugString = debugString + GO.gameObject.GetComponent<TileObject>().name + ": \n";
//          Debug.Log(GO.gameObject.GetComponent<TileObject>().name);
            last = getLastInArray(GO.name);
            previous = null;
            first = null;
            next = null;
            inLine = 0;
            for(int i = 0; i < rows; i++){
//              Debug.Log("i: " + i);
                for(int j = 0; j < columns; j++){
//                  Debug.Log("j: " + j);
                    if(visibleItems[i,j].name.Equals(GO.gameObject.GetComponent<TileObject>().name)){//GO.gameObject.GetComponent<TileObject>().name
                        if(first == null){
                            first = visibleItems[i,j];
                            if(inLine == 0)
                                inLine = 1;
                        } 
                        for(int a = 0; a < rows; a++){
//                          Debug.Log("a: " + a);
                            for(int b = 0; b < columns; b++){
//                              Debug.Log("b: " + b);
                                if(visibleItems[a,b].name.Equals(GO.gameObject.GetComponent<TileObject>().name)){
                                    if(visibleItems[a,b].Equals(first)){
                                        continue;
                                    } else {
                                        if(next == null){
                                            next = visibleItems[a,b];
                                            if(next.Equals(previous)){
                                                continue;
                                            } else {
                                                debugString = debugString + "Es " + first.name + "(" + first.getRow() + "," + first.getColumn() + ") adyacente a ";
                                                debugString = debugString + next.name + "(" + next.getRow() + "," + next.getColumn() + ")?: ";
                                                if(isAdjacent(first, false, next)){
                                                    debugString = debugString + " Si\n";
                                                    previous = first;
                                                    first = next;
                                                    next = null;
                                                    a = i;
                                                    b = j;
//                                                  Debug.Log("Es Adyacente");
                                                    inLine++;
                                                    if(inLine >= 3){
//                                                      Debug.Log("Es mayor igual a 3");
//                                                      Debug.Log(debugString);
                                                        debugLine = debugLine + inLine;
//                                                      Debug.Log(debugLine);
                                                        a = rows + 1;
                                                        b = columns + 1;
                                                        i = a;
                                                        j = b;
                                                        playable = true;
                                                        break;
                                                    }
                                                } else {
//                                                  Debug.Log("No es adyacente");
                                                    debugString = debugString + " No\n";
//                                                      Debug.Log(debugString);
                                                    if(next.Equals(last)){
//                                                      Debug.Log("Next es el ultimo");
                                                        previous = null;
                                                        first = null;
                                                        next = null;
                                                        inLine = 0;
                                                        break;
    //                                                      Debug.Log("i: " + i);
    //                                                      Debug.Log("j: " + j);
                                                    } else {
//                                                      Debug.Log("Next no es el ultimo");
                                                        next = null;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if(!playable){
                debugLine = "En Linea: ";
                debugLine = debugLine + inLine.ToString();
//              Debug.Log(debugString);
//              Debug.Log(debugLine);
                debugString = "";
                debugLine = "En Linea: ";
            }
//          if(playable)
//              Debug.Log(GO.gameObject.GetComponent<TileObject>().name + " Yes"); //GO.name
//          else
//              Debug.Log(GO.gameObject.GetComponent<TileObject>().name + " No"); //GO.name
        }
        if(!playable){
//          Debug.Log("No es Jugable");
            allowGame = false;
        } else {
//          Debug.Log("Es jugable");
            allowGame = true;
        }

        if(allowGame){
            if(!CandyCrushMain.Script.getStart()){
                CandyCrushMain.Script.gameStart();
                CandyCrushMain.Script.setEndTime();
            }
        }
    }

并在每个方向进行验证:

public  bool isAdjacent(TileObject tileObj, bool checkChosen = true, TileObject next = null){
        bool meetsRequirements = false;
        if(checkChosen){
            if(chosenItems != null && chosenItems.Count > 0){
                if(chosenItems.ToArray()[0].name.Equals(tileObj.name)){
                    foreach(TileObject item in chosenItems){
                        meetsRequirements =     verifyDown(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                                verifyUp(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                                verifyLeft(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                                verifyRight(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                                verifyUpLeft(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                                verifyUpRight(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                                verifyDownLeft(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow()) || 
                                                verifyDownRight(item.getColumn(), tileObj.getColumn(), item.getRow(), tileObj.getRow());
                    }
                } else {
                    meetsRequirements = false;
                }
            } else {
                meetsRequirements = false;
            }
        } else {
            meetsRequirements =     verifyRight(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                    verifyDownRight(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                    verifyDown(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
//                                  verifyDownLeft(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                    verifyLeft(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                    verifyUpLeft(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                    verifyUp(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow()) || 
                                    verifyUpRight(next.getColumn(), tileObj.getColumn(), next.getRow(), tileObj.getRow());
        }
        return meetsRequirements;
    }