Stanford CS106A Assignment 2 Pyramid Solution

The pyramid problem in Assignment 2 is actually pretty difficult for beginners, so I recommend you do the Target problem first. If you were able to figure out the Target problem, move on to this one. Be warned though, this one takes some time to figure out. Here is the problem:

Here is my solution, which you can also find on github:


The key here is to create 2 loops, one for the height part of the pyramid, and one for the width. The height-based loop needs to start with one, while the width-based loop can start with 0. I found it helpful by first writing out the code manually for the first 3 rows that needed to be built and then figuring out the small differences, after which I started over and created the two loops.

<pre>/*
 * File: Pyramid.java
 * Name: 
 * Section Leader: 
 * ------------------
 * This file is the starter file for the Pyramid problem.
 * It includes definitions of the constants that match the
 * sample run in the assignment, but you should make sure
 * that changing these values causes the generated display
 * to change accordingly.
 */

import acm.graphics.*;
import acm.program.*;
import java.awt.*;

public class Pyramid extends GraphicsProgram {

/** Width of each brick in pixels */
	private static final int BRICK_WIDTH = 30;

/** Width of each brick in pixels */
	private static final int BRICK_HEIGHT = 12;

/** Number of bricks in the base of the pyramid */
	private static final int BRICKS_IN_BASE = 14;

	public void run() {
		putAllBricks();
	}
	private void putAllBricks()
	{
		//row - 0-based index of the row 
		//row+1 - The 1-based index of the row
		//brickNum - 0-based index of the brick

		//Loop through the 0-based index of the rows
		for( int row = 0; row < BRICKS_IN_BASE; row++ )
		{
			int bricksInRow = BRICKS_IN_BASE - row;

			//
			// Total number of bricks = row
			//
			for( int brickNum = 0; brickNum < bricksInRow; brickNum++ )
			{
				//1. Calculate the center
				//2. Calculate the starting point based on the center
				//3. Add the number of bricks * brick width to find this brick's location
				int x = ( getWidth()/2 ) - (BRICK_WIDTH * bricksInRow) / 2 + brickNum * BRICK_WIDTH;

				// 
				// Calculate the vertical location of the brick based on the row
				//
				int y = getHeight() - BRICK_HEIGHT * (row+1);

				//
				// Draw the brick
				//
				GRect brick = new GRect( x , y , BRICK_WIDTH , BRICK_HEIGHT );
				add(brick);
			}
		}
	}
}</pre>

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