concepts

Members
  • Content count

    29
  • Joined

  • Last visited

Community Reputation

122 Neutral

About concepts

  • Rank
    Member
  1. Putting pieces inside a board

    Can someone 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.
  2. Putting pieces inside a board

    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...
  3. Collision detection

    Some information that might be helpful [B]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.)[/B]     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?
  4. Collision detection

    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.  
  5. Putting pieces inside a board

    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.  
  6. Putting pieces inside a board

    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?
  7. Putting pieces inside a board

    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..
  8. Collision detection

    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
  9. Collision detection

    It somewhat works, but I need the pieces to move all the way down...  
  10. Collision detection

    http://www.gamedev.net/topic/638951-putting-pieces-inside-a-board/   help. i don't know what's the problem
  11. 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
  12. Putting pieces inside a board

    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...
  13. Collision detection

    why?
  14. Putting pieces inside a board

        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...
  15. Putting pieces inside a board

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