// Method that loads one layer void LoadLevelFromString(int layer, string content) { // Split our layer on rows by the new lines (\n) List <string> lines = new List <string>(content.Split('\n')); // Place each block in order in the correct x and y position levelInfo data = new levelInfo(); data.dataLst = new List <List <blockInfo> >(); for (int i = 0; i < lines.Count; i++) { string[] blockIDs = lines[i].Split(','); // Debug.Log(">>>>>>>>>>>>level lines i:"+i+" "+lines[i]); List <blockInfo> rowdata = new List <blockInfo>(); data.dataLst.Add(rowdata); bool haveData = false; for (int j = 0; j < blockIDs.Length - 1; j++) { blockInfo colData = new blockInfo(); if (blockIDs[j] != "EMPTY") { //Debug.Log(">>>>>>>>>>>>>blockIDs:"+j+" "+blockIDs[j]); // 名字|分数|角度|类型 string[] temp = blockIDs[j].Split('|'); colData.row = j; colData.col = lines.Count - i - 1; if (temp[1] != "") { colData.score = int.Parse(temp[1]); } colData.angle = float.Parse(temp[2]); colData.type = int.Parse(temp[3]); haveData = true; } else { colData.type = -1; } rowdata.Add(colData); } if (haveData) { data.maxRow = i + 1; } } allData[curLevel] = data; }
//------------------------------------------------------------------------- // Find adjacent blocks information like indices for above below left right public blockInfo GetBlocks(int row, int col) { blockInfo blocks; int n; blocks = new blockInfo(); if (row == 2 && col == 2) { Console.WriteLine(""); } //get adjacent elements for left right above below if (row == 0) { n = layout[row].Length; if (col != 0) { //left blocks.left = new mapKey(); blocks.left.xPosition = row; blocks.left.yPosition = col - 1; } if (col != n - 1) { //right blocks.right = new mapKey(); blocks.right.xPosition = row; blocks.right.yPosition = col + 1; } if (row != layout.Length - 1) { //below if (layout[row + 1].Length > col) { blocks.below = new mapKey(); blocks.below.xPosition = row + 1; blocks.below.yPosition = col; } } } else if (row == layout.Length - 1) { if (col != 0) { //left blocks.left = new mapKey(); blocks.left.xPosition = row; blocks.left.yPosition = col - 1; } if (col != layout[row].Length - 1) { //right blocks.right = new mapKey(); blocks.right.xPosition = row; blocks.right.yPosition = col + 1; } if (row != 0) { //above if (layout[row - 1].Length > col) { blocks.above = new mapKey(); blocks.above.xPosition = row - 1; blocks.above.yPosition = col; } } } else { if (col != 0) { //left blocks.left = new mapKey(); blocks.left.xPosition = row; blocks.left.yPosition = col - 1; } if (col != layout[row].Length - 1) { //right blocks.right = new mapKey(); blocks.right.xPosition = row; blocks.right.yPosition = col + 1; } if (row != 0) { //above if (layout[row - 1].Length > col) { blocks.above = new mapKey(); blocks.above.xPosition = row - 1; blocks.above.yPosition = col; } } if (row != layout.Length) { //below if (layout[row + 1].Length > col) { blocks.below = new mapKey(); blocks.below.xPosition = row + 1; blocks.below.yPosition = col; } } } return(blocks); }
//------------------------------------------------------------------------- // Find largest contiguous blocks public void FindBlocks() { /* IMPLEMENT ME -- See README-Instructions */ /*********** Algorithm to get blocks following the BFS approach *******************/ storage = new Dictionary <char, List <List <mapKey> > >(); Queue <mapKey> Qpieces; bool[][] visited = new bool[layout.Length][]; //keep track of visited pieces to avoid duplicates and infinite loops for (int i = 0; i < visited.Length; i++) // loop to initialize all visited pieces false { visited[i] = new bool[layout[i].Length]; for (int j = 0; j < layout[i].Length; j++) { visited[i][j] = false; } } for (int i = 0; i < layout.Length; i++) { for (int j = 0; j < layout[i].Length; j++) { if (visited[i][j] == false && layout[i][j].type != ' ') //if piece is not space and not visisted { if (!storage.ContainsKey(layout[i][j].type)) //check for valid entry ie if the entry exists { List <List <mapKey> > positionList = new List <List <mapKey> >(); // a new list structure for each entry storage.Add(layout[i][j].type, positionList); } mapKey key = new mapKey(); key.xPosition = i; key.yPosition = j; // make key object for current entry's position indices Qpieces = new Queue <mapKey>(); Qpieces.Enqueue(key); List <mapKey> mapList = new List <mapKey>(); //loop to get all adjacent pieces of the current key; left; right; above; below; while (Qpieces.Count != 0) // enqueue all adjacent pieces when the type is same; and dequeue untill adjacent pieces are not visited { mapKey mkey = new mapKey(); mkey = Qpieces.Dequeue(); if (visited[mkey.xPosition][mkey.yPosition] == false) { mapList.Add(mkey); blockInfo blocks = GetBlocks(mkey.xPosition, mkey.yPosition); if (blocks.left != null && layout[blocks.left.xPosition][blocks.left.yPosition].type == layout[mkey.xPosition][mkey.yPosition].type) { Qpieces.Enqueue(blocks.left); } if (blocks.right != null && layout[blocks.right.xPosition][blocks.right.yPosition].type == layout[mkey.xPosition][mkey.yPosition].type) { Qpieces.Enqueue(blocks.right); } if (blocks.above != null && layout[blocks.above.xPosition][blocks.above.yPosition].type == layout[mkey.xPosition][mkey.yPosition].type) { Qpieces.Enqueue(blocks.above); } if (blocks.below != null && layout[blocks.below.xPosition][blocks.below.yPosition].type == layout[mkey.xPosition][mkey.yPosition].type) { Qpieces.Enqueue(blocks.below); } visited[mkey.xPosition][mkey.yPosition] = true; } } storage[layout[i][j].type].Add(mapList); } } } }