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?
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?
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();
}
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...
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;
}
}