I have been working on the pset3 Breakout problem, and find myself using many nested IF statements, specifically for the creation of the blocks, and the assigning of points based on where the block is placed.
Is there an easier/more ascetically pleasing way to do this in C that achieves the same goal?
The code in question is:
void initBricks(GWindow window)
{
double BWIDTH = (WIDTH - 1.0 - 2.0 * 2.0 - (COLS - 1.0) * SPACE) / COLS;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
double x = 2.0 + (j * (BWIDTH + SPACE));
double y = DISTANCE + (i * (SPACE + BHEIGHT));
GRect rect = newGRect(x, y, BWIDTH, BHEIGHT);
setFilled(rect, true);
if (i == 0)
setColor(rect, LEVEL5);
else if (i == 1)
setColor(rect, LEVEL4);
else if (i == 2)
setColor(rect, LEVEL3);
else if (i == 3)
setColor(rect, LEVEL2);
else if (i == 4)
setColor(rect, LEVEL1);
else
setColor(rect, "ORANGE");
add(window, rect);
}
}
}
My question is, is there a more succinct method in C that could replace the if statement allocating the colours