2016-12-01 4 views
1

私はunity 3D 4.7.2fでゲームを作成しています。ここでは、タッチして「接続」できるアイテムのグリッドがあります。 3つ以上が選択されていれば、それらが消えます。あなたがBest Fiendsをプレイしていれば、そのアイデアを得るでしょう。私はすでにゲームロジックのほとんどを実装していますが、私が引き出すことができないような部分は、ゲームがプレイできるかどうかをチェックする方法です(I.E.では、隣り合わせに少なくとも3つのタイルが並んでいます)。私はこの情報を必要とするので、アイテムを再生可能なセットにスクランブルすることができます。とにかく、ここでボードが再生可能であるかどうかを確認する私のコルーチンは次のとおりです。2d配列の隣接関係を確認するUnity3D

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スクリプトがアタッチされています)が含まれています。 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"

+0

これはあなたに正しい方向にプッシュを与えるかもしれない:http://gamedev.stackexchange.com/questions/48869/3-in-a-row-or -more-logic – Abion47

答えて

0

それはしばらくして、しかし、念のために、ここに私のソリューションですです:へ

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; 
    } 
関連する問題