 # Stanford CS106A Breakout Game Setting Up Bricks Solution

So the first part of the Breakout game in Assignment 3 is setting up the bricks, as described in the instructions: Here is my solution:

The hardest part about setting up the bricks for me was figuring out how to make sure the bricks were spaced out as they are in the picture. Otherwise, this was pretty similar to the Pyramid Problem in Assignment 2. Here is how I conquered this part, also available on gist.

```&lt;pre&gt;//adding an individual brick object
private GRect brick;

//drawing all the bricks necessary for the game
private void drawBricks(double cx, double cy) {

/*need to have several columns in each row
* so there need to be two for loops,
* one for loop for the rows and one for loop for the columns.
*/

for( int row = 0; row &amp;lt; NBRICK_ROWS; row++ ) {

for (int column = 0; column &amp;lt; NBRICKS_PER_ROW; column++) {

/* To get the x coordinate for the starting width:
* 	start at the center width,
* 	subtract half of the bricks (width) in the row,
* 	subtract half of the separations (width) between the bricks in the row,
* now you're at where the first brick should be,
* so for the starting point of the next bricks in the column, you need to:
* 	add a brick width
* 	add a separation width
*/

double	x = cx - (NBRICKS_PER_ROW*BRICK_WIDTH)/2 - ((NBRICKS_PER_ROW-1)*BRICK_SEP)/2 + column*BRICK_WIDTH + column*BRICK_SEP;

/* To get the y coordinate of the starting height:
* 	start at the given length from the top for the first row,
* 	then add a brick height and a brick separation for each of the following rows
*/

double	y = cy + row*BRICK_HEIGHT + row*BRICK_SEP;

brick = new GRect( x , y , BRICK_WIDTH , BRICK_HEIGHT );
add (brick);
brick.setFilled(true);

//Setting colors depending on which row the bricks are in

if (row &amp;lt; 2) {
brick.setColor(Color.RED);
}
if (row == 2 || row == 3) {
brick.setColor(Color.ORANGE);
}
if (row == 4 || row == 5) {
brick.setColor(Color.YELLOW);
}
if (row == 6 || row == 7) {
brick.setColor(Color.GREEN);
}
if (row == 8 || row == 9) {
brick.setColor(Color.CYAN);
}
}
}&lt;/pre&gt;
```

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.