gpt4 book ai didi

c# - 如何计算给定索引周围 bool[,] 中的真实值?

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:07 24 4
gpt4 key购买 nike

我找到了几年前写的一个简单的扫雷程序,如果您可以优化以下代码,我很感兴趣,因为它看起来一点也不好。

以下方法用于检查索引是否超出范围并返回所请求索引的值:

private static bool IsMineOnCoords(bool[,] field, int posI, int posJ)
{
if (posI < 0 || posI > field.GetLength(0) - 1 || posJ < 0 || posJ > field.GetLength(1) - 1) return false;
return field[posI, posJ];
}

我在 8 个 if 语句中为给定索引周围的每个位置调用此方法:

int count = 0;

if (IsMineOnCoords(field, posI + 1, posJ)) count++;
if (IsMineOnCoords(field, posI - 1, posJ)) count++;
if (IsMineOnCoords(field, posI + 1, posJ + 1)) count++;
if (IsMineOnCoords(field, posI + 1, posJ - 1)) count++;
if (IsMineOnCoords(field, posI - 1, posJ + 1)) count++;
if (IsMineOnCoords(field, posI - 1, posJ - 1)) count++;
if (IsMineOnCoords(field, posI, posJ + 1)) count++;
if (IsMineOnCoords(field, posI, posJ - 1)) count++;

有没有更好的方法可以做到这一点?

最佳答案

像这样的东西应该适合你。基本上,您只需计算出周围单元格的 ij 坐标的最小值和最大值,然后循环遍历这些单元格,将 true 值(但跳过传入的单元格):

private static int GetSurroundingBombCount(bool[,] field, int posI, int posJ)
{
var surroundingBombCount = 0;

// Get the minimum and maximum i and j coordinates of the surrounding cells
var iMin = posI - 1 < 0 ? 0 : posI - 1;
var iMax = posI + 1 > field.GetUpperBound(0) ? posI : posI + 1;
var jMin = posJ - 1 < 0 ? 0 : posJ - 1;
var jMax = posJ + 1 > field.GetUpperBound(1) ? posJ : posJ + 1;

// Loop through these cells and increment our counter for each true value
// unless we hit the initial cell, in which case we just continue
for (int i = iMin; i <= iMax; i++)
{
for (int j = jMin; j <= jMax; j++)
{
if (i == posI && j == posJ) continue;
if (field[i, j]) surroundingBombCount++;
}
}

return surroundingBombCount;
}

那么用法看起来像这样:

int surroundingBombCount = GetSurroundingBombCount(field, posI, posJ);

我使用这段代码对其进行了测试,它生成了一个 3x3 的网格(您可以将其调整为任何您想要的),在单元格上放置随机炸弹,然后显示两个网格以便您可以直观地检查它们:一个带有炸弹的网格位置,以及另一个带有计数的网格:

private static void Main()
{
bool[,] field = new bool[3, 3];

Random rnd = new Random();

// The lower this number, the more bombs will be placed.
// Must be greater than one, should be greater than 2.
// If set to '2', all cells will contain a bomb, which isn't fun.
int bombScarcity = 10;

// Assign random bombs
for (int i = 0; i < field.GetLength(0); i++)
{
for (int j = 0; j < field.GetLength(1); j++)
{
field[i, j] = rnd.Next(1, bombScarcity) % (bombScarcity - 1) == 0;
}
}

// Show bomb locations
Console.WriteLine("\nBomb Locations:\n");

for (int i = 0; i < field.GetLength(0); i++)
{
for (int j = 0; j < field.GetLength(1); j++)
{
Console.Write(field[i, j] ? " T" : " F");
if ((j + 1) % field.GetLength(1) == 0) Console.WriteLine();
}
}

// Show bomb counts
Console.WriteLine("\nBomb Counts:\n");

for (int i = 0; i < field.GetLength(0); i++)
{
for (int j = 0; j < field.GetLength(1); j++)
{
Console.Write($" {GetSurroundingBombCount(field, i, j)}");
if ((j + 1) % field.GetLength(1) == 0) Console.WriteLine();
}
}

Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();
}

输出

enter image description here

为了好玩,一个 10 x 10:

enter image description here

关于c# - 如何计算给定索引周围 bool[,] 中的真实值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43477642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com