public MineField(Vector2 position, Point tileSize, int width, int height, bool wrapAround, float mineRatio) { Position = position; TileSize = tileSize; Width = width; Height = height; WrapAround = wrapAround; MasksPerRow = width / BitMaskGrid.Size + 1; var maskAmountY = height / BitMaskGrid.Size + 1; var maskAmountTotal = MasksPerRow * maskAmountY; MineMasks = SeedMineField(width, height, tileSize, mineRatio, maskAmountTotal); RevealMasks = new BitMaskGrid[maskAmountTotal]; }
private static BitMaskGrid[] SeedMineField(int width, int height, Point tileSize, float mineRatio, int totalMasks) { mineRatio = MathHelper.Clamp(mineRatio, 0, 1); var invertSeeding = mineRatio > 0.5f; var mineMasks = new BitMaskGrid[totalMasks]; if (invertSeeding) { for (var i = 0; i < mineMasks.Length; i++) { mineMasks[i].ResetToOne(); } } var seedAmount = (int)(width * height * (invertSeeding ? 1 - mineRatio : mineRatio)); var seedValue = (uint)(invertSeeding ? 0 : 1); var mineLayer = new Random(); while (seedAmount > 0) { var x = mineLayer.Next(width); var y = mineLayer.Next(height); var maskIndex = y / BitMaskGrid.Size * (width / BitMaskGrid.Size + 1) + x / BitMaskGrid.Size; if (!(((mineMasks[maskIndex][y % BitMaskGrid.Size] & (BitMaskGrid.MostSignificantBitOnly >> (x % BitMaskGrid.Size))) == 0) ^ invertSeeding)) { continue; } mineMasks[maskIndex][y % BitMaskGrid.Size] ^= BitMaskGrid.MostSignificantBitOnly >> x; //Console.WriteLine(mask[y % BitMaskGrid.Size]); seedAmount--; } return(mineMasks); }