Wednesday, February 10, 2010

Stuck Stuck Stuck like a Duck



/*
* File: Pyramid.java
* Name:
* Section Leader:
* ------------------
* First, you have an outer loop that says, "Start the next row". It does this by,
* once you've made a row, removing one of the bricks from Bricks_In_Base so that
* the second row is one brick shorter, and by moving the starting brick up by
* 1 brick-height up and 1/2 brick-width to the right.
*
* Second, you have an inner loop that says "Make a row." It does this by
* placing down a brick in the starting location, then adding Brick_Width to
* the x coordinate and laying another brick at the new x-coordinate, repeated
* until you get to the end of the number of bricks in your row
* (or, until the index j gets to Bricks_In_Base.)
*
*/

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

public class Pyramid extends GraphicsProgram {

public void run() {

//X and Y coordinates for the starting (bottom-left) brick:
double xstart = getWidth()/2 -(1/2)*(BRICK_WIDTH)*(BRICKS_IN_BASE);
double ystart = getHeight()/2 + (1/2)*(BRICK_HEIGHT)*(BRICKS_IN_BASE);

double x = xstart;

double y = ystart;
double BricksInRow = BRICKS_IN_BASE;


//Outer loop to make a new row. The pyramid has as many rows as bricks in base.
for(int i = 0; i < BRICKS_IN_BASE; i++) {


//Inner loop to place bricks in a row
for(int j = 0; j < BricksInRow; j++) {

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

x += BRICK_WIDTH; //place next brick forward

}

BricksInRow -= 1; //Make the next row 1 brick shorter
y = y - BRICK_HEIGHT; //and one brick higher
xstart += BRICK_WIDTH / 2; //and start 1/2 a brick to the right

}
}

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

//Width of each brick in pixels
privat
e static final int BRICK_HEIGHT = 12;

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

}

If I built pyramids, all my mummies would have disintegrated long ago.

7 comments:

  1. Remember to reset x at the end of your outer loop!

    ReplyDelete
  2. Right after you increase the y position by brick-height you need reset the x position to the original x position plus brick-length/2

    ReplyDelete
  3. It is customary to declare your constant variables (private static final int CAP_LETTERS = 99, etc) at the top of your class outside of any methods, between 'public class....' and 'public void run', this way they are in scope.

    This will not actually make your code work better.

    ReplyDelete
  4. ooh, ok. So the problem was that, even though I made XStart 1/2 a brick-width over, I didn't reset x to the new x start. Therefore, when I started a new row, it just picked up x from where the old row left off! Good catch, Usman and Arram.

    ReplyDelete
  5. @Brendan: Huh? My constants are outside the run method, right before the last closing bracket. I made them after, not before the method....but in the Roberts textbook, all the examples have the constants declared at the end. Can and 3rd parties weigh in on the proper convention?

    ReplyDelete
  6. Our site q9x46b9w76 replica louis vuitton bag replica chanel bags ebay go to my site h1g68z6x06 replica louis vuitton bags replica bags china more info here q9s83p7c50 replica ysl handbags 7a replica bags wholesale

    ReplyDelete