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 6 of 6 FirstFirst ... 456
Results 126 to 150 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. #126
    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

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


    It seemed to have improved slightly, but I don't understand why 30 is outofbound... like what the hell?

    console program with recent modifications

    public class collisiontest
    {
     
    	public static boolean legalDownSpec(int currentX, int currentY, int[][] board, int[][] shape, int index)
    	{
    	   int x = currentX+1;
    	   int y = currentY;
     
    	   //int row = array.length-1; row >= 0; row--
     
    	   for (int row = index; row > 0; row--)
    	   {
    	   	for (int col = index; col > 0; 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;
     
    	   for (int row = 1; row < 4; row++)
    	   {
    	   	for(int col = 0; col < 4; col++)
    	   	{
     
    	   	    if(shape[row][col] == 1 && x+row >= 6)
    	   		{
    	   			return false;
    	   		}
    	   	}
    	   }
     
    	   	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 = 6; //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));
     
     
     
     
     
     
     
     
     
     
    }
    }


    --- Update ---

    oK... this line in legalDownSpec causes the exception... so... I can't think of a way to solve it

    if (shape[row][col] == 0 && board[x+row][y+col] == 1)

    I did the following, because I thought it would solve it, but it didn't

    for (int row = index; row > 0; row--)
    {
    for (int col = index; col > 0; col--)
    {

    --- Update ---

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
    at Board.addArray(Board.java:49)
    at Piece.fall(Piece.java:103)
    at Tetris.dropPiece(Tetris.java:88)
    at Tetris.main(Tetris.java:80)

    Process completed.

    Well... it reached the bottom at the least

  2. #127
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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.addArray(Board.java:49)
    Look at line 49 and see why the index is past the end of the array.


    recent modifications
    The code still is using the confusing x,y and row,col variables.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #128
    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

    damnit ... OK it kinda works now, but when I rotate an object there are 2 shapes that are created...
    lol



    public class Piece extends Grid {
    	int currentX;     // current X location on the board
    	int currentY;  // current Y location on the board
     
     
    	public Piece(int shape[][]) {
    		super(shape);
    		currentX = 7;
    		currentY = 5;
    		updateLocation();
    	}
     
     
     
    	void updateSize() {
    		setSize(Tetris.SQUARE_SIZE*getColumns(),
    				Tetris.SQUARE_SIZE*getRows());
    	}
     
    	void updateLocation() {
    		setLocation(Tetris.SQUARE_SIZE*currentX,
    					(int) (Tetris.SQUARE_SIZE*currentY));
    	}
     
    	synchronized void moveDown() {
     
    	}
     
    	synchronized void moveLeft() {
     
    		currentX--;
    		updateLocation();
     
    	}
     
    	synchronized void moveRight() {
     
    		currentX++;
    		updateLocation();
     
    	}
     
        synchronized void rotateClockwise(Board board) {
     
        	int [][] a = getContents();
    		int [][] b = board.getContents();
     
    	    if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    	    {
    	    int [][] transpose = new int[4][4];
    	    transpose = transpose(contents);
    	    contents = multiplyMatrix(transpose);
     
    	    updateLocation();
    	    updateSize();
    	    }
        }
     
     
     
     
    	synchronized void rotateCounterclockwise(Board board) {
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
    		if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    		{
    		int [][] transpose2 = new int[4][4];
    		transpose2 = multiplyMatrix(contents);
    		transpose2 = transpose(transpose2);
    		contents = transpose2;
     
    		updateLocation();
    	    updateSize();
    		}
    	}
     
    	void fall(Board board) {
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
     
    		while(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    		{
    			currentY++;
    			updateLocation();
    	    	updateSize();
    	    	Tetris.sleep(2000);
     
     
     
     
    		}
    		Board.addArray(currentX, currentY, b, a);
    		updateSize(); //necessary?
     
    		//Tetris.sleep(2000);
    	}
     
    	synchronized void drop(Board board) {
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
    		if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    		{
    		currentY++;
    		updateLocation();
    		}
    		else
    		{
    		Board.addArray(currentX, currentY, b, a);
    		updateLocation(); //necessary?
    		}
     
    	}
     
      public static int [][] transpose(int [][] m1)
      {
      	  int m = 4;
      	  int n = 4;
      	  int c = 0;
      	  int d = 0;
     
      	  int[][] transpose = new int [n][m];
     
     
      	  for ( c = 0 ; c < m ; c++ )
          {
    	      for ( d = 0 ; d < n ; d++ )
    	      {
    	         transpose[d][c] = m1[c][d];
    	      }
          }
     
          return transpose;
     
      }
     
      public static int [][] multiplyMatrix(int [][] m1)
      {
      		int [][] m2 =
    		{{0,0,0,1},
    		 {0,0,1,0},
    		 {0,1,0,0},
    		 {1,0,0,0},
    		};
     
     
        int[][] result = new int[4][4];
     
        // multiply
        for (int i=0; i<4; i++)
          for (int j=0; j<4; j++)
            for (int k=0; k<4; k++)
            result[i][j] += m1[i][k] * m2[k][j];
     
     
        return result;
      }
     
        }

    maybe it's because I called updateLocation 2 times sometimes.. I don't know or that rotate() and fall() both fills an array... I don't know..

  4. #129
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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 don't know
    Time for more debugging to see what the code is doing.

    synchronized void moveLeft() {
     
    		currentX--;
    That code says that x is a column. Not a row: board[x+row]
    If you don't understand my answer, don't ignore it, ask a question.

  5. #130
    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
    Time for more debugging to see what the code is doing.

    synchronized void moveLeft() {
     
    		currentX--;
    That code says that x is a column. Not a row: board[x+row]
    Any tip for debugging?

    Uhmm, I had called addArray in two different places, now I only have it in fall();

    and I remove updateLocation() in 2 places

    Now, it kinda works, but there's an unusual behavior with the "I" block when I rotate it...

    Do you have any idea what's causing this?


  6. #131
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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

    Any tip for debugging?
    I use println statements.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #132
    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 need to update shape when I rotate. Do you have any idea how to do this? Because it fills up the array as if they weren't rotated at all... Do I have to write a new method?

  8. #133
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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

    Sorry, I have no ideas on what methods you need or what should be in them.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #134
    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

    Some information that might be helpful

    In the Piece class, you'll want methods for moving the piece down, moving it left and right by one column, and for rotating the piece clockwise and counterclockwise 90 degrees. (You want these methods to be synchronized so that two modifications of the piece aren't attempted concurrently.) When a piece is rotated, the array changes shape (somewhat similar, but not exactly like, the transpose algorithm we did in class). Note: Each time you move a piece, call its updateLocation method so that it gets painted properly on the board. Similarly, each time you change the shape of a piece, call its updateSize method. You should only move or rotate a piece if it would be legal to do so. (The methods of the Board will be helpful for determining legality of a board position.)

     
    	public static void addArray(int currentX, int currentY, int[][] board, int[][] shape)
    	{
    	   int x = currentY;
    	   int y = currentX;
     
    	   for (int row = 0; row < 4; row++)
    	   {
    	   	for(int col = 0; col < 4; col++)
    	   	{
     
     
    	   		if (shape[row][col] == 1 )
    	   		{
     
    				board[x+row][y+col] = 1;
    	   		}
     
     
    	   	}
    	   }
    	}

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

        synchronized void rotateClockwise(Board board) {
     
        	int [][] a = getContents();
    		int [][] b = board.getContents();
     
    	    if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    	    {
    	    int [][] transpose = new int[4][4];
    	    transpose = transpose(contents);
    	    contents = multiplyMatrix(transpose);
     
    	    updateLocation();
    	    updateSize();
    	    }
        }
     
     
     
     
    	synchronized void rotateCounterclockwise(Board board) {
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
    		if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    		{
    		int [][] transpose2 = new int[4][4];
    		transpose2 = multiplyMatrix(contents);
    		transpose2 = transpose(transpose2);
    		contents = transpose2;
     
    		updateLocation();
    	    updateSize();
    		}
    	}

    i don't see anything wrong with the rotate methods

    how can i debug the rotate methods?

    --- Update ---

    it's because the method fall takes content before it rotates... so how do I fix this?

    int [][] a = getContents();

    --- Update ---

    while(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    {
    a = getContents();
    b = board.getContents();

    currentY++;
    updateLocation();
    updateSize();
    Tetris.sleep(2000);




    }

    ok fixed

    --- Update ---

    now I need to fix the error when the pieces goes out of bound in the left or in the right side...

    ummm... any suggestion??

    --- Update ---

    I should have writen a legal() method for both left and right and down... but i didn't and I can't think of a way to do that... now, i am kinda screwed up, because I can't think of a simple way to do this....

    --- Update ---

    ok the easiest way is to check for legalRight and legalLeft before checking for legalDown, right? I can't think of a better way? This way I won't get any exception right?

    --- Update ---

    I have a question... say we have

    a shape

    defined as

    1 0 0 0 0
    1 0 0 0 0
    1 0 0 0 0
    1 0 0 0 0

    Does it matter if 0 goes out of bound? No, right? It just that when you check for the board[][] the index for col must be between 0 and 15, right?

  10. #135
    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

    Help!! When I print the array of board, I only get 0s. Do you know why?

    The pieces aren't there...

  11. #136
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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

    Why do you think there should be non-0s in the array? Where are there non-0 values assigned to elements of the array?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #137
    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 nevermind. geeze. ummm when i rotate the objects the shape aren't updated for some reason and they land before they hit anything

    --- Update ---

    Quote Originally Posted by Norm View Post
    Why do you think there should be non-0s in the array? Where are there non-0 values assigned to elements of the array?
    umm i just commented out printarray for some reason

    --- Update ---



    yeah i think there is something wrong with my legal method, i think i need to update it inside by using the getter method...

    --- Update ---

    Hmm yeah, now the problem is that sometimes rotating it make it land before it should. sometimes it doesn't... do you hav any clue as to what is wrong? i think it has to do with the update methods

     
     
    public class Piece extends Grid {
    	int currentX;     // current X location on the board
    	int currentY;  // current Y location on the board
     
     
    	public Piece(int shape[][]) {
    		super(shape);
    		currentX = 7;
    		currentY = 5;
    		updateLocation();
    	}
     
     
     
    	void updateSize() {
    		setSize(Tetris.SQUARE_SIZE*getColumns(),
    				Tetris.SQUARE_SIZE*getRows());
    	}
     
    	void updateLocation() {
    		setLocation(Tetris.SQUARE_SIZE*currentX,
    					(int) (Tetris.SQUARE_SIZE*currentY));
    	}
     
    	synchronized void moveDown() {
     
    	}
     
    	synchronized void moveLeft() {
     
    		currentX--;
    		updateLocation();
     
    	}
     
    	synchronized void moveRight() {
     
    		currentX++;
    		updateLocation();
     
    	}
     
        synchronized void rotateClockwise(Board board) {
     
        	int [][] a = getContents();
    		int [][] b = board.getContents();
     
    	    if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    	    {
    	    int [][] transpose = new int[4][4];
    	    transpose = transpose(contents);
    	    contents = multiplyMatrix(transpose);
     
    	    updateLocation();
    	    updateSize();
    	    }
        }
     
     
     
     
    	synchronized void rotateCounterclockwise(Board board) {
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
    		if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    		{
    		int [][] transpose2 = new int[4][4];
    		transpose2 = multiplyMatrix(contents);
    		transpose2 = transpose(transpose2);
    		contents = transpose2;
     
    		updateLocation();
    	    updateSize();
    		}
    	}
     
    	void fall(Board board) {
    		int stopLoop = 0;
     
    		while(stopLoop == 0)
    		{
     
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
     
    		while(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    		{
    			a = getContents();
    		    b = board.getContents();
     
    			currentY++;
    			updateLocation();
    	    	updateSize();
    	    	Tetris.sleep(2000);
     
     
     
     
    		}
    		Board.addArray(currentX, currentY, b, a);
    		stopLoop++;
    		}
     
     
    		//Tetris.sleep(2000);
    	}
     
    	synchronized void drop(Board board) {
     
    		int [][] a = getContents();
    		int [][] b = board.getContents();
     
    		if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    		{
     
    		currentY++;
    		updateLocation();
    		}
     
     
     
    	}
     
      public static int [][] transpose(int [][] m1)
      {
      	  int m = 4;
      	  int n = 4;
      	  int c = 0;
      	  int d = 0;
     
      	  int[][] transpose = new int [n][m];
     
     
      	  for ( c = 0 ; c < m ; c++ )
          {
    	      for ( d = 0 ; d < n ; d++ )
    	      {
    	         transpose[d][c] = m1[c][d];
    	      }
          }
     
          return transpose;
     
      }
     
      public static int [][] multiplyMatrix(int [][] m1)
      {
      		int [][] m2 =
    		{{0,0,0,1},
    		 {0,0,1,0},
    		 {0,1,0,0},
    		 {1,0,0,0},
    		};
     
     
        int[][] result = new int[4][4];
     
        // multiply
        for (int i=0; i<4; i++)
          for (int j=0; j<4; j++)
            for (int k=0; k<4; k++)
            result[i][j] += m1[i][k] * m2[k][j];
     
     
        return result;
      }
     
        }


    --- Update ---

    Do you know why sometimes a block stops before it should? Is it because of simultaneous processes that conflict with each other or it's just my code? What are the possibilities.

    --- Update ---



    ok so the problem is in this:

    public static boolean legalDown(int currentX, int currentY, int[][] board, int[][] shape)
    	{
    	   int x = currentY+1;
    	   int y = currentX;
     
     
     
     
    	   //printArray(board);
     
     
     
     
     
    	   if (x > 28)
    	   {
     
     
    	   	return legalDownSpec(currentX, currentY, board, shape);
     
     
     
    	   }
     
    	   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
    	   			    {
    	   			    System.out.println("NOT spec");
     
    	   				return false;
    	   			    }
    	   			}
    	   		}
    	   	}
    	   }
     
     
     
     
    	    return true;
    	}

    I get: notspec like 6 times when it should have been only 1; furthermore, it stops a block before it should... What is the problem? I need an expert here...

  13. #138
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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

    why sometimes a block stops before it should?
    What does that mean? What is a block? What does "stops"mean for a block?

    What is the method: legalDown() supposed to do?

    I still see this mix of x(a column) with a variable named row: board[x+row]
    That makes no sense to me.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #139
    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

    Is it because of legalDown() and rotateCounterClockWise() running concurrently?

    --- Update ---

    the tetris block stops before it lands on another block or the floor...

    legalDown is supposed to say if it will land or not.

  15. #140
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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

    Those terms are for a user viewing the game being played.
    Try explaining the problem in programming terms using variable names and their contents, etc.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #141
    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

    0000000000000000
    0000000000000000
    0000000000000000
    0000000000000000
    0000000000000000
    0000000000000000
    0000000000000000
    0000000000000000
    0000000000000000
    0000000000000000
    0000000000000000
    0000001000000000
    0000011000000000
    0000010000000000
    0000000000000000
    0000000111110000
    0000000111000000

    It landed before it should have because of legalDown()...

  17. #142
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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

    It landed before it should
    See my last post.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #143
    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
    Those terms are for a user viewing the game being played.
    Try explaining the problem in programming terms using variable names and their contents, etc.
    currentPiece.contents should not stop falling (fall()) until it touches the last row of board or another contents

    --- Update ---

    Ohhhh... I knoww!!

    Can you tell me how I can do this:

    a = getContents();
    b = board.getContents();

    Inside of:

     
    	public static boolean legalDown(int currentX, int currentY, int[][] board, int[][] shape)
    	{
    	   int x = currentY+1;
    	   int y = currentX;
     
     
     
     
    	   printArray(board);
     
     
     
     
     
    	   if (x > 28)
    	   {
     
     
    	   	return legalDownSpec(currentX, currentY, board, shape);
     
     
     
    	   }
     
    	   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
    	   			    {
    	   			    System.out.println("NOT spec");
     
    	   				return false;
    	   			    }
    	   			}
    	   		}
    	   	}
    	   }
     
     
     
     
    	    return true;
    	}

    I need to update the variables..., but I can't since the method is static, but doing so would fix the problem.

  19. #144
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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 method is static
    Why static ? That would be OK for a method that does not need to access any instance variables in the class.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #145
    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

    for some reason i can't remove static...


    this doesn't work:

    board = board.getContents();
    shape = currentPiece.getContents();

    --- Update ---

    Ok, actually I could remove static, but these

    board = board.getContents();
    shape = currentPiece.getContents();

    still give me

    error: cannot find symbol
    error: cannot find symbol

  21. #146
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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 variables need to be defined and in scope at the statement where you try to access them.

    I made a suggestion earlier about writing a test program with some classes for testing this problem.
    You need to try that to see how to solve the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #147
    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

    How do we refer to the currentPiece object inside drop()?

    synchronized void drop(Board board) {

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


    if(Board.legalDown(currentX, currentY, b, a) && Board.isNotOutOfBoundDown(currentX, currentY, b, a))
    {

    currentY++;
    updateLocation();
    }



    }

    I wrote the following:

    Piece currentPiece = currentPiece; this inside


    but it doesn't work, is there any way to do this?

    --- Update ---

    Quote Originally Posted by Norm View Post
    The variables need to be defined and in scope at the statement where you try to access them.

    I made a suggestion earlier about writing a test program with some classes for testing this problem.
    You need to try that to see how to solve the problem.
    I do know how to access variables from other classes, but in this case, it a different story, because I have to access an Object that wasn't parsed as an argument inside the method from which I want to make the method call.

    --- Update ---

    If I write:

    drop(board, currentPiece) instead of

    currentPiece.drop(board);

    will the method still work as intended? I think the variables currentX and currentY won't be able to be accessed directly, right? So I would have to do:

    currentPiece.currentX++

    instead of

    currentX++

    right?

  23. #148
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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

    It is very hard to follow your posts.
    Can you write a smaller complete testing program with some classes, methods and variables that demonstrates the problem you are having?
    This is getting to be a waste of time trying to make sense of the isolated pieces of code that have been posted.
    Make a small complete program that shows the problem. Without that I'm outa here.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #149
    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
    It is very hard to follow your posts.
    Can you write a smaller complete testing program with some classes, methods and variables that demonstrates the problem you are having?
    This is getting to be a waste of time trying to make sense of the isolated pieces of code that have been posted.
    Make a small complete program that shows the problem. Without that I'm outa here.


    synchronized void drop(Board board) {

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


    //I don't have currentObject here... what do I need to write?


    if(Board.goDown(currentX, currentY, b, a, board, currentObject))
    {

    currentY++;
    updateXY();
    }



    }

    the method call is currentObject.drop(board) and we can't pass in currentObject as a variable.

    Is there any easy way of doing this or I just have to rewrite the entire code?

  25. #150
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 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

    Make a small complete program that shows the problem.
    This is my last post unless the requested testing code is posted that shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

Page 6 of 6 FirstFirst ... 456

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