// This method will shuffle the board if it becomes deadlocked (recursive method) private void ShuffleBoard() { // Create a list of gameobjects List <GameObject> newBoard = new List <GameObject>(); // Add every piece to list for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (allShapeTiles[i, j] != null) { newBoard.Add(allShapeTiles[i, j]); } } } // for every spot on the board for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { // If this spot shouldnt be blank if (!blankSpaces[i, j] && !concreteTiles[i, j] && !slimeTiles[i, j]) { // Pick a rand number int pieceToUse = Random.Range(0, newBoard.Count); // make sure the while loop doesnt become infinite int maxIterations = 0; // Check to see if there are matches, if there are, choose a different shape tile while (MatchesAt(i, j, newBoard[pieceToUse]) && maxIterations < 100) { // Choose a new tile to instantiate pieceToUse = Random.Range(0, newBoard.Count); // Increase the maxIterations by 1 maxIterations++; Debug.Log(maxIterations); } // Piece container ShapeTile piece = newBoard[pieceToUse].GetComponent <ShapeTile>(); // Reset maxIterations maxIterations = 0; // Set the piece column to i piece.column = i; // Set the piece row to j piece.row = j; // Fill in the tiles array with new piece allShapeTiles[i, j] = newBoard[pieceToUse]; // Remove from list newBoard.Remove(newBoard[pieceToUse]); } } } // check if the board is still deadlocked if (IsDeadlocked()) { ShuffleBoard(); } }
// Helper method to find ColumnBombs private List <GameObject> IsColumnBomb(ShapeTile tile1, ShapeTile tile2, ShapeTile tile3) { List <GameObject> currentTiles = new List <GameObject>(); // Check to see if the middle piece is a row bomb if (tile1.isColumnBomb) { currentMatches.Union(GetColumnPieces(tile1.column)); } // Check to see if the up piece is a row bomb if (tile2.isColumnBomb) { currentMatches.Union(GetColumnPieces(tile2.column)); } // Check to see if the down piece is a row bomb if (tile3.isColumnBomb) { currentMatches.Union(GetColumnPieces(tile3.column)); } return(currentTiles); }
// Helper method to find Adjacent bombs private List <GameObject> IsAdjacentBomb(ShapeTile tile1, ShapeTile tile2, ShapeTile tile3) { List <GameObject> currentTiles = new List <GameObject>(); // Check to see if the middle piece is an adjacent bomb if (tile1.isAdjacentBomb) { currentMatches.Union(GetAdjacentPieces(tile1.column, tile1.row)); } // Check to see if the up piece is an adjacent bomb if (tile2.isAdjacentBomb) { currentMatches.Union(GetAdjacentPieces(tile2.column, tile2.row)); } // Check to see if the down piece is an adjacent bomb if (tile3.isAdjacentBomb) { currentMatches.Union(GetAdjacentPieces(tile3.column, tile3.row)); } return(currentTiles); }
// Helper method - Row Pieces List <GameObject> GetRowPieces(int row) { // Initialize list List <GameObject> tiles = new List <GameObject>(); // Cycle through tiles in array and see if they're in this list for (int i = 0; i < board.width; i++) { if (board.allShapeTiles[i, row] != null) { ShapeTile tile = board.allShapeTiles[i, row].GetComponent <ShapeTile>(); // Check to see if one of the tiles happens to be a column bomb, if so -> union those pieces to the tiles list if (tile.isColumnBomb) { tiles.Union(GetColumnPieces(i)).ToList(); } tiles.Add(board.allShapeTiles[i, row]); tile.isMatched = true; } } return(tiles); }
// Check to make bombs public void CheckBombs(MatchType matchType) { // Did the player move something if (board.currentTile != null) { // Is the piece they moved matched? if (board.currentTile.isMatched && board.currentTile.tag == matchType.colour) { // Make it unmatched board.currentTile.isMatched = false; // Right Swipe if ((board.currentTile.swipeAngle > -45 && board.currentTile.swipeAngle <= 45) // Left Swipe || board.currentTile.swipeAngle < -135 || board.currentTile.swipeAngle >= 135) { // MAKE ROW BOMB board.currentTile.MakeRowBomb(); } // else its an up or down swipe else { // MAKE COLUMN BOMB board.currentTile.MakeColumnBomb(); } #region OLD BOMB CODE // Decide which bomb we're going to place /* * int typeOfBomb = Random.Range(0, 100); * if (typeOfBomb < 50) * { * // Make a row bomb * board.currentTile.MakeRowBomb(); * } * else if (typeOfBomb >= 50) * { * // Make a column bomb * board.currentTile.MakeColumnBomb(); * } */ #endregion } // Is the other piece matched? else if (board.currentTile.otherShapeTile != null) { ShapeTile otherTile = board.currentTile.otherShapeTile.GetComponent <ShapeTile>(); if (otherTile.isMatched && otherTile.tag == matchType.colour) { otherTile.isMatched = false; // Right Swipe if ((board.currentTile.swipeAngle > -45 && board.currentTile.swipeAngle <= 45) // Left Swipe || board.currentTile.swipeAngle < -135 || board.currentTile.swipeAngle >= 135) { // MAKE ROW BOMB otherTile.MakeRowBomb(); } // else its an up or down swipe else { // MAKE COLUMN BOMB otherTile.MakeColumnBomb(); } } } } }
private IEnumerator FindAllMatchesCo() { //yield return new WaitForSeconds(.1f); yield return(null); for (int i = 0; i < board.width; i++) { for (int j = 0; j < board.height; j++) { GameObject currentTile = board.allShapeTiles[i, j]; if (currentTile != null) { ShapeTile currentTileTile = currentTile.GetComponent <ShapeTile>(); if (i > 0 && i < board.width - 1) { GameObject leftTile = board.allShapeTiles[i - 1, j]; GameObject rightTile = board.allShapeTiles[i + 1, j]; if (leftTile != null && rightTile != null) { ShapeTile rightTileTile = leftTile.GetComponent <ShapeTile>(); ShapeTile leftTileTile = leftTile.GetComponent <ShapeTile>(); if (leftTile != null && rightTile != null) { // If the left and right tags are equal to the current tiles tag if (leftTile.tag == currentTile.tag && rightTile.tag == currentTile.tag) { // Check for row bombs currentMatches.Union(IsRowBomb(leftTileTile, currentTileTile, rightTileTile)); // Check for column bombs currentMatches.Union(IsColumnBomb(leftTileTile, currentTileTile, rightTileTile)); // Check for Adjacent bombs currentMatches.Union(IsAdjacentBomb(leftTileTile, currentTileTile, rightTileTile)); GetNearbyPieces(leftTile, currentTile, rightTile); } } } } if (j > 0 && j < board.height - 1) { GameObject upTile = board.allShapeTiles[i, j + 1]; GameObject downTile = board.allShapeTiles[i, j - 1]; if (upTile != null && downTile != null) { ShapeTile downTileTile = downTile.GetComponent <ShapeTile>(); ShapeTile upTileTile = upTile.GetComponent <ShapeTile>(); if (upTile != null && downTile != null) { if (upTile.tag == currentTile.tag && downTile.tag == currentTile.tag) { // Check for column bombs currentMatches.Union(IsColumnBomb(upTileTile, currentTileTile, downTileTile)); // Check for row bombs currentMatches.Union(IsRowBomb(upTileTile, currentTileTile, downTileTile)); // Check for Adjacent bombs currentMatches.Union(IsAdjacentBomb(upTileTile, currentTileTile, downTileTile)); GetNearbyPieces(upTile, currentTile, downTile); } } } } } } } }
private void CheckToMakeBombs() { // how many objects are in findMatches/ currenmatcher if (findMatches.currentMatches.Count > 3) { // what type of match has been made MatchType typeOfMatch = ColumnOrRow(); if (typeOfMatch.type == 1) { // Make a colour bomb // Is the current tile matched? // Unmatch it and turn it into a colour bomb if (currentTile != null && currentTile.isMatched && currentTile.tag == typeOfMatch.colour) { currentTile.isMatched = false; currentTile.MakeColourBomb(); } else { if (currentTile.otherShapeTile != null) { ShapeTile otherTile = currentTile.otherShapeTile.GetComponent <ShapeTile>(); if (otherTile.isMatched && otherTile.tag == typeOfMatch.colour) { otherTile.isMatched = false; otherTile.MakeColourBomb(); } } } } else if (typeOfMatch.type == 2) { // Make an adjacent bomb // Is the current tile matched? // Unmatch it and turn it into a colour bomb if (currentTile != null && currentTile.isMatched && currentTile.tag == typeOfMatch.colour) { currentTile.isMatched = false; currentTile.MakeAdjacentBomb(); } else { if (currentTile.otherShapeTile != null) { ShapeTile otherTile = currentTile.otherShapeTile.GetComponent <ShapeTile>(); if (otherTile.isMatched && otherTile.tag == typeOfMatch.colour) { otherTile.isMatched = false; otherTile.MakeAdjacentBomb(); } } } } else if (typeOfMatch.type == 3) { findMatches.CheckBombs(matchType); } #region OLD CODE /* * if (findMatches.currentMatches.Count == 4 || findMatches.currentMatches.Count == 7) * { * findMatches.CheckBombs(); * } * if (findMatches.currentMatches.Count == 5 || findMatches.currentMatches.Count == 8) * { * if (ColumnOrRow()) * { * // Make a colour bomb * // Is the current tile matched? * // Unmatch it and turn it into a colour bomb * if (currentTile != null) * { * if (currentTile.isMatched) * { * if (!currentTile.isColourBomb) * { * currentTile.isMatched = false; * currentTile.MakeColourBomb(); * } * } * else { * if (currentTile.otherShapeTile != null) * { * ShapeTile otherTile = currentTile.otherShapeTile.GetComponent<ShapeTile>(); * if (otherTile.isMatched) * { * if (!otherTile.isColourBomb) * { * otherTile.isMatched = false; * otherTile.MakeColourBomb(); * } * } * } * } * } * } else { * // Make an adjacent bomb * // Is the current tile matched? * // Unmatch it and turn it into a colour bomb * if (currentTile != null) * { * if (currentTile.isMatched) * { * if (!currentTile.isAdjacentBomb) * { * currentTile.isMatched = false; * currentTile.MakeAdjacentBomb(); * } * } * else * { * if (currentTile.otherShapeTile != null) * { * ShapeTile otherTile = currentTile.otherShapeTile.GetComponent<ShapeTile>(); * if (otherTile.isMatched) * { * if (!otherTile.isAdjacentBomb) * { * otherTile.isMatched = false; * otherTile.MakeAdjacentBomb(); * } * } * } * } * } * * } * } */ #endregion } }
// Check to see how many pieces in list are in one row or column private MatchType ColumnOrRow() { // make a copy of the current matches List <GameObject> matchCopy = findMatches.currentMatches as List <GameObject>; matchType.type = 0; matchType.colour = ""; // cycle through all of matchCopy and decide if a bombs needs to be made for (int i = 0; i < matchCopy.Count; i++) { // store the spaeTile[i] ShapeTile thisTile = matchCopy[i].GetComponent <ShapeTile>(); // Set a tenp variable colour equal to the matchCopy[i]'s tag string colour = matchCopy[i].tag; // store the column and row int column = thisTile.column; int row = thisTile.row; int columnMatch = 0; int rowMatch = 0; // cycle through the rest of the pieces and compare for (int j = 0; j < matchCopy.Count; j++) { // store the next tile ShapeTile nextTile = matchCopy[j].GetComponent <ShapeTile>(); // is the next tile the same as this tile if (nextTile == thisTile) { continue; } // check to see if it's a column match if (nextTile.column == thisTile.column && nextTile.tag == colour) { columnMatch++; } // check to see if it's a row match if (nextTile.row == thisTile.row && nextTile.tag == colour) { rowMatch++; } } // return 1 if its colour bomb if (columnMatch == 4 || rowMatch == 4) { matchType.type = 1; matchType.colour = colour; return(matchType); // return 2 if its adjacent } else if (columnMatch == 2 && rowMatch == 2) { matchType.type = 2; matchType.colour = colour; return(matchType); // return 3 if its column or row } else if (columnMatch == 3 || rowMatch == 3) { matchType.type = 3; matchType.colour = colour; return(matchType); } } matchType.type = 0; matchType.colour = ""; return(matchType); #region OLD CODE /* * int numberHorizontal = 0; * int numberVertical = 0; * * ShapeTile firstPiece = findMatches.currentMatches[0].GetComponent<ShapeTile>(); * * if (firstPiece != null) * { * foreach (GameObject currentPiece in findMatches.currentMatches) * { * ShapeTile tile = currentPiece.GetComponent<ShapeTile>(); * if (tile.row == firstPiece.row) * { * numberHorizontal++; * } * if (tile.column == firstPiece.column) * { * numberVertical++; * } * } * } * return (numberVertical == 5 || numberHorizontal == 5); */ #endregion }