//------------------------------------------------------------------------- // 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); } } } }
await Redis.GetDatabase().HashSetAsync(mapKey, hashEntries);