Thread: Need to pass a pointer of a unknown sized 2d array

  1. #1
    Registered User
    Join Date
    Dec 2005

    Need to pass a pointer of a unknown sized 2d array

    Ok, so I've read allot about this problem but still cant figure it out.

    Basically Im making a Tetris game, I have a function 'getRandomPiece()' which I would like to return a random Tetris piece which is a 2d array. Problem is, not all pieces are the same dimensions (same width and height).

    So after allot of reading it seems c++ cant return an array.

    So then ill try passing a pointer to store the array. The problem with this is c++ wants me to declare the size of the array in the parameter which I dont know what its going to be yet.

    Ideally in Java I can just go

    int[][] getRandomPiece(){
          //Random logic
         int[][] arr = {{1,1,0},
          return arr;
    Which is the best way to solve this problem?

  2. #2
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    London, ON
    I'd just use a vector of vectors and avoid this entire headache.

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Plano, Texas, United States
    So this array represents the shape of the random piece, then?

    I don't know how large scale this tetris game your making is, but a simple "piece" class would go a long ways to help out on such a problem. There are several ways you could approach this.

    You could make a seperate class for each type of piece, and have them all derive from the base piece class. Then each piece would automatically store a 2d array with its shape inside. For example:

    class LSHAPE: public PIECE
    int shape [][] = { {011}, {001}, {001} };
    ...then comes the rest of the class...
    You have to keep in mind that the pieces will be rotating, and therefore this matrix wont be staying the same.

    Essentially the most states any piece could have is 4 different states, and so you could have 4 different matrixes, and then a variable that flags which matrix should be used for the display of pieces in your tetris game.

    If you dont want to hold 4 different matrixes, I think some bitwise operations could be done to "rotate" the matrix.

    Now, back to the problem at hand. If you created a seperate derived class for each piece, you could have you random logic that selects a piece, and then just return that piece (which already contains its correct matrix) instead of returning a matrix.


    PIECE *randomPiece ( )
    ...random piece logic...
    switch ( logic result )
    case LSHAPE_PIECE:
        return new LSHAPE();
    Does this help?
    My Website

    "Circular logic is good because it is."

  4. #4
    Registered User
    Join Date
    Dec 2005
    It does help, although in Java I have already written a algorithm to rotate a 2d array and would rather use that than pre-calculated rotations.

    I guess each class such as LShape could have its own rotate function which only rotated its own shape...

    Thanks for the ideas both of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array pointer
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-30-2008, 12:30 PM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM