private List <AxialCoord> GetNeighboursWithBubbleNumber(AxialCoord testedBubbleCoord, int bubbleNumber) { var neighbourAxialCoords = HexHelperService.GetNeighbours(testedBubbleCoord); var canBeMergedNeighbourAxialCoords = new List <AxialCoord>(); foreach (var axialCoord in neighbourAxialCoords) { var arrayIndices = HexHelperService.GetArrayIndices(axialCoord); //bounds check if (arrayIndices.x >= _boardSize.x || arrayIndices.y >= _boardSize.y || arrayIndices.x < 0 || arrayIndices.y < 0) { continue; } //null check if (_hexMap[arrayIndices.x, arrayIndices.y] == null) { continue; } //number check if (_hexMap[arrayIndices.x, arrayIndices.y].bubbleNumber.Value != bubbleNumber) { continue; } canBeMergedNeighbourAxialCoords.Add(axialCoord); } return(canBeMergedNeighbourAxialCoords); }
protected override void Execute(List <GameEntity> entities) { var boardSize = _contexts.game.boardSize.Value; var hexMap = HexStorageService.UpdateHexMap(); for (var x = 0; x < boardSize.x; x++) { for (var y = 0; y < boardSize.y; y++) { var bubble = hexMap[x, y]; if (bubble == null) { continue; } var bubbleNumber = bubble.bubbleNumber.Value; if (bubbleNumber < 2048) { continue; } var bubbleCoord = bubble.axialCoord.Value; var neighbours = HexHelperService.GetNeighbours(bubbleCoord); foreach (var neighbourCoord in neighbours) { var arrayIndices = HexHelperService.GetArrayIndices(neighbourCoord); //bounds check if (arrayIndices.x >= boardSize.x || arrayIndices.y >= boardSize.y || arrayIndices.x < 0 || arrayIndices.y < 0) { continue; } //null check if (hexMap[arrayIndices.x, arrayIndices.y] == null) { continue; } ExplodeBubble(hexMap[arrayIndices.x, arrayIndices.y], false); } ExplodeBubble(bubble, true); } } }
private bool IsConnected(AxialCoord rootCoord) { var ceilingCoords = _contexts.game.ceilingCoords.Value; var visited = new bool[_boardSize.x, _boardSize.y]; for (var x = 0; x < _boardSize.x; x++) { for (var y = 0; y < _boardSize.y; y++) { visited[x, y] = false; } } var queue = new Queue <AxialCoord>(); queue.Enqueue(rootCoord); var rootIndices = HexHelperService.GetArrayIndices(rootCoord); visited[rootIndices.x, rootIndices.y] = true; while (queue.Count > 0) { var testCord = queue.Dequeue(); if (ceilingCoords.Any(c => c.Q == testCord.Q && c.R == testCord.R)) { return(true); } var neighbours = HexHelperService.GetNeighbours(testCord); foreach (var neighbourCoord in neighbours) { var arrayIndices = HexHelperService.GetArrayIndices(neighbourCoord); //bounds check if (arrayIndices.x >= _boardSize.x || arrayIndices.y >= _boardSize.y || arrayIndices.x < 0 || arrayIndices.y < 0) { continue; } //visited check if (visited[arrayIndices.x, arrayIndices.y]) { continue; } visited[arrayIndices.x, arrayIndices.y] = true; //null check if (_hexMap[arrayIndices.x, arrayIndices.y] == null) { continue; } queue.Enqueue(neighbourCoord); } } return(false); }
private List <AxialCoord> GetBubbleCluster(AxialCoord bubbleCoord) { var visited = new bool[_boardSize.x, _boardSize.y]; for (var x = 0; x < _boardSize.x; x++) { for (var y = 0; y < _boardSize.y; y++) { visited[x, y] = false; } } var cluster = new List <AxialCoord>(); var queue = new Queue <AxialCoord>(); var rootIndices = HexHelperService.GetArrayIndices(bubbleCoord); var rootBubble = _hexMap[rootIndices.x, rootIndices.y]; var bubbleNumber = rootBubble.bubbleNumber.Value; cluster.Add(bubbleCoord); queue.Enqueue(bubbleCoord); visited[rootIndices.x, rootIndices.y] = true; while (queue.Count > 0) { var testCord = queue.Dequeue(); var neighbours = HexHelperService.GetNeighbours(testCord); foreach (var neighbourCoord in neighbours) { var arrayIndices = HexHelperService.GetArrayIndices(neighbourCoord); //bounds check if (arrayIndices.x >= _boardSize.x || arrayIndices.y >= _boardSize.y || arrayIndices.x < 0 || arrayIndices.y < 0) { continue; } //null check if (_hexMap[arrayIndices.x, arrayIndices.y] == null) { continue; } //visited check if (visited[arrayIndices.x, arrayIndices.y]) { continue; } visited[arrayIndices.x, arrayIndices.y] = true; //number check if (_hexMap[arrayIndices.x, arrayIndices.y].bubbleNumber.Value != bubbleNumber) { continue; } queue.Enqueue(neighbourCoord); cluster.Add(neighbourCoord); } } return(cluster); }