problem with 'extends'. Need a new way of doing it.

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 461 Views - Last Post: 08 June 2014 - 10:44 AM Rate Topic: -----

#1 josephjnatale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-June 14

problem with 'extends'. Need a new way of doing it.

Posted 07 June 2014 - 05:34 PM

I am making a game for my computer science class, and I ran into a problem, it pertains to using the extends. The game is basically super puzzle fighter, the problem is when I try to keep track of how many blocks are in each row. The way I have it set up is that there is a class called piece and the other "pieces" such as , gems, diamonds, and shperes extend piece. I want to be able to have a counter that keeps track of how many pieces are in each row. So I tried creating a new class called grid, and piece inherents grid. I have a method in grid called addOne and the array that is housing the amount in each row will not add more than one. I don't know if extending to grid will point to the same reference location or it would create a new one like creating a object.
Other notable things: I am using Processing 2. Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: problem with 'extends'. Need a new way of doing it.

#2 SuperProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 46
  • Joined: 17-April 14

Re: problem with 'extends'. Need a new way of doing it.

Posted 07 June 2014 - 09:21 PM

Could you show some of your code? It will help a lot.
Was This Post Helpful? 0
  • +
  • -

#3 josephjnatale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-June 14

Re: problem with 'extends'. Need a new way of doing it.

Posted 07 June 2014 - 10:45 PM

View PostSuperProgrammer, on 07 June 2014 - 09:21 PM, said:

Could you show some of your code? It will help a lot.

//calilng this in my main method
private void checkPiecesOnScreen() 
{
	p1Piece[p1CurrentPiece][0].checkValidY();
	p1Piece[p1CurrentPiece][1].checkValidY();
	p2Piece[p2CurrentPiece][0].checkValidY();
	p2Piece[p2CurrentPiece][1].checkValidY();
			
}
//checkValidaY() is inside piece which is a separate file
public abstract class piece extends grid
{
public void checkValidY()
	{
		
		if(row==(13-amountOfPiecesInEachColum[currentColum]) && stillMovingDown)
			{
				
				ableToMove=false;
				stillMovingDown=false;
				addOneToColum(currentColum);
			}
	}
}
//grid is also a separate file which addOneToColum is located, 
public class grid
{
	
	public int[] amountOfPiecesInEachColum = new int[6];
	
	public void addOneToColum(int currentColum)
	{
		amountOfPiecesInEachColum[currentColum]++;	
		
		for(int i =0; i<amountOfPiecesInEachColum.length;i++)
			System.out.print(amountOfPiecesInEachColum[i]+", ");
		System.out.println();
	}
	
}



I need to be able to modify the amountOfPiecesInEachColum Array through any piece.
Thanks
Joe
Was This Post Helpful? 0
  • +
  • -

#4 SuperProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 46
  • Joined: 17-April 14

Re: problem with 'extends'. Need a new way of doing it.

Posted 07 June 2014 - 11:45 PM

Try using an ArrayList instead of an Array and see if it makes a difference or any exception.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 02:05 AM

Quote

So I tried creating a new class called grid, and piece inherents grid.
That sounds quite wrong. Surely a piece appears in a grid? Also, note that in Java, class names begin upper case
Was This Post Helpful? 0
  • +
  • -

#6 josephjnatale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-June 14

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 09:19 AM

View Postg00se, on 08 June 2014 - 02:05 AM, said:

Quote

So I tried creating a new class called grid, and piece inherents grid.
That sounds quite wrong. Surely a piece appears in a grid? Also, note that in Java, class names begin upper case

i Know I just created those quickly to test if it works. What i think is going on is that when i call AddOne from any piece, it points to a different reference location, i forget if using extends is like creating an object and copies the location or if it shares the location?

View PostSuperProgrammer, on 07 June 2014 - 11:45 PM, said:

Try using an ArrayList instead of an Array and see if it makes a difference or any exception.

