我是编码的初学者,我正试图缩小一大块容易出错的代码。我非常想要它
if (tileBlocked[row - 1][col] == true)
{
add tileAir[row - 1][col] to averaging
}
这样,只有当tileBlocked没有将该区域列为阻塞时,我才能找到数组tileAir中值的平均值。我四处搜索,我发现的唯一的东西是Averageif,据我所知,这只是excel。是否有任何等效或某种方式可以减少这一块代码的大小并允许更好的扩展能力?
现在这是我目前的代码:
for (int row = 1; row < 255; ++row) // Repeats for all rows, skipping the first and final row
{
for (int col = 1; col < 255; ++col) // Repeats for all columns, skipping the first and final column
{
bool check[4] = { tilesBlocked[row - 1][col], tilesBlocked[row][col - 1], tilesBlocked[row][col + 1], tilesBlocked[row + 1][col] }; // Creates an Array for testing what tiles should be ommited from the calculation
if (tilesBlocked[row][col][0] == true) // If the tile being calculated is blocked, skip it
{
}
else if (check[0] == true && check[1] == true && check[2] == true && check[3] == true)
{
}
else if (check[0] == false && check[1] == false && check[2] == false && check[3] == false)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row - 1][col] + tilesOxygen[row][col - 1] + tilesOxygen[row][col + 1] + tilesOxygen[row + 1][col]) / 5);
tilesOxygen[row - 1][col] = tilesOxygen[row][col];
tilesOxygen[row][col - 1] = tilesOxygen[row][col];
tilesOxygen[row][col + 1] = tilesOxygen[row][col];
tilesOxygen[row + 1][col] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == false && check[1] == false && check[2] == false && check[3] == true)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row - 1][col] + tilesOxygen[row][col - 1] + tilesOxygen[row][col + 1]) / 4);
tilesOxygen[row - 1][col] = tilesOxygen[row][col];
tilesOxygen[row][col - 1] = tilesOxygen[row][col];
tilesOxygen[row][col + 1] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == false && check[1] == false && check[2] == true && check[3] == false)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row - 1][col] + tilesOxygen[row][col - 1] + tilesOxygen[row + 1][col]) / 4);
tilesOxygen[row - 1][col] = tilesOxygen[row][col];
tilesOxygen[row][col - 1] = tilesOxygen[row][col];
tilesOxygen[row + 1][col] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == false && check[1] == false && check[2] == true && check[3] == true)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row - 1][col] + tilesOxygen[row][col - 1]) / 3);
tilesOxygen[row - 1][col] = tilesOxygen[row][col];
tilesOxygen[row][col - 1] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == false && check[1] == true && check[2] == false && check[3] == false)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row - 1][col] + tilesOxygen[row][col + 1] + tilesOxygen[row + 1][col]) / 4);
tilesOxygen[row - 1][col] = tilesOxygen[row][col];
tilesOxygen[row][col + 1] = tilesOxygen[row][col];
tilesOxygen[row + 1][col] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == false && check[1] == true && check[2] == false && check[3] == true)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row - 1][col] + tilesOxygen[row][col + 1]) / 3);
tilesOxygen[row - 1][col] = tilesOxygen[row][col];
tilesOxygen[row][col + 1] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == false && check[1] == true && check[2] == true && check[3] == false)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row - 1][col] + tilesOxygen[row + 1][col]) / 3);
tilesOxygen[row - 1][col] = tilesOxygen[row][col];
tilesOxygen[row + 1][col] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == false && check[1] == true && check[2] == true && check[3] == true)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row - 1][col]) / 2);
tilesOxygen[row - 1][col] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == true && check[1] == false && check[2] == false && check[3] == false)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row][col - 1] + tilesOxygen[row][col + 1] + tilesOxygen[row + 1][col]) / 4);
tilesOxygen[row][col - 1] = tilesOxygen[row][col];
tilesOxygen[row][col + 1] = tilesOxygen[row][col];
tilesOxygen[row + 1][col] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == true && check[1] == false && check[2] == false && check[3] == true)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row][col - 1] + tilesOxygen[row][col + 1]) / 3);
tilesOxygen[row][col - 1] = tilesOxygen[row][col];
tilesOxygen[row][col + 1] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == true && check[1] == false && check[2] == true && check[3] == false)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row][col - 1] + tilesOxygen[row + 1][col]) / 3);
tilesOxygen[row][col - 1] = tilesOxygen[row][col];
tilesOxygen[row + 1][col] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == true && check[1] == false && check[2] == true && check[3] == true)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row][col - 1]) / 2);
tilesOxygen[row][col - 1] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == true && check[1] == true && check[2] == false && check[3] == false)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row][col + 1] + tilesOxygen[row + 1][col]) / 3);
tilesOxygen[row][col + 1] = tilesOxygen[row][col];
tilesOxygen[row + 1][col] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == true && check[1] == true && check[2] == false && check[3] == true)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row][col + 1]) / 2);
tilesOxygen[row][col + 1] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else if (check[0] == true && check[1] == true && check[2] == true && check[3] == false)
{
tilesOxygen[row][col] = ((tilesOxygen[row][col] + tilesOxygen[row + 1][col]) / 2);
tilesOxygen[row + 1][col] = tilesOxygen[row][col];
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
else
{
std::cout << "Something Broke";
}
}
}
正如您所看到的,这是非常难以扩展且容易出错的。在此特定示例中,tileAir将替换为tileOxygen。
答案 0 :(得分:0)
至少,对于任何布尔表达式foo
,foo==true
可以仅由foo
替换,而foo==false
可以由!foo.
替换< / p>
答案 1 :(得分:0)
似乎没有相当于Excel中excel的Averageif。但是,通过使用函数减少了错误的可能性并增加了可读性。
答案 2 :(得分:0)
如果我理解了您的意图和代码,您的循环可以像这样转换:
// Repeats for all rows, skipping the first and final row
for (int row = 1; row < 255; ++row)
{
// Repeats for all columns, skipping the first and final column
for (int col = 1; col < 255; ++col)
{
// If the tile being calculated is blocked, skip it
if ( tilesBlocked[row][col]
|| ( tilesBlocked[row - 1][col] && tilesBlocked[row][col - 1]
&& tilesBlocked[row][col + 1] && tilesBlocked[row + 1][col] ))
{
continue;
}
double avg = tilesOxygen[row][col]; // educated guess about the type
int count = 1;
if ( !tilesBlocked[row + 1][col] ) {
avg += tilesOxygen[row + 1][col];
++count;
}
if ( !tilesBlocked[row][col + 1] ) {
avg += tilesOxygen[row][col + 1];
++count;
}
if ( !tilesBlocked[row][col - 1] ) {
avg += tilesOxygen[row][col - 1];
++count;
}
if ( !tilesBlocked[row - 1][col] ) {
avg += tilesOxygen[row - 1][col];
++count;
}
avg /= count;
tilesOxygen[row][col] = avg;
if ( !tilesBlocked[row + 1][col] ) {
tilesOxygen[row + 1][col] = avg;
}
if ( !tilesBlocked[row][col + 1] ) {
tilesOxygen[row][col + 1] = avg;
}
if ( !tilesBlocked[row][col - 1] ) {
tilesOxygen[row][col - 1] = avg;
}
if ( !tilesBlocked[row - 1][col] ) {
tilesOxygen[row - 1][col] = avg;
}
tilesOxygen[0][col] = 0;
tilesOxygen[row][0] = 0;
tilesOxygen[255][col] = 0;
tilesOxygen[row][255] = 0;
}
}