【问题标题】:Check adjacency in 2d array C# Unity3D检查二维数组 C# Unity3D 中的邻接性
【发布时间】:2016-12-01 20:52:52
【问题描述】:

我正在 Unity 3D 4.7.2f 上创建一个游戏,其中我有一个可以通过触摸它们“连接”的项目网格。如果选择了 3 个或更多,它们就会消失,如果你玩过 Best Fiends,你就会明白这一点。我已经实现了大部分游戏逻辑,我似乎无法提取的唯一部分是一种检查游戏是否可玩的方法(即,至少有 3 个瓷砖彼此相邻)。我需要这些信息,这样我就可以将这些项目打乱成一个可玩的集合。无论如何,这是我的协程,用于检查棋盘是否可玩:

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”数组包含游戏对象(附加了 TileObject 脚本),每个游戏内项目都基于该对象。 TileObject 包含它自己的名称,并且它们也有它们的列号和行号(用于 isAdjacent 函数)。我很确定我的逻辑问题出在“isPlayable”协程中,但我不知道为什么。基本上它需要检查 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"

【问题讨论】:

标签: c# arrays unity3d


【解决方案1】:

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

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

并在各个方向进行验证:

public bool verifyDown(int column, int x, int row, int y){
        bool meetsRequirements = false;
        if(column == x){
            if(y == 0){
                meetsRequirements =  false;
            } else if((y - row) == 1){
                meetsRequirements = true;
            } else {
                meetsRequirements = false;
            }
        } else {
            meetsRequirements = false;
        }
        return meetsRequirements;
    }

    public bool verifyUp(int column, int x, int row, int y){
        bool meetsRequirements = false;
        if(column == x){
            if(y >= rows - 1){
                meetsRequirements = false;
            } else if((row - y) == 1){
                meetsRequirements = true;
            } else {
                meetsRequirements = false;
            }
        } else {
            meetsRequirements = false;
        }
        return meetsRequirements;
    }

    public bool verifyLeft(int column, int x, int row, int y){
        bool meetsRequirements = false;
        if(row == y){
            if(x == 0){
                meetsRequirements = false;
            } else if((x - column) == 1){
                meetsRequirements = true;
            } else {
                meetsRequirements = false;
            }
        } else {
            meetsRequirements = false;
        }
        return meetsRequirements;
    }

    public bool verifyRight(int column, int x, int row, int y){
        bool meetsRequirements = false;
        if(row == y){
            if(x == columns - 1){
                meetsRequirements = false;
            } else if((column - x) == 1){
                meetsRequirements = true;
            } else {
                meetsRequirements = false;
            }
        } else {
            meetsRequirements = false;
        }
        return meetsRequirements;
    }

    public bool verifyUpLeft(int column, int x, int row, int y){
        bool meetsRequirements = false;
        if(y > rows -1 || x == 0){
            meetsRequirements = false;
        } else if((row - y) == 1 && (x - column) == 1){
            meetsRequirements = true;
        } else {
            meetsRequirements = false;
        }
        return meetsRequirements;
    }

    public bool verifyUpRight(int column, int x, int row, int y){
        bool meetsRequirements = false;
        if(y >= rows - 1 || x == columns - 1){
            meetsRequirements = false;
        } else if((row - y) == 1 && (column - x) == 1){
            meetsRequirements = true;
        } else {
            meetsRequirements = false;
        }
        return meetsRequirements;
    }

    public bool verifyDownLeft(int columna, int x, int row, int y){
        bool meetsRequirements = false;
        if(y == 0 || x == 0){
            meetsRequirements = false;
        } else if((y - row) == 1 && (x - columna) == 1){
            meetsRequirements  = true;
        } else {
            meetsRequirements = false;
        }
        return meetsRequirements;
    }

    public bool verifyDownRight(int column, int x, int row, int y){
        bool meetsRequirements = false;
        if(y == 0 || x == columns - 1){
            meetsRequirements = false;
        } else if((y - row) == 1 && (column - x) == 1){
            meetsRequirements  = true;
        } else {
            meetsRequirements = false;
        }
        return meetsRequirements;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 2010-10-13
    • 2013-04-08
    • 2017-02-10
    • 2018-07-30
    • 1970-01-01
    相关资源
    最近更新 更多