IslandInfo GetShapeIslandCount(Vector2 startPos, BoardLogic board) { IslandInfo info = new IslandInfo(); //NOTE: This is since shape can be blown apart we want to still be able to move a block on their //own island. So the count isn't the shapeCount but only a portion in this count. So we first search //how big the island is and match against that. bool[] boardArray = new bool[board.boardWidth * board.boardHeight]; VisitedQueue sentinel = new VisitedQueue(); sentinel.next = sentinel.prev = sentinel; addToQueryList(sentinel, startPos, boardArray, board); VisitedQueue queryAt = sentinel.next; Assert.IsTrue(queryAt != sentinel); while (queryAt != sentinel) { Vector2 pos = queryAt.pos; Assert.IsTrue(info.count < info.poses.Length); info.poses[info.count++] = pos; addToQueryList(sentinel, new Vector2(1, 0) + pos, boardArray, board); addToQueryList(sentinel, new Vector2(-1, 0) + pos, boardArray, board); addToQueryList(sentinel, new Vector2(0, 1) + pos, boardArray, board); addToQueryList(sentinel, new Vector2(0, -1) + pos, boardArray, board); queryAt = queryAt.next; } if (info.count > this.count) { //printf("Count At; %d\n", info.count); //printf("Shape Count: %d\n", shape->count); } Assert.IsTrue(info.count <= this.count); return(info); }
private void addToQueryList(VisitedQueue sentinel, Vector2 pos, bool[] boardArray, BoardLogic board) { if ((int)pos.x >= 0 && (int)pos.x < board.boardWidth && (int)pos.y >= 0 && (int)pos.y < board.boardHeight) { int boardIndex = (((int)pos.y) * board.boardWidth) + (int)pos.x; bool visited = boardArray[boardIndex]; if (!visited && board.GetBoardState(pos) == BoardState.BOARD_SHAPE) { boardArray[boardIndex] = true; VisitedQueue queue = new VisitedQueue(); queue.pos = pos; //add to the search queue Assert.IsTrue(sentinel.prev.next == sentinel); queue.prev = sentinel.prev; queue.next = sentinel; sentinel.prev.next = queue; sentinel.prev = queue; } } }