i only need 6 index's in the array though. i mean its getting to that section because i have it out put what is in each row, and basically it only outputs 1 never anymore even though there are like 2 or three pieces. Another key thing is that im using the "AmountOfPiecesInEachRow" [what ever row the current piece is in, im keeping track], them multiplying the amount by the height of the piece to determine when to stop the next piece from moving down.
Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 09:27 AM

Like goose said Piece should not extend Grid. A Grid should contain pieces (composition not inheritance).

Piece shouldn't know anything about being in a Grid. It should behave properly if it were in a grid of if I put it somewhere completely different. It shouldn't know its coordinates at all - that's the job of the Grid, to maintain a grid of pieces. The pieces should only really know what type they are and maybe other properties such as if they have been collected etc.

First revise your code so a Piece is completely separate from Grid. Then have an array or ArrayList of Piece objects inside Grid and see where you get.

You can think of inheritance like an 'is a' relationship. Is a Piece a Grid - no so there shouldn't be inheritance here. On the other hand does a Grid 'have a' Piece? Yes so you use composition and have a array/list of Piece objects instead.

This post has been edited by Ryano121: 08 June 2014 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#8 josephjnatale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-June 14

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 09:34 AM

I understand that they how the inheritance works, however i have many pieces in a array inside my main method and i need each of those pieces to understand and modify how many pieces there are in any given colum

This post has been edited by Ryano121: 08 June 2014 - 09:43 AM
Reason for edit:: Removed quotes

Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 09:38 AM

Quote

i have many pieces in a array inside my main method and i need each of those pieces to understand and modify how many pieces there are in any given colum


You shouldn't. That's the job of the Grid class.
Was This Post Helpful? 1
  • +
  • -

#10 josephjnatale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-June 14

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 09:41 AM

yes however the data inside the grid class is not getting changed.

This post has been edited by Ryano121: 08 June 2014 - 09:44 AM
Reason for edit:: No need to quote the post above

Was This Post Helpful? 0
  • +
  • -

#11 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 09:46 AM

It should be - that's my point. The Grid should handle how many Pieces are in each of its columns. Depending on how many there are it changes its Pieces accordingly.
Was This Post Helpful? 1
  • +
  • -

#12 josephjnatale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-June 14

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 09:48 AM

Yeah thats why im confused, because i thought it would work, If you have another way of doing it, that would be greatly appreciated

This post has been edited by Ryano121: 08 June 2014 - 09:59 AM
Reason for edit:: No need to quote the post above

Was This Post Helpful? 0
  • +
  • -

#13 SuperProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 46
  • Joined: 17-April 14

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 10:01 AM

I tried the Grid class itself and it seems to work (well at least for me).

Code:

import java.util.Random;

class Grid {

	public int[] amountOfPiecesInEachColum = new int[6];

	public void addOneToColum(int currentColum) {
		amountOfPiecesInEachColum[currentColum]++;

		for (int i = 0; i < amountOfPiecesInEachColum.length; i++)
			System.out.print(amountOfPiecesInEachColum[i] + ", ");
		System.out.println();
	}

}

public class Testing {

	public static void main(String[] args) {
		Grid grid = new Grid();

		for (int i = 0; i < grid.amountOfPiecesInEachColum.length; i++)
			System.out.print(grid.amountOfPiecesInEachColum[i] + ", ");
		System.out.println();

		Random random = new Random();

		for (int i = 0; i < grid.amountOfPiecesInEachColum.length; i++) {
			int randomNum = random.nextInt(6);
			grid.addOneToColum(randomNum);
		}
	}

}



Was This Post Helpful? 0
  • +
  • -

#14 josephjnatale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 07-June 14

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 10:09 AM

i have piece extends Grid, because each piece needs to know how many other pieces are in the current row, so the method checkValidY() could run correctly.

This post has been edited by Ryano121: 08 June 2014 - 10:18 AM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#15 SuperProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 46
  • Joined: 17-April 14

Re: problem with 'extends'. Need a new way of doing it.

Posted 08 June 2014 - 10:12 AM

I checked the method checkValidY() and it looks like it is working.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2