My main problem is the createplayfield method, this is the description to make it:

createPlayfield is a static method that returns a List of GRects. Each GRect is coloured randmoly and is
of size BLOCKWIDTH x BLOCKHEIGHT. The blocks are placed in a grid BLOCKROWS height x
NBLOCKS. The i'th row of blocks has its upper edge aligned with row i * ROWSEPARATION +
ROWZERO. The j'th column of blocks has its left edge aligned with j * BLOCKWIDTH. You may
find it useful to implement this method using the addBrick method below.

Alright edited it, take a look now, every other method should be fine except the method "createPlayfield".

May 18th, 2012, 12:12 AM

Parranoia

Re: Making a method to move blocks in a grid

Well in your createPlayField method, you never return a list at all and you only add one brick the whole time. Your loops really don't make any logical sense either. Try and think about it again step by step.

May 18th, 2012, 03:14 AM

geforce

Re: Making a method to move blocks in a grid

Um... yeah, I'm aware of that, thats why I'm trying to figure out what to do because I don't understand the statement for the createplayfield method, if my method had an small error I would of fixed it, but I'm obviously lost out of my mind on this method..

May 18th, 2012, 07:28 AM

Norm

Re: Making a method to move blocks in a grid

Rewrite the requirements for the method as a list of small simple steps. Then work on the code for each step.

What was posted is hard to read because it was has weird line breaks and doesn't have the steps in a simple list.

May 21st, 2012, 03:51 PM

geforce

Re: Making a method to move blocks in a grid

Quote:

Originally Posted by Norm

Rewrite the requirements for the method as a list of small simple steps. Then work on the code for each step.

What was posted is hard to read because it was has weird line breaks and doesn't have the steps in a simple list.

I understand how to follow the simple steps but i dont understand how to implement it in code, like how would i return the list of grects. Since the descriptions talks about j'tg coloumn and i'th row i know that i need a nested for loop but i dont know where to put the description of the i'th row into the i'th block, same as j coloumn and where to insert the blockrow x nblock

May 21st, 2012, 03:56 PM

Norm

Re: Making a method to move blocks in a grid

Often you can code from the list of simple steps.
If you have a list of steps and are having a problem coding any of them, list the step you are having problems with.

Quote:

how would i return the list of grects.

With a return statement:
return grects;

l is a terrible name for a variable. Can you add a few more letters to it?

May 21st, 2012, 04:57 PM

geforce

Re: Making a method to move blocks in a grid

Quote:

Originally Posted by Norm

Often you can code from the list of simple steps.
If you have a list of steps and are having a problem coding any of them, list the step you are having problems with.

With a return statement:
return grects;

l is a terrible name for a variable. Can you add a few more letters to it?

I will rename all the variables to make it look better once I can make the program function right.

ok so

Code java:

publicstatic java.util.List<acm.graphics.GRect> createPlayfield(){Random r =newRandom();
List<GRect> l =null;int x =0;int y = ROWZERO - BLOCKHEIGHT;Color c =newColor(r.nextInt(256),r.nextInt(256),r.nextInt(256));// Where to implement how to insert the blocks into a grid?for(i =0; i <= BLOCKROWS; i++)// {// How to implement that i'th row is aligned with i * ROWSEPARATION + ROWZERO
i = i * ROWSEPARATION + ROWZERO;for(j =0; j <= NBLOCKS; j++){// How to implement that i'th row is aligned with j x BLOCKWIDTH;
j = j * BLOCKWIDTH;}
addBrick(l,x,y,BLOCKWIDTH,BLOCKHEIGHT,c);// How to implement i and j into the list of grects.}return l;}

These are my problems and theres some more, I think my other methods are wrong too.

May 21st, 2012, 05:03 PM

Norm

Re: Making a method to move blocks in a grid

Quote:

I will rename all the variables to make it look better once I can make the program function right.

The idea of using well named variables is so when you (or anyone) reads the code they can understand what it does. When the coding is finished and is being tossed in the bin who cares what the names are.

Those comments hidden in the code are hard to read.

Quote:

how to insert the blocks into a grid?

What is a block? Is it an object?
What is the grid? Is it an array?

At what locations in the array are the objects to be inserted?

May 21st, 2012, 05:05 PM

geforce

Re: Making a method to move blocks in a grid

Yeah I'm aware, it's for coding to be more effiecent but at this time It won't make a difference if the whole program doesn't work.

May 21st, 2012, 05:07 PM

geforce

Re: Making a method to move blocks in a grid

The things I put in the comments are my main issues in this method and also that I'm not sure if I'm intializing variables that I don't need to before the constructor of addBrick, or if I'm adding it too late or without a proper loop etc.

May 21st, 2012, 05:11 PM

Norm

Re: Making a method to move blocks in a grid

Writing code before you have a design for what the code is supposed to do usually is a bad idea.
Get the design, then write the code.

May 21st, 2012, 05:15 PM

geforce

Re: Making a method to move blocks in a grid

Quote:

Originally Posted by Norm

Writing code before you have a design for what the code is supposed to do usually is a bad idea.
Get the design, then write the code.

Thats exactly what I've been telling you, I know what to do but I don't know where to implement the code, I don't know how to thats why I need help, If I knew the design I would of done it myself, I just don't know "Where to put the code" and "Where it belongs"

May 21st, 2012, 05:20 PM

Norm

Re: Making a method to move blocks in a grid

I have not seen the List of steps the code has to do to solve the problem. Post that and we'll work on the code for each step one by one.

Perhaps the steps you are thinking of are at a too high a level and need to be further broken down into simpler steps.

May 21st, 2012, 05:31 PM

geforce

Re: Making a method to move blocks in a grid

1) Create a static method (createPlayfield)
2) Each GRect is randomly coloured
3) Each GRect is of size BLOCKWIDTH x BLOCKHEIGHT
4) The blocks are placed in an grid
5) The grid is of size BLOCKROWS x NBLOCKS
6) The i'th row of the grid is aligned at the upper edge with i x ROWSEPARATION + ROWZERO
7) The j'th coloumn is aligned with it's left edge with j x BLOCKWIDTH
8) The list of GRects are returned

The words in capital are finals which are listed on my first post, the addBrick method and maybe the createBrick (Not sure) method are used in this method.

May 21st, 2012, 05:39 PM

Norm

Re: Making a method to move blocks in a grid

#4 what is a block? what is a grid? At what locations in the grid do the blocks go?
#5 implies the grid is a 2D array. Is that right?

I have no idea what #6 & #7 mean

#8 What is the List that is returned? How does that relate to the grid (an array?)?

May 21st, 2012, 05:45 PM

geforce

Re: Making a method to move blocks in a grid

Quote:

Originally Posted by Norm

#4 what is a block? what is a grid? At what locations in the grid do the blocks go?
#5 implies the grid is a 2D array. Is that right?

I have no idea what #6 & #7 mean

#8 What is the List that is returned? How does that relate to the grid (an array?)?

You're thinking way too complicated, there are no arrays in this at all.

The package imported acm.graphics implements the block, and the grid is implemented. On the grid there is a low left edge and a upper edge and their aligned with those things assigned above.. the picture I showed gives a VERY VERY good description of what the program is, it's basically that. The variable "l" in List<GRect> is returned.

Ok, I think I get it. The method is supposed to layout shapes on a visual grid. Most of the needed logic is for step #4. Work on the rows one by one using step #6 to get the rows position. On each row position the shapes column as per step #7

Now you need to expand step #4 to handle going through the rows and columns etc

May 21st, 2012, 05:53 PM

geforce

Re: Making a method to move blocks in a grid

This should give you a good example the methods are used in this program.

Ok so, I did what you described but now, the problem is, i and j are just there and their doing nothing, and there is no grid, how would I add an grid because there is BLOCKROW x NBLOCK for grid.

May 21st, 2012, 06:09 PM

geforce

Re: Making a method to move blocks in a grid

Also since the grid has a size would I need to put a loop for that?

May 21st, 2012, 06:14 PM

Norm

Re: Making a method to move blocks in a grid

Do you have the steps the program needs to do now? You keep posting lines of code.
What is the logic for placing the shapes for #4?
Does the GRect constructor take the size? That would be for #3. It also would have a method to take the results of #2.

May 21st, 2012, 06:21 PM

geforce

Re: Making a method to move blocks in a grid

4) I'm not sure.. I have to place the GRects onto the grid, probably would be l.add(i,j) or l.add(x,y) because of the coordinates? Maybe l.setLocation(x,y)...
3) it would be addBrick(l,x,y,BLOCKWIDTH,BLOCKHEIGHT,c) but.. the compiler says I must intialize it first so i end up doing List<GRect> = null, x = 0, y = something above, etc.. and then I don't know where to put the constructor because it has to be remade, or do I put the list? not sure..