繁体   English   中英

检查2d阵列C#Unity3D中的邻接

[英]Check adjacency in 2d array C# Unity3D

我在团结3D 4.7.2f上创建了一个游戏,我有一个项目网格,我可以通过触摸它们来“连接”。 如果选择了3个或更多,他们会消失,如果你玩过最好的恶魔,你就会明白。 我已经实现了大部分游戏逻辑,我似乎无法提取的唯一部分是检查游戏是否可玩的方式(IE中至少有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种(例如“蓝色项目”)是否在一行(在任何方向上,只要它们相邻),但是当它不是可玩的游戏时,它有时会显示“是”或它是“不”。 事情并非总是如此,但确实会发生。 有人能帮我吗? 希望我能准确地解释自己。

编辑:作为一个例子,我添加此图像:

如你所见,绿色应该说“是”

在另一个案例中,蓝色应该说“不”,一直到“黄色”说“是”

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM