public void ShuffleBoard(int [,] board, Tile[,] tilesBoard, BoardManager boardManager, TileManager tileManager) { System.Random rand = new System.Random(); int width = board.GetLength(0); int height = board.GetLength(1); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int x = i + rand.Next(width - i); int y = j + rand.Next(height - j); tileManager.AnimateTile(tilesBoard[x, y], new Vector2(i, j), 1f); boardManager.SwapTilesOnBoard(new Vector2(x, y), new Vector2(i, j)); } } }
public void ShuffleBoard(int[,] board, Tile[,] tilesBoard, BoardManager boardManager, TileManager tileManager) { System.Random rand = new System.Random(); int height = board.GetUpperBound(1) + 1; for (int n = board.Length; n > 0;) { int k = rand.Next(n); --n; int dr = n / height; int dc = n % height; int sr = k / height; int sc = k % height; tileManager.AnimateTile(tilesBoard[sr, sc], new Vector2(dr, dc), 1f); boardManager.SwapTilesOnBoard(new Vector2(sr, sc), new Vector2(dr, dc)); } }
/// <summary> /// Initialize the game board with chosen fill method /// </summary> public IEnumerator InitializeBoard() { InitBGTiles(); //Init the board arrays m_logicalBoard = new int[width, height]; m_tilesOnBoard = new Tile[width, height]; m_matchedTiles = new List <Tile>(); m_bombPosition = new List <Tile>(); possibleMoves = new List <Vector2>(); highlightedMoves = new List <Tile>(); Random.InitState((int)System.DateTime.Now.Ticks); m_boardFillStrategy = new RandomBoardFillStrategy(); //Fill the graphical board using logical board data for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { //Get the logical filled board data based on chosen fill method int tileCode = m_boardFillStrategy.FillBoard(width, height, i, j, Constants.MAX_TILE_CODES, 0, false); //Get the appropriate tile based on tile code Tile tile_to_place = m_tileManager.GetTileFromFactory(tileCode); //Place the tile on board PlaceTilesOnBoard(tile_to_place, tile_to_place.tileData.TILE_CODE, i, j); int x = 1; //while there are matches on fill //Get new tile while (CheckMatchOnFill(i, j)) { //If there is a match / remove previous place tile and get a new tile m_tilesOnBoard[i, j].OnDeSpawnTile(); //Get new tile tileCode = m_boardFillStrategy.FillBoard(width, height, i, j, Constants.MAX_TILE_CODES, 0, true); //Get the appropriate tile based on new tile code tile_to_place = m_tileManager.GetTileFromFactory(tileCode); //Place the new tile on board PlaceTilesOnBoard(tile_to_place, tile_to_place.tileData.TILE_CODE, i, j); x++; //If max iterations passed //TODO: RESTART LOADING NEW PIECES FROM 0 if (x >= 100) { break; } } //Activate so we can animate tile tile_to_place.gameObject.SetActive(true); //Animate tile on start m_tileManager.AnimateTile(tile_to_place, new Vector2(i, j), 1f); } } //Cache the board for reloading purposes m_storedLogicalBoard = m_logicalBoard; yield return(new WaitForSeconds(1)); //Check if we have atleast 1 possible move if (IsThereAPossibleMatch()) { m_tileManager.canAcceptInputs = true; } //If not create a new level else { NewLevel(); } }