JAVA CODING - Implementing methods for a game, API

  • Context: Comp Sci 
  • Thread starter Thread starter geforce
  • Start date Start date
  • Tags Tags
    Coding Game Java
Click For Summary

Discussion Overview

The discussion revolves around implementing the createPlayfield() method for a Java game using the ACM graphics library. Participants are addressing the challenges of creating a grid of colored rectangles (GRects) based on specific alignment and sizing requirements.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the implementation of the createPlayfield() method, specifically regarding how to properly align the blocks in a grid format.
  • Another participant requests clarification on the nature of the issues encountered, asking whether there are output problems, compile-time errors, or run-time errors.
  • A later reply attempts to outline a structure for the createPlayfield() method but raises questions about how to correctly implement the alignment of rows and columns, as well as the proper initialization of variables.
  • Concerns are raised about whether the participant is initializing unnecessary variables and the timing of adding bricks to the list.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the implementation details of the createPlayfield() method, with multiple competing views on how to approach the problem remaining unresolved.

Contextual Notes

There are indications of missing assumptions regarding the initialization of the list of GRects and the proper use of loops for grid placement, which remain unaddressed.

geforce
Messages
26
Reaction score
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
so... no one?
 
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.
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K