JAVA CODING - Implementing methods for a game, API

In summary, the createPlayfield() method is a static method that returns a list of GRects. Each GRect is colored randomly 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. The method should use the addBrick() method and properly initialize and loop through the variables.
  • #1
geforce
26
0
Code:
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;
	}

Problem I have is the createPlayfield() method, I have no idea on what to do, this is the description..

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.
 
Last edited:
Physics news on Phys.org
  • #2
so... no one?
 
  • #3
geforce said:
As you can see I tryed to do it but haven't done well, it's a very confusing description.

What does "haven't done well" mean? Are you getting output? Compile-time errors? Run-time errors? Help us out here.
 
  • #4
Code:
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 there's some more, I think my other methods are wrong too.

I know what to do but I don't know where to implement the code, I don't know how to that's 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".

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.
 
  • #5


I would suggest breaking down the problem into smaller, more manageable steps. First, let's focus on understanding what the createPlayfield() method is supposed to do. From the description, it seems like this method should create a grid of GRects (rectangles) with random colors, with each rectangle having the size of BLOCKWIDTH x BLOCKHEIGHT. The number of rows and columns in the grid is determined by BLOCKROWS and NBLOCKS, respectively. Each row should be separated by ROWSEPARATION and the first row should start at ROWZERO.

With this understanding, we can start thinking about how to approach the problem. One possible solution could be to use nested for loops, with the outer loop looping through each row and the inner loop looping through each column. Within each iteration of the inner loop, we can use the addBrick() method to create a GRect with the appropriate coordinates and color. We can also use the Random class to generate a random color for each GRect.

It is also important to consider the API provided, specifically the acm.graphics.GRect class. This class has methods that can help us create and manipulate rectangles, such as creating a rectangle with specific coordinates and size, and setting its color.

In summary, to implement the createPlayfield() method, we can use nested for loops to create a grid of GRects with random colors and the specified size and spacing. We can also use the methods provided by the acm.graphics.GRect class to help us with this task.
 

FAQ: JAVA CODING - Implementing methods for a game, API

1. What is the purpose of implementing methods for a game in Java coding?

The purpose of implementing methods for a game in Java coding is to create a set of instructions that can be repeatedly executed within the game. These methods allow for more efficient and organized code, making it easier to manage and update the game.

2. What is an API in Java coding?

An API (Application Programming Interface) is a set of predefined methods, classes, and interfaces that allow for easier and more standardized communication between different software components. In Java coding, APIs are commonly used to interact with external libraries or services.

3. How do you declare and define a method in Java coding?

To declare and define a method in Java coding, you must use the following syntax:
modifier returnType methodName(parameter list) {
      // method body
}

4. Can you provide an example of implementing a method for a game in Java coding?

Sure, here is an example of a method called "attack" that takes in an integer parameter for the amount of damage to be dealt to an enemy:
public void attack(int damage) {
      enemyHealth -= damage;
      System.out.println("You dealt " + damage + " damage to the enemy!");
}

5. Why is it important to properly implement methods and use APIs in Java coding?

Properly implementing methods and using APIs in Java coding is important because it allows for more efficient and organized code, making it easier to maintain and update the game. It also helps to avoid repetition and makes the code more readable for other developers. Additionally, using APIs allows for easier integration with external libraries or services, saving time and effort in the development process.

Similar threads

Replies
5
Views
2K
Replies
7
Views
2K
Replies
12
Views
2K
Replies
2
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
2
Views
3K
Replies
6
Views
3K
Back
Top