Which part?
Collision detection
It's not working as expected. There are plenty of bug that are caused by the fact that the legalDown method may go out of bound by checking what's outside of bound
http://www.gamedev.net/topic/638951-putting-pieces-inside-a-board/
help. i don't know what's the problem
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));
}
}
I added legalDownSpec to solve the problem, but I get another problem instead. Actually, I still get Array Out Of Bound, but it seems to stop earlier. I think I need another method to check if the piece is out of bound
the problem has to do with the rotate method that doesn't update the shape...
do you know how i can fix this, because I don't know which method I have to change.
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?