0

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

0
3

Not sure what you mean by "many" if statements, but most people will use two nested for loops to create the bricks - an inner for loop to create each row and an outer for loop to do multiple rows. Then, you only need to create one set of statements to create each brick.

[EDIT - addition] There's nothing wrong with what you have done. This is a perfect example of "there can be lots of ways to do things, and one is not necessarily better than another." As you get more experience, you'll have your own opinions on what is more 'elegant' or efficient.

In this case, there are several alternatives. You could do what you did. You could use a case statement. You could create an array of color names and use the control value of one of the for loops as the array index to select a color (this was my choice). There are surely other ways. Is one better than another? Depends on your criteria for 'better'. The important questions are 'Does it work correctly?" and "does it impair performance?"

As you learn more, you'll pick up more of these techniques and hopefully will gain an instinct for which should be used. ;-)

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

2
  • Thanks for the reply. I think I wasn't clear enough in my question. I have now added in my code. What I basically want to know is whether there is a more succinct way to assign the colors to the blocks than the way that I have done it? (Note that the LEVEL1 etc are colors that I defined at the top of the program).
    – Annemarie
    Aug 16 '15 at 18:37
  • U cud declare char* COLORS = {BLUE, RED, ORANGE, PINK, YELLOW} then increment i by using i++ to COLORS[i] , so for i = 0, color will b blue, for i = 1 color will be red and so on. Thanks to Felipe De Lima Peressim from Brazil, it was his idea. Aug 22 '15 at 12:29
0

The block creation can be done as Cliff B states assigning the color depending on the row value in the inner loop.

"and the assigning of points based on where the block is placed"

The points assigned by block does not need to be done at creation time - If you are determining the score based on the block location (i.e. which row it is in) - remember you changed the color based on row? You can detect the color of the block object at the time of collision & then assign the points based on that value.

0

You can use the switch condition. It's like this:

switch (i)
{
    case 0:
        setColor(rect, LEVEL5);
        break;
    case 1: 
        setColor(rect, LEVEL4);
        break;
    case 2: 
        setColor(rect, LEVEL3);
        break;
    case 3: 
        setColor(rect, LEVEL2);
        break;
    case 4: 
        setColor(rect, LEVEL1); 
        break;                               
    default:  
        setColor(rect, "ORANGE");
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .