-
Making a method to move blocks in a grid
Code java:
import acm.graphics.*;
import acm.program.*;
import java.awt.Color;
import java.util.Iterator;
import java.util.Random;
import java.util.List;
public class LAB1
{
final static int BLOCKWIDTH = 50;
final static int BLOCKHEIGHT = 10;
final static int NBLOCKS = 10;
final static int BLOCKROWS = 5;
final static int ROWZERO = 50;
final static int ROWSEPARATION = (5*BLOCKHEIGHT);
final static int WIDTH = (BLOCKWIDTH*NBLOCKS);
final static int HEIGHT = 600;
public static java.util.List<acm.graphics.GRect> createPlayfield()
{
Random r = new Random();
List<GRect> l = null;
int x = 0;
int y = ROWZERO - BLOCKHEIGHT;
Color c = new Color(r.nextInt(256));
addBrick(l,x,y,BLOCKWIDTH,BLOCKHEIGHT,c);
for (int i = 0; i < BLOCKROWS; i++)
{
i = (i * ROWSEPARATION) + ROWZERO;
for(int j = 0; j < NBLOCKS; j++)
{
j = j * BLOCKWIDTH;
}
}
}
public static void addBrick(java.util.List<acm.graphics.GRect> l, int x, int y, int width, int height, java.awt.Color c)
{
l.add(createBrick(x,y,WIDTH,HEIGHT,c));
}
public static acm.graphics.GRect createBrick(int x, int y, int width, int height, java.awt.Color c)
{
GRect s = new GRect(x,y,WIDTH,HEIGHT);
return s;
}
public static acm.graphics.GRect intersect(acm.graphics.GObject o,java.util.List<acm.graphics.GRect> l)
{
GRectangle r = o.getBounds();
for(GRect e : l)
{
if(e.getBounds().intersects(r))
return e;
}
return null;
}
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.
API's : The acm.graphics.GRect Class.
As you can see I tryed to do it but haven't done well, it's a very confusing description.
-
Re: Making a method to move blocks in a grid
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
-
Re: Making a method to move blocks in a grid
Alright edited it, take a look now, every other method should be fine except the method "createPlayfield".
-
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.
-
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..
-
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.
-
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
-
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?
-
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:
public static java.util.List<acm.graphics.GRect> createPlayfield()
{
Random r = new Random();
List<GRect> l = null;
int x = 0;
int y = ROWZERO - BLOCKHEIGHT;
Color c = new Color(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.
-
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?
-
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.
-
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.
-
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.
-
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"
-
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.
-
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.
-
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?)?
-
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.
This program is based on a game breakout http://www.arcadebond.com/games/imag...akout_game.jpg
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.
this is the api for GRect Generated Documentation (Untitled) and Generated Documentation (Untitled) is the Package of everything such as graphic tools for the blocks etc
-
Re: Making a method to move blocks in a grid
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
-
Re: Making a method to move blocks in a grid
This should give you a good example the methods are used in this program.
Code java:
import acm.graphics.*;
import acm.program.*;
import java.awt.Color;
import java.util.Random;
import java.util.List;
public class Lab1Exerciser extends GraphicsProgram
{
private static final int BALLWIDTH = 20;
public void run()
{
this.setSize(GraphicsTools.WIDTH,GraphicsTools.HEIGHT);
Random r = new Random();
int x = (GraphicsTools.WIDTH-BALLWIDTH) /2;
int y = (GraphicsTools.HEIGHT-BALLWIDTH) / 2;
int vx = 5 - r.nextInt(11);
int vy = 5 - r.nextInt(11);
if(vx == 0)
vx = 1;
if(vy == 0)
vy = 1;
GOval ball = new GOval(x, y, BALLWIDTH, BALLWIDTH);
ball.setFillColor(Color.RED);
ball.setFilled(true);
this.add(ball);
List<GRect> l = GraphicsTools.createPlayfield();
for(GRect e : l)
this.add(e);
for(;;)
{
x = x + vx;
y = y + vy;
if(x < 0)
{
x = 0;
vx = -vx;
}
if(y < 0)
{
y = 0;
vy = -vy;
}
if(x > GraphicsTools.WIDTH-BALLWIDTH)
{
x = GraphicsTools.WIDTH-BALLWIDTH;
vx = -vx;
}
if(y > GraphicsTools.HEIGHT - BALLWIDTH)
{
y = GraphicsTools.HEIGHT - BALLWIDTH;
vy = -vy;
}
GRect hit = GraphicsTools.intersect(ball, l);
if(hit != null)
{
GRectangle bb = ball.getBounds();
GRectangle bo = hit.getBounds();
double bxo = bb.getX() + bb.getWidth() / 2;
double byo = bb.getY() + bb.getHeight() / 2;
double oxo = bo.getX() + bo.getWidth() / 2;
double oyo = bo.getY() + bo.getHeight() / 2;
if(bxo < oxo) vx = -Math.abs(vx);
if(bxo > oxo) vx = Math.abs(vx);
if(byo < oyo) vy = -Math.abs(vy);
if(byo > oyo) vy = Math.abs(vy);
l.remove(hit);
this.remove(hit);
}
ball.setLocation(x, y);
this.pause(10);
}
}
}
-
Re: Making a method to move blocks in a grid
Code java:
public static java.util.List<acm.graphics.GRect> createPlayfield()
{
Random r = new Random();
List<GRect> l = null;
int x = 0;
int y = ROWZERO - BLOCKHEIGHT;
Color c = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
for (i = 0; i <= BLOCKROWS; i++)
{
i = i * ROWSEPARATION + ROWZERO;
for (j = 0; j <= NBLOCKS; j++)
{
j = j * BLOCKWIDTH;
addBrick(l.getX(),l.getY(),BLOCKWIDTH,BLOCKHEIGHT,l.getColor(c));
}
}
return l;
}
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.
-
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?
-
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.
-
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..
-
Re: Making a method to move blocks in a grid
oh maybe for 4) I do for(GRect e : l) this.add(e);