Stanford CS106A Breakout Game Setting Up Bricks Solution

8 Nov

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.

<pre>//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 &lt; NBRICK_ROWS; row++ ) {

			for (int column = 0; column &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);

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

				if (row &lt; 2) {
				if (row == 2 || row == 3) {
				if (row == 4 || row == 5) {
				if (row == 6 || row == 7) {
				if (row == 8 || row == 9) {

4 Responses to “Stanford CS106A Breakout Game Setting Up Bricks Solution”

  1. João Silva March 29, 2012 at 8:29 am #

    From what i’m reading in this code, and pretty much like i saw in every people trying to do this part of assignment no one takes in mind if there is a different number of rows. Wich limit the chance to do different levels fast. I came up with a solution for that wich use’s the %… The exercice that i’v done is from 2012 wich has 4 colors, RED, YELLOW, GREEN, CYAN.

    Wich means RED appears in row 1,2,9,10. repits cycle each 8 rows:
    So using switch condition u can say that all i%8 = 1 or 2 u get red.
    By doing the same to others color for YELLOW 3,4 GREEN 5,6 and CYAN 7,default
    You always get the sequence right no matter if u’v 1 or 9999 rows with 1 or 9999 bricks per row.

    GCanvas gc = new GCanvas();
    gc.setSize(WIDTH, HEIGHT);
    for(int i=1;i<=NBRICK_ROWS;i++){
    for(int j=1;j<=NBRICKS_PER_ROW;j++){
    GRect brick=new GRect(BRICK_WIDTH,BRICK_HEIGHT);
    case 1:
    case 2: brick.setColor(Color.RED);break;
    case 3:
    case 4: brick.setColor(Color.YELLOW);break;
    case 5:
    case 6: brick.setColor(Color.GREEN);break;
    case 7:
    default: brick.setColor(Color.CYAN);break;

    By adding more cases and changing the 8 to NumberofColor*2 you can actually do this for infinity number of Colors. And with pretty much low changing u can do 3 in 3 rows 4 in 4 rows color change. Anyway the most important part is that u can do different levels just by changing the number of rows and number of bricks per row, while keeping the color change every 2 rows.

    • erik July 9, 2012 at 1:56 pm #

      Hi I am a complete noob to to Java and am having trouble figuring out what cx and cy mean and why people use them as variables. In your code, it doesn’t seem like cx and cy are ever given any values, so how can cx be used to find the x-coordinate of the first column of bricks? I understand the logic of how to find the x-coordinate, but have trouble implementing it in my code. I would think you would use the getWidth method, but for some reason getWidth keeps creating errors when I run it in eclpse.

  2. leo268 July 25, 2012 at 8:19 pm #

    I just had an idea for this solution:

    	private void buildBricks(){
    		// initial brick creation
    		int initial_x = (getWidth()/BRICK_WIDTH)/BRICK_SEP;
    		int y = BRICK_Y_OFFSET;
    		int x = initial_x;
    		for (int j = 0; j&lt;NBRICK_ROWS; j++){
    			for (int i = 0; i&lt;NBRICKS_PER_ROW;i++){
    				GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    				// this loop verifies what color corresponds to each row
    				if (j == 0 || j == 1){
    				} if (j == 2 || j == 3) {
    				} if (j == 4 || j == 5){
    				} if (j == 6 || j == 7){
    				} if (j == 8 || j == 9){
    				x = x + BRICK_SEP + BRICK_WIDTH;
    				if (i==NBRICKS_PER_ROW-1){
    					x = initial_x;
    					y+=BRICK_HEIGHT + BRICK_SEP; 


  1. CS106A Assignment 3 - Breakout, Part 1: Setting up the Bricks | PROGRAMMING ZERO TO HERO - April 25, 2012

    […] Natasha The Robots solution […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s