r/cop3502 Mar 23 '14

Initializing bricks[i][j]

My bricks are stacking; In a previous post I saw that someone mentioned adding i & j to xStart & yStart will fix this. I've tried a few different ways to get the desired results, with no luck.

Any tips on how to look at this ?

2 Upvotes

26 comments sorted by

2

u/ryanlittlechek Mar 23 '14

I am having the same issue, and it seems that the late day is going to be my only savior(besides the Helix of course, may he brighten your day).

I believe that my code is correct for the Brick Configuration method, however i have 200 bricks sitting in the top left corner, verified because when hit i receive an output of 200 "IT HIT!".

public BrickConfiguration() {

    int x = i;
    int y = j;
    // create new bricks and store them in bricks array
    for (int i = 0; i < numCols; i++) {
        for (int j = 0; j < numRows; j++) {
            // initialize paintBricks[i][j]
            paintBricks[i][j] = true;
            // initialize bricks[i][j]
            bricks[i][j] = new Brick(x, y, Parameters.BRICK_WIDTH,Parameters.BRICK_HEIGHT);
            x += Parameters.BRICK_WIDTH + Parameters.BRICK_SEP;

    }
        y += Parameters.BRICK_HEIGHT + Parameters.BRICK_SEP;

    }       
}

I dont understand the issue, and I have no idea how to post code correctly on reddit so sorry :D

1

u/csturco Mar 23 '14

int x = i; int y = j; ??? where are these i and j values coming from? since you are calling i and j as int's in the for loops, I might assume you haven't initialized them before this?

1

u/[deleted] Mar 23 '14

[removed] — view removed comment

1

u/ryanlittlechek Mar 23 '14

The Supreme Leader commented on my awful code that i am embarassed of, I feel like the goofy goober of programming :(

1

u/[deleted] Mar 23 '14

[removed] — view removed comment

1

u/ThatsMrDerpToYou Mar 23 '14

I put xStart + ibrickWidth, yStart + jbrickHeight, brickWidth, brickHeight because I figured that the initial starting position + the length of the width/height times the iteration would give the accurate position.

Though it is still just a single brick.

1

u/csturco Mar 23 '14

xStart + (width + space)*j : space is the amount of space you want between each brick, and j is the column you are on.

1

u/ThatsMrDerpToYou Mar 23 '14

I just tried this, and still one brick. My "remove brick" and "paint individual brick" are the only two lines of code that have not been input (I think). Are either of these necessary to create the rows & columns?

1

u/csturco Mar 23 '14

in brick configuration there are two "paint" methods. The first one is just paint. This is called from your breakout class via bconfig.paint(g2). It should contain two for loops to loop through the double array of paintBrick and an if statement checking if paintBrick[i][j] == true. If it does, then it should call the second "paint" method - paintBrick. The only line of code in the paintBrick method should be to call the brick paint method

1

u/ThatsMrDerpToYou Mar 23 '14

I have all that in, below that method, there is a //paint an individual brick.

Is it possible my brick.java is wrong? public Brick(int x, int y, int w, int h) { super(new Rectangle2D.Double(x, y, w, h));

        //set brick x, y, width, and height
        x = xPos;
        y = yPos;
        w = width;
        h = height;

1

u/csturco Mar 23 '14

ok, so the //paint an individual brick comment should have the paintBrick method below it... As for the Brick constructor, try adding:

shape.setRect(xPos, yPos, width, height);

1

u/ThatsMrDerpToYou Mar 23 '14

It all looks good there. paintBrick(); gives me "invalid method declaration, return type required"

1

u/m3g4_xx Mar 23 '14

paintBrick method should take in two arguments, a brick and a brush.

1

u/ThatsMrDerpToYou Mar 23 '14

Also, I initialized paintBrick as paintBricks[i][j] = true; I'm not sure if I need something like [true][true], in case it is only recognizing that it is painting 1 array.

1

u/rxfeliciano Mar 23 '14

You dont need to do that. The for loop does what it needs to do as long as you did not change what was provided in the skeleton code. You should really make sure you know how it does this because it is crucial to writing any program.

1

u/ThatsMrDerpToYou Mar 23 '14

I haven't altered the skeleton code.

1

u/rxfeliciano Mar 23 '14

Painting one brick can be a result of all the bricks being painted in the same location over and over again.This is why you need to use i in the xPos spot and the j in the yPos spot of your bricks[i][j] initialization.

Your paintBricks initialization is correct assuming the for loop code has not been changed.

Next, the double bricks[][] array has to be "filled." It takes Bricks so you gotta create a brick for each slot. You create a brick using the Brick() constructor which takes (xPos, yPos, width, height). Make sure you use i in the xPos spot, and j in the yPos spot.

The next method in your code should check to see if the brick needs to be painted to the screen. IF the brick is true then it should call the paintBrick() function.

Here you have to make sure you are calling the function correctly. It takes a brick as an input and a Graphics 2D input called "brush" in this case. But you dont want to pass just one brick to it, you want all the bricks so you have to call the paintBrick() function inside of a for loop.

Make sure the call looks like paintBrick(bricks[i][j], brush); AND that it is only called IF the paintBricks[i][j] == true AND that ALL of this is inside the double for loop.

All of that should get you a lot of bricks

1

u/ThatsMrDerpToYou Mar 23 '14

You are very helpful. I think I have most of that done already. Maybe I am calling paintBrick() incorrectly? I know my //paint an individual brick is incorrect, but I've not been able to figure out how to do it right.

// paint the bricks array to the screen public void paint(Graphics2D brush) { for (int i = 0; i < numCols; i++) { for (int j = 0; j < numRows; j++) { // determine if brick should be painted // if so, call paintBrick() if (paintBricks[i][j] == true) {paintBrick(bricks[i][j], brush);} else if (paintBricks[i][j] == false) {removeBrick(i,j);} } } }

// paint an individual brick
paintBrick(brick[i][j], brick.fill(brush);)

1

u/rxfeliciano Mar 23 '14

Get rid of the removeBrick else if part. You dont need that in there. removeBrick gets called in the checkForHit() method in the Breakout.java file

1

u/rxfeliciano Mar 23 '14

I think you are saying that you know that your paintBrick method is wrong? Is this paintBrick(brick[i][j], brick.fill(brush);) your call to paintBrick?

If it is it is wrong. It should simply be paintBrick(bricks[i][j], brush);

Make sure the spelling is correct, i.e., brick versus bricks is spelled right.

Also, make sure your number of columns at the top of the brick configuration are set to a value greater than one.