战舰概率网格
Battleship Probability Grid
我正在尝试为我的战列舰程序制作一个函数,以创建一个概率网格,其中包含所有剩余部分可能所在的位置。我希望网格在整个数组中循环,然后检查在该点上对象是否有放置空间(水平或垂直),并在它将覆盖的每个点上添加一个,告诉我哪些坐标上有船的可能性最大。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 == 1
、BS_GRID_COLS == 1
和shipSize == 1
。
发生的情况是,条件(col+shipSize) <BS_GRID_COLS
是false
,即使col == 0
可能不是您想要的,您应该将其更改为col + shipSize <= BS_GRID_COLS
。
出于同样的原因,您应该将row<=BS_GRID_ROWS-shipSize-1
更改为row + shipSize <= BS_GRID_ROWS
。
相关文章:
- 如何根据单词在文本中出现的概率输出单词
- 使用对象文件读取三角形数据网格
- 如何从 Skia 路径几何体中获取网格?
- CGAL:如何创建填充边界曲线的曲面网格?
- 检查 2D 网格的某个元素是否与另一个元素共享对角线、水平线或垂直线
- 创建具有两个视口的Qt3D C++场景,其中显示适当的不同网格
- 如何从 3D 曲面网格中删除自相交三角形?
- 在均匀网格中处理碰撞
- 更改高度贴图,使其在 4x4 网格上显示 16 个 hieghtmap
- 用于查找网格中最短路径的算法
- 循环通过网格获取温度,但不知道如何告诉程序停止循环
- 使用 glvertex4i 传递网格面索引时的顶点着色器错误
- 如何使用 sdl2 快速绘制像素网格?
- 如何使用条件计算 3D 网格中从一个点到另一个点的所有路径
- 使用 MFC 的表/网格
- 是否可以制作没有内部分割的cgal 3d多多马因网格?
- 获取网格中心的屏幕位置
- ASIMP 模型加载不会加载所有网格
- C++中的多维数据集:从 std::vector 的 2D 数据到 std::vector 的 2D 网格的最干净方法?
- 战舰概率网格