# Thread: Stanford Breakout Exercise

1. Member
Join Date
Jul 2012
Posts
33
Rep Power
0

## Stanford Breakout Exercise

Hiya

I'm doing the Stanford cs106a course online and am doing an exercise where you code a game of breakout. I'm not stuck- what I've done so far satisfies the first part, which is just to set up the board and the rows of bricks. But what I've done seems very clumsy. I've used a while loop to repeat code several times (most of the following code is the repeated loop), once for each differently colored row of bricks. My first thought was to get the createBrick method (at the bottom) to count the bricks and return different colors. But there doesn't seem to be any way to set the call parameters to accept color. So in the end I did this. Is this as inelegant as I think it is? Thanks! Robbie
Java Code:
```/** Runs the Breakout program. */
public void run() {
setUpBreakout();
}

private void setUpBreakout() {
setUpCanvas(APPLICATION_WIDTH / 2, APPLICATION_HEIGHT / 2);
setUpBricks();
}
private void setUpCanvas(double cx, double cy) {
double x = cx - WIDTH / 2;
double y = cy - HEIGHT /2;
GRect canvas = new GRect(x, y, WIDTH, HEIGHT);
canvas.setFilled(false);
add(canvas);
}

private void setUpBricks() {
double y = BRICK_Y_OFFSET;
int numOfRows = NBRICK_ROWS;
int numBricks = NBRICKS_PER_ROW;
while (numOfRows > 8) {
double x = (getWidth() / 2) - (((NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP)) / 2) - BRICK_SEP / 2);
for (int i = 0; i < NBRICKS_PER_ROW; i++) {
add(createBrick(x, y, BRICK_WIDTH, BRICK_HEIGHT, Color.RED));
x += BRICK_WIDTH + BRICK_SEP;
}
numBricks--;
numOfRows--;
y += BRICK_HEIGHT + BRICK_SEP;
}
while (numOfRows > 6) {
double x = (getWidth() / 2) - (((NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP)) / 2) - BRICK_SEP / 2);
for (int i = 0; i < NBRICKS_PER_ROW; i++) {
add(createBrick(x, y, BRICK_WIDTH, BRICK_HEIGHT, Color.ORANGE));
x += BRICK_WIDTH + BRICK_SEP;
}
numBricks--;
numOfRows--;
y += BRICK_HEIGHT + BRICK_SEP;
}
while (numOfRows > 4) {
double x = (getWidth() / 2) - (((NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP)) / 2) - BRICK_SEP / 2);
for (int i = 0; i < NBRICKS_PER_ROW; i++) {
add(createBrick(x, y, BRICK_WIDTH, BRICK_HEIGHT, Color.YELLOW));
x += BRICK_WIDTH + BRICK_SEP;
}
numBricks--;
numOfRows--;
y += BRICK_HEIGHT + BRICK_SEP;
}
while (numOfRows > 2) {
double x = (getWidth() / 2) - (((NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP)) / 2) - BRICK_SEP / 2);
for (int i = 0; i < NBRICKS_PER_ROW; i++) {
add(createBrick(x, y, BRICK_WIDTH, BRICK_HEIGHT, Color.GREEN));
x += BRICK_WIDTH + BRICK_SEP;
}
numBricks--;
numOfRows--;
y += BRICK_HEIGHT + BRICK_SEP;
}
while (numOfRows > 0) {
double x = (getWidth() / 2) - (((NBRICKS_PER_ROW * (BRICK_WIDTH + BRICK_SEP)) / 2) - BRICK_SEP / 2);
for (int i = 0; i < NBRICKS_PER_ROW; i++) {
add(createBrick(x, y, BRICK_WIDTH, BRICK_HEIGHT, Color.CYAN));
x += BRICK_WIDTH + BRICK_SEP;
}
numBricks--;
numOfRows--;
y += BRICK_HEIGHT + BRICK_SEP;
}
}

private GRect createBrick(double x, double y, int width, int height, Color color) {
GRect brick = new GRect(x, y, width, height);
brick.setColor(color);
brick.setFilled(true);
return brick;
}
}```
Last edited by Norm; 07-27-2012 at 02:24 PM. Reason: added code tags

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•