Putting pieces inside a board

Started by
21 comments, last by concepts 11 years, 2 months ago

synchronized void moveLeft() {



currentX--;

updateLocation();

board.method(); //method puts an array inside a bigger array



}

if I want to put shapes inside board, this is what must be done, right?

Advertisement
If you mean writing the contents of a piece array into the board array, I think it should happen only when the pieces settles down, and not when it is still moving. So I don't think it belongs in the moveLeft method, unless I am misunderstanding you again.

    public Board() {
        super(new int[ROWS][COLUMNS]);
        setSize(COLUMNS*Tetris.SQUARE_SIZE,
                ROWS*Tetris.SQUARE_SIZE);
    }

 

How do I access int[ROWS][COLUMNS] outside?

I tried the following, but it doesn't work...


        while(legalDown(currentX, currentY, board.contents, currentPiece.contents) && isNotOutOfBoundDown(currentX, currentY, board.contents, currentPiece.contents))
        {
        currentX--;
        updateLocation();
        updateSize();
        }
 
Ok, now I see what you mean. The array you are looking for is in the Grid class. You don't see it in the Board class because Grid has it. Is the Grid class available to you? You will have to see which methods of the Grid class allows you to change the array.
The board class is a grid itself. What does the grid class look like. Does it have operator to allow access to the data like a normal array? Or functions to do so.

I would keep the falli g piece seperate from the board untill its done falling. Then copy the contents of your pieces to the correct spot on the board.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.

    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 < 28 )
        {
        currentY++;
        updateLocation();
        }
        else
        {
        Board.addArray(currentX, currentY, b, a);
        }

    }
 


	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);
			}
			else
			{

			Board.addArray(currentX, currentY, b, a);

			}
		}

		//Tetris.sleep(2000);
	}

	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;
	   		}


	   	}
	   }
	}

	public static boolean legalDown(int currentX, int currentY, int[][] board, int[][] shape)
	{
	   int x = currentY+1;
	   int y = currentX;

	   for (int row = 0; row < 4; row++)
	   {
	   	for(int col = 0; col < 4; col++)
	   	{
	   		if(x + row == 32)
	   		{
	   			return false;
	   		}


	   		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 isNotOutOfBoundDown(int currentX, int currentY, int[][] board, int[][] shape)
	{
	   int x = currentX+1;
	   int y = currentY;
	   int lowestRow = 0;

	   for (int row = 0; row < 4; row++)
	   {
	   	for(int col = 0; col < 4; col++)
	   	{
	   		if (shape[row][col] == 1)
	   		{
	   			lowestRow = row+1;
	   			System.out.println(lowestRow);

	   		}





	    }
	   }

	   if (x + lowestRow > board.length)
	   {
	   	return false; //can't do it
	   }
	   else
	   {
	   	return true;
	   }
	}




Ok, I have some trouble. I get array out of bound exception if I go all the way down, because my method legalDown checks below the grid. So I could only fix it partially by making it stop at 31, then it froze so I fixed it by making it stop at 28 and then some pieces go below 28 after I rotate them. Now, I don't know what to do... Help! And how do I know that a piece landed? Like I fill the array at the end, but it doesn't seem to work that well at certain times...

If, after a rotation, a piece goes underground, then don't allow the rotation to happen. You can either first check that the surrounding area is clear before rotating, or you can rotate and undo if it goes underground.

A shape is considered landed if it overlaps with the board after a drop or a movedown.

It goes underground just because I rotated it earlier and not as I rotate it.

I know that, but it seems my code still doesn't work, so...

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..

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?

This topic is closed to new replies.

Advertisement