Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 5 of 6 FirstFirst ... 3456 LastLast
Results 101 to 125 of 150

Thread: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

  1. #101
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    I seriously don't know what to pass in as parameters.
    Time to go back to the design for the program and see what needs to be done to what data by what methods at what time in the execution.
    If you don't understand my answer, don't ignore it, ask a question.

  2. The Following User Says Thank You to Norm For This Useful Post:

    wholegrain (February 18th, 2013)

  3. #102
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    I need to access contents, but do you know how considering that contents is declared in Grid? I thought about declaring contents in Piece and Board, but I don't know if that will work.

    Write a method in the Board class that takes a row and column index and a two-dimensional array as parameters, and checks to see that the non-zero entries of the given array correspond to empty positions on the grid, assuming that the given array's upper left were positioned at the given row and column. You'll use this to determine if a piece can move into a given position.

    Here they say 1 2d array as parameters, but I used 2. Do you know any other way. There must be a simple way to do this, but I don't have the knowledge required to find it.

    Or do you know an easy way to rework the class architecture? Like putting Grid inside both Board and Piece, but I don't know if I can do that, because I don't know what Grid does exactly. I don't think I can just copy and paste everything inside those 2.

    It should be easy to modify it, since the tutorial is for an introductory course. I am pretty sure there is an easy way of doing this, but what is it?

    I could access shapes by writing contents inside moveDown(), because the method call was currentPiece.moveDown(), but here I can't really call the method like that. I don't understand is it because Tetris is in a different package? It doesn't seem to be in a different package. They are all in the same package, right? So, why can't I do that?

  4. #103
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    I need to access contents, but do you know how considering that contents is declared in Grid?
    You need to add a getter method to the Grid class that returns a reference to contents.

    putting Grid inside both Board and Piece,
    That's done. Board and Piece extend Grid.
    Again this is basic OOP that you need to know before working on this project. Have you ever heard the expression:
    Learn to walk before trying to run.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    wholegrain (February 18th, 2013)

  6. #104
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    I know basic inheritance, but this program is a lot more complex, so it kinda different from the examples I saw.

    I did write this a long time ago, but I don't know how I would use it in this case:

    	public int[][] getContents()
    	{
    		return contents;
    	}

    So do I have to do something like this?


    void fall() {
     
     
    int [][] a = board.getContent();
    int [][] b = currentPiece.getContent();
     
    		while(legalDown(currentX, currentY, a, b) && isNotOutOfBoundDown(currentX, currentY, a, b))
    		{
    		currentX--;
    		updateLocation();
        	updateSize();
    		}

    Would that work?

    --- Update ---

    	void fall() {
     
    		int [][] a = board.getContent();
    		int [][] b = currentPiece.getContent();
     
    		while(legalDown(currentX, currentY, a, b) && isNotOutOfBoundDown(currentX, currentY, a, b))
    		{
    		currentX--;
    		updateLocation();
        	updateSize();
    		}
     
    		Tetris.sleep(2000);
    	}

    Well that doesn't work.

    	void fall() {
     
    		int [][] a = null;
    		int [][] b = null;
     
    		while(legalDown(currentX, currentY, a, b) && isNotOutOfBoundDown(currentX, currentY, a, b))
    		{
    		currentX--;
    		updateLocation();
        	updateSize();
    		}
     
    		Tetris.sleep(2000);
    	}

    more surprisingly, this doesn't work something is wrong with this line:

    while(legalDown(currentX, currentY, a, b) && isNotOutOfBoundDown(currentX, currentY, a, b))

    I get:

    error: cannot find symbol

    	public int[][] getContents()
    	{
    		return contents;
    	}

    I put this in piece and strangely it compiles.

    		int [][] a = board.getContents();
    		int [][] b = currentPiece.getContents();

    doesn't work even with the getContents() in all 3 classes... so is the problem caused by board and currentPiece? How is that possible though? Both of them are public.

    Ok... something is weird...

    I get no error when I do this:

    	void fall() {
     
    		int [][] a = getContents();
    		//int [][] b = currentPiece.getContents();
     
    		//while(legalDown(currentX, currentY, a, b) && isNotOutOfBoundDown(currentX, currentY, a, b))
    		{
    		currentX--;
    		updateLocation();
        	updateSize();
    		}
     
    		Tetris.sleep(2000);
    	}

  7. #105
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Again, this is basic OOP programming. Calling a method define in a class from another class is a very basic technique that is used over and over and over in programs.

    There should only be one getContents() method. It should be in the class where contents is defined.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    wholegrain (February 18th, 2013)

  9. #106
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Quote Originally Posted by Norm View Post
    Again, this is basic OOP programming. Calling a method define in a class from another class is a very basic technique that is used over and over and over in programs.

    There should only be one getContents() method. It should be in the class where contents is defined.
    Are you sure about that? Because it gives me cannot find symbol when I remove it. It doesn't seem to inherit the method. So how would you go about doing the following:

    	void fall() {
     
    		int [][] a = Piece.getContents();
    		int [][] b = Board.getContents();
     
    		while(legalDown(currentX, currentY, contents, contents) && isNotOutOfBoundDown(currentX, currentY, contents, contents))
    		{
    		currentX--;
    		updateLocation();
        	updateSize();
    		}
     
    		Tetris.sleep(2000);
    	}

    int [][] a = Piece.getContents();
    int [][] b = Board.getContents();
    while(legalDown(currentX, currentY, contents, contents) && isNotOutOfBoundDown(currentX, currentY, contents, contents))

    these lines give me the same error" cannot find symbol

    Wait, I can't reference a non-static variable in a static context... So what am I supposed to do? It will never work, right?

  10. #107
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Again this problem is a basic OOP programming problem. You're trying to work with code that is past your knowledge. I suggest that you take a break from this project and go back and study the basics.

    Write a 30-50 line complete program with 4 classes. One base, two that extend and one driver.
    Define a variable in the base class and a getter method for it. In the driver class define instances of each of the classes that extend base class and call the getter method for each instance.

    What is the symbol that can not be found.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    wholegrain (February 18th, 2013)

  12. #108
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

     
    		int [][] a = currentPiece.getContents();
    		int [][] b = board.getContents();

    wait.. these doesn't work, because currentPiece and board aren't created yet? Is that it? So, how do I solve this problem? Do I have to modify fall() in main and pass board and as an arguments and then do the following?

     
    		int [][] a = getContents();
    		int [][] b = board.getContents();

    because getContents work, because of the method call: currentPiece.fall();

    --- Update ---

    Quote Originally Posted by Norm View Post
    Again this problem is a basic OOP programming problem. You're trying to work with code that is past your knowledge. I suggest that you take a break from this project and go back and study the basics.

    Write a 30-50 line complete program with 4 classes. One base, two that extend and one driver.
    Define a variable in the base class and a getter method for it. In the driver class define instances of each of the classes that extend base class and call the getter method for each instance.

    What is the symbol that can not be found.
    I just completed an assignment on inheritance. I just need to know how to make this work. This program is a lot more complex than what I had to do.

    board.getContents();

    doesn't work, because board is created at run-time. that's probably why.

    --- Update ---

    Ahhh, it works, except this line: while(legalDown(currentX, currentY, a, a) && isNotOutOfBoundDown(currentX, currentY, a, a))

     
    	void fall(Board board) {
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
    		while(legalDown(currentX, currentY, a, a) && isNotOutOfBoundDown(currentX, currentY, a, a))
    		{
    		currentX--;
    		updateLocation();
        	updateSize();
    		}
     
    		Tetris.sleep(2000);
    	}


    --- Update ---

     
    void fall(Board board) {
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
    		while(Board.legalDown(currentX, currentY, a, a) && Board.isNotOutOfBoundDown(currentX, currentY, a, a))
    		{
    		currentX--;
    		updateLocation();
        	updateSize();
    		}
     
    		Tetris.sleep(2000);
    	}

    Ok... what the...

    error: method legalDown in class Board cannot be applied to given types;

    error: method isNotOutOfBoundDown in class Board cannot be applied to given types;

    It doesn't make any sense...

    public static boolean legalDown(int currentX, int currentY, int[][] board, int[][] shape)

    public static boolean isNotOutOfBoundDown(int currentX, int currentY, int[][] board, int[][] shape)

    I assume there is something wrong with this:

     
    	public int[][] getContents()
    	{
    		return contents;
    	}

    There's only 1 getContents in Grid... so what is the problem?

  13. #109
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    board is created at run-time.
    That is true of most of a program. Not much happens when the code is compiled.

    Try writing the small exercise program to get an idea of where code should go.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    wholegrain (February 18th, 2013)

  15. #110
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    I think I solved most of the problems, but there is something wrong with the getter method...

     
    	void fall(Board board) {
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
    		while(Board.dummy(currentX, currentY, a) && Board.isNotOutOfBoundDown(currentX, currentY, a, b))
    		{
    		currentX--;
    		updateLocation();
        	updateSize();
    		}
     
    		Tetris.sleep(2000);
    	}

    error: method dummy in class Board cannot be applied to given types;
    error: method isNotOutOfBoundDown in class Board cannot be applied to given types;

    a and b gives the same error... do you know why?

  16. #111
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Try using the javac program to compile the code. The error messages you are posting are not complete.
    Here is a sample of a javac error message:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    wholegrain (February 18th, 2013)

  18. #112
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Microsoft Windows [Version 6.0.6002]
    Copyright (c) 2006 Microsoft Corporation. All rights reserved.


    C:\Users\->cd C:\Users\-\Desktop\tetris\tocomplete

    C:\Users\-\Desktop\tetris\tocomplete>javac Piece.java
    Piece.java:77: error: method legalDown in class Board cannot be applied to given
    types;
    while(Board.legalDown(currentX, currentY, b, a) && Board.isNotOu
    tOfBoundDown(currentX, currentY, b, a))
    ^
    required: int,int,int[][],int[][]
    found: int,double,int[][],int[][]
    reason: actual argument double cannot be converted to int by method invocation
    conversion
    Piece.java:77: error: method isNotOutOfBoundDown in class Board cannot be applie
    d to given types;
    while(Board.legalDown(currentX, currentY, b, a) && Board.isNotOu
    tOfBoundDown(currentX, currentY, b, a))
    ^
    required: int,int,int[][],int[][]
    found: int,double,int[][],int[][]
    reason: actual argument double cannot be converted to int by method invocation
    conversion
    2 errors

    C:\Users\-\Desktop\tetris\tocomplete>

    --- Update ---

    OMFG
    OMFG
    OMFG

    double lol

    --- Update ---

    man Jcreator is fucking gay

  19. #113
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    required: int,int,int[][],int[][]
    found: int,double,int[][],int[][]
    Note the difference in the data types. The compiler wants them to match exactly.

    reason: actual argument double cannot be converted to int by method invocation conversion

    Is currentY double vs int?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #114
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Well great... it compiles, but I get this:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at Board.legalDown(Board.java:75)
    at Piece.fall(Piece.java:77)
    at Tetris.dropPiece(Tetris.java:88)
    at Tetris.main(Tetris.java:80)

    I changed currentY to int, because I don't think it really matters.

    --- Update ---

    public class collisiontest
    {
     
    	public static boolean legalDown(int currentX, int currentY, int[][] board, int[][] shape)
    	{
    	   int x = currentX+1;
    	   int y = currentY;
     
    	   for (int row = 0; row < 4; row++)
    	   {
    	   	for(int col = 0; col < 4; col++)
    	   	{
     
     
    	   		if (shape[row][col] == 1 && board[x+row][y+col] == 0)
    	   		{
     
     
    	   		}
    	   		else
    	   		{
    	   			if (shape[row][col] == 0 && board[x+row][y+col] == 1)
    	   			{
     
    	   			}
    	   			else
    	   			{
     
    	   			    if(shape[row][col] ==0 && board[x+row][y+row] == 0)
    	   			    {
     
    	   			    }
     
    	   			    else
    	   			    {
     
    	   				return false;
    	   			    }
    	   			}
    	   		}
    	   	}
    	   }
     
     
        return true;
    	}
     
     
    public static void main(String[] args)
    {
      		int [][] m1 =
    		{{1,1,0,0},
    		 {1,1,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		};
     
     
    		int[][] board =
    		{{0,0,0,0},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		};
     
    			int[][] board3 =
    		{{0,0,0,0},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {1,0,1,1},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		 {1,1,1,1},
    		};
     
    		int[][] board2 =
    		{{0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {1,1,0,0},
    		};
     
    int currentX = 1;
    int currentY = 0;
     
    		System.out.println(legalDown(currentX, currentY, board, m1));
     
    		System.out.println(legalDown(currentX, currentY, board3, m1));
     
    		System.out.println(legalDown(currentX, currentY, board2, m1));
     
     
     
     
     
     
     
     
     
     
    }
    }

    I don't understand what might be wrong with the code.

    Like how come I get an indexoutofbound exception?

    Apparently it has to do with this line: if (shape[row][col] == 0 && board[x+row][y+col] == 1)


    1
    1
    2
    3
    1
    1
    2
    3
    1
    1
    2
    3
    1
    1
    2
    3
    1
    1
    2
    3
    1
    1
    2
    3
    1
    1
    2
    3
    1
    1
    2
    3
    1
    1
    2
    3
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    	at Board.legalDown(Board.java:68)
    	at Piece.fall(Piece.java:77)
    	at Tetris.dropPiece(Tetris.java:88)
    	at Tetris.main(Tetris.java:80)
     
    Process completed.


    --- Update ---

    Ok... I changed this

    currentX--;

    to that

    currentX++;

    and I get the reverse and no piece appears... I don't know what is wrong.

    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    1
    2
    2
    3
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
    at Board.legalDown(Board.java:75)
    at Piece.fall(Piece.java:77)
    at Tetris.dropPiece(Tetris.java:88)
    at Tetris.main(Tetris.java:80)

  21. #115
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
    at Board.legalDown(Board.java:75)
    At line 75 the index to an array went past the end of the array.
    Remember that array indexes range in value from 0 to the array length-1

    Use the println method to print out the values of the indexes to see which one has the bad value.


    The mixing of x and y with row and col is confusing.
    x measures across like columns
    y measures down like rows

    I'd change the names to all be same naming style instead of mixing them.
    Is currentX the starting row or the starting column?

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #116
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Ok... I added the following

    	   		if(x + row == 32)
    	   		{
    	   			return false;
    	   		}

    and it doesn't give an out of bound exception anymore; however, it freezes...

     
    	void fall(Board board) {
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
     
    		while(currentY < 32)
    		{
    			if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    			{currentY++;
    			updateLocation();
    	    	updateSize();
    	    	Tetris.sleep(2000);
    			}
    		}
     
    		//Tetris.sleep(2000);
    	}

    something is wrong with the above

    how do i get out of loop after a certain time?

    Ok... it works, I will post updates...



    Ok so basically I've done the following:

     
    	void fall(Board board) {
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
     
    		while(currentY < 28)
    		{
    			if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    			{currentY++;
    			updateLocation();
    	    	updateSize();
    	    	Tetris.sleep(2000);
    			}
    		}
     
    		//Tetris.sleep(2000);
    	}

    The piece should stop at 28, but there's one who kept going until it reached the floor... I think I rotated it, so it might explain the exceptional behavior. I need to fix 2 things, the freezing and the exceptional behaviour and find a way to make the piece go all the way down.

  23. #117
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Hard coding magic numbers like 28 and 32 makes for hard to maintain and understand code. Try using variables.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #118
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Quote Originally Posted by Norm View Post
    Hard coding magic numbers like 28 and 32 makes for hard to maintain and understand code. Try using variables.
    How do we debug this. I really have a hard trouble debugging the program and understanding why so many things are going wrong.

  25. #119
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    I use println statements to display the values of variables at each step of the process to see what the computer sees. If you understand what values the variables should have as the code executes, you should be able to see when the values are wrong and then look at the code to see what made the values go wrong.
    It can take lots of println statements.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #120
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class



    It somewhat works, but I need the pieces to go all the way down, and I think there is a problem with updateLocation(), because sometimes it doesn't hit anything and stops moving

  27. #121
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    Continue debugging the code until you see what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  28. #122
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    public class collisiontest
    {
     
    	public static boolean legalDownSpec(int currentX, int currentY, int[][] board, int[][] shape, int index)
    	{
    	   int x = currentX+1;
    	   int y = currentY;
     
    	   for (int row = 0; row < index; row++)
    	   {
    	   	for (int col = 0; col < index; col++)
    	   	{
     
    	   		if (shape[row][col] == 1 && board[x+row][y+col] == 0)
    	   		{
     
     
    	   		}
    	   		else
    	   		{
    	   			if (shape[row][col] == 0 && board[x+row][y+col] == 1)
    	   			{
     
    	   			}
    	   			else
    	   			{
     
    	   			    if(shape[row][col] ==0 && board[x+row][y+row] == 0)
    	   			    {
     
    	   			    }
     
    	   			    else
    	   			    {
     
    	   				return false;
    	   			    }
    	   			}
    	   		}
    	   	}
    	   }
     
     
        return true;
    	}
     
     
     
     
    	public static boolean legalDown(int currentX, int currentY, int[][] board, int[][] shape)
    	{
    	   int x = currentX+1;
    	   int y = currentY;
     
     
    	   if (x >= 3)
    	   {
    	   	int index = x;
    	   	index = 6-index;
     
    	   	return legalDownSpec(currentX, currentY, board, shape, index);
     
    	   	//call special code here
    	   }
     
    	   for (int row = 0; row < 4; row++)
    	   {
    	   	for(int col = 0; col < 4; col++)
    	   	{
     
    	   		if (shape[row][col] == 1 && board[x+row][y+col] == 0)
    	   		{
     
     
    	   		}
    	   		else
    	   		{
    	   			if (shape[row][col] == 0 && board[x+row][y+col] == 1)
    	   			{
     
    	   			}
    	   			else
    	   			{
     
    	   			    if(shape[row][col] ==0 && board[x+row][y+row] == 0)
    	   			    {
     
    	   			    }
     
    	   			    else
    	   			    {
     
    	   				return false;
    	   			    }
    	   			}
    	   		}
    	   	}
    	   }
     
     
        return true;
    	}
     
     
    public static void main(String[] args)
    {
      		int [][] m1 =
    		{{1,0,0,0},
    		 {1,0,0,0},
    		 {1,0,0,0},
    		 {1,0,0,0},
    		};
     
     
    		int[][] board =
    		{{0,0,0,0},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {1,0,1,1},
    		};
     
    			int[][] board3 =
    		{{0,0,0,0},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {0,0,1,1},
    		 {1,1,1,1},
    		};
     
    		int[][] board2 =
    		{{0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		 {0,0,0,0},
    		};
     
    int currentX = 7; //3 causes exception
    int currentY = 0;
     
    		System.out.println(legalDown(currentX, currentY, board, m1));
     
    		System.out.println(legalDown(currentX, currentY, board3, m1));
     
    		System.out.println(legalDown(currentX, currentY, board2, m1));
     
     
     
     
     
     
     
     
     
     
    }
    }

    Any tips. Do you know what's causing the problem?

    I did add a new method, but I get an Array Out Of Bound Exception even earlier.

    Any suggestion?

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 27
    at Board.legalDownSpec(Board.java:74)
    at Board.legalDown(Board.java:110)
    at Piece.fall(Piece.java:92)
    at Tetris.dropPiece(Tetris.java:88)
    at Tetris.main(Tetris.java:80)

    line74: if (shape[row][col] == 0 && board[x+row][y+col] == 1)
    line110: return legalDownSpec(currentX, currentY, board, shape, index);

    I don't get any exception in the console program, but in the Tetris program. It's another story. I don't know, can you look at the code and see what's wrong?

     
    	   if (x >= 28)
    	   {
    	   	int index = x;
    	   	index = 31-index;
     
    	   	return legalDownSpec(currentX, currentY, board, shape, index);
     
    	   	//call special code here
    	   }

    I changed a bit of code before putting it in Board.

  29. #123
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    The posted code still has the confusion between using x,y and row,col. It would be less confusing if the code worked with a single style of names.
    To me x is a column value and y is a row value. In a 2 dim array, the first dim is the row, the second the column.
    This code makes for confusion:
     board[x+row][y+col]
    mixing x (a column) with row

    Also this makes no sense:
    int currentX = 7; //3 causes exception
    none of the arrays have 7 columns.
    If you don't understand my answer, don't ignore it, ask a question.

  30. #124
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

     
    	   		if(shape[row][col] == 1 && row == 7)
    	   		{
    	   			return false;
    	   		}

    would this fix it? if row indexes was between 6 and 0, wouldn't this cause an arrayoutofbound?

    --- Update ---

    Quote Originally Posted by Norm View Post
    The posted code still has the confusion between using x,y and row,col. It would be less confusing if the code worked with a single style of names.
    To me x is a column value and y is a row value. In a 2 dim array, the first dim is the row, the second the column.
    This code makes for confusion:
     board[x+row][y+col]
    mixing x (a column) with row

    Also this makes no sense:
    int currentX = 7; //3 causes exception
    none of the arrays have 7 columns.
    oh that's inside my console program... ah sorry for the confusion, i kinda messed up, and just did a quick fix...

    which was

    currentX = y

    and current Y = x or something along the same line

  31. #125
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Changing the attribute of an instance object of a class that's declared in another package and whose variable is constructed by its parent class

    With good, consistent names for the variables, some of the confusion will be removed and the code can be easier to understand. Mixing x,y with row,col is confusing and should be removed.
    If you don't understand my answer, don't ignore it, ask a question.

Page 5 of 6 FirstFirst ... 3456 LastLast

Similar Threads

  1. Instance Variable does't work in extended class?
    By jean28 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 21st, 2013, 12:36 AM
  2. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  3. calling a changing variable from another class
    By bondage in forum Collections and Generics
    Replies: 11
    Last Post: December 7th, 2011, 09:17 AM
  4. Replies: 7
    Last Post: July 21st, 2011, 02:29 PM
  5. Access and set variable in parent class through child
    By java_newbie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 19th, 2011, 10:44 PM