战舰概率网格

Battleship Probability Grid

本文关键字:网格 概率      更新时间:2023-10-16

我正在尝试为我的战列舰程序制作一个函数,以创建一个概率网格,其中包含所有剩余部分可能所在的位置。我希望网格在整个数组中循环,然后检查在该点上对象是否有放置空间(水平或垂直),并在它将覆盖的每个点上添加一个,告诉我哪些坐标上有船的可能性最大。BS_GRID_ROWS和BS_GRID_COLS都是保持战列舰甲板大小的值,矩阵是我想显示概率值的数组,hits是一个数组,如果一艘船被击中,则数字(对应于每艘船)大于零,如果我开枪未中,则为-1,如果在该坐标下没有开枪,则为零。这是我目前掌握的代码。它正在工作,但不正确,它在接近终点时完全错过了几个方块,我知道这是不可能的,当我在任何船只放置之前运行它时。如有任何帮助,我们将不胜感激。感谢

void
probabilityGrid(int matrix[BS_GRID_ROWS][BS_GRID_COLS], int hits[BS_GRID_ROWS][BS_GRID_COLS], int shipSize)
{
bool isValid=true;
for (int row = 0; row< BS_GRID_ROWS; row++)
{
for (int col = 0; (col+shipSize) <BS_GRID_COLS; col++)
{
for (int hold = col; hold < shipSize; hold++)
{
if (hits[row][hold]!=0)
isValid=false;
}
if (isValid)
{
for (int hold = col; hold < shipSize; hold++)
{
matrix[row][hold]++;
}
}
isValid=true;
}
}
//For now I'm just working on the horizontal part of the algorithm.
}

想象一下(或者更好地设置并通过调试器运行)BS_GRID_ROWS == 1BS_GRID_COLS == 1shipSize == 1

发生的情况是,条件(col+shipSize) <BS_GRID_COLSfalse,即使col == 0可能不是您想要的,您应该将其更改为col + shipSize <= BS_GRID_COLS

出于同样的原因,您应该将row<=BS_GRID_ROWS-shipSize-1更改为row + shipSize <= BS_GRID_ROWS