Tetris Pieces

I'm working on doing a tetris game and i started to prototype everything but the problem i'm having is with the pieces.

I have a 10x10 array defined for the game board, initialized to zero at first. I thought about two ways to do this:

1. Store pieces and rotations in an array and update the board each button press (for rotation) and line clear (for score).

Conclusion: This would be very experimental for me, im new to C++ and i just took 'C++ I' at the college i'm attending and i dont know if i can declare an array (the board) and then place the pieces (another array) inside of one another and then update that effeciently. I think even if it was plausible that its just bad programming and it shouldnt be as complex, in short it can be simplified for better results.

2. Using an update function in the game, update every second or so.......

This is where i'm lost. Either i try silly idea 1 or idea 2 but i cant think of how to use an update function to control the pieces.

Any help here?

Note: Prototypes but not code is appreciated as i'm learning and just giving me the code out right doesnt help me learn. I just want to know how thats done (in any fashion) and how to think about this in a way that makes sense because it isnt clicking for me, i need some direction here.
Last edited on
Are you doing this all in the command line?

It would seem to me that you would have one blank array to start the board. This would be a 10 by 10 2D array that was all initialize to blank spaces. Then every piece would have to have four separate 2D arrays for each 90 degree rotation. You would start with your blank array. Then write the next piece to the board array. Use an update function like you said that would be a on a one second timer or so. Then basically every movement, the board array would be erased and a new board draw at the new coordinates with the new piece. If the player turns the piece, erase the board and write a new board with the rotation.

I've never done anything like this but I suppose I would start there.

However, if you are interested in programming games, you should look into the Allegro game library. It's an awesome free 2D programming library. That's what I've been messing with for the last year or so. Good luck!
So basically i need to erase the board and redraw every update, i got that.

Now how would you write one array to another? Thats what tripped me up before. Do i just write a function that would write to the board the data from those locations in the piece array? I cant just say: writefromarray to anotherarray(alldata); or something like that.

Last edited on
Try something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;

int main()
{
   char board[10][10];
   char temp_board[10][10];
   
   // initialize the board to blank pieces
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           board[i][j] = ' ';
       }
   }
   
   // do this for all pieces, that is, create a 10x10 blank array 
   // with x's for a piece, or maybe different letters to distinguish
   // between them later
   
   // now load a piece
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           board[i][j] = piece[i][j];
       }
   }
   // So each spot on the board equals the piece
   // then maybe as the piece moves down
   
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           board[i][j+1] = piece[i][j];
           if ( j+1 > 10)
               break; // so you don't exceed the bounds of the array
       }
   }
   // I may not have the indexes on the arrays right.  I don't have time to 
   // test it at the moment.  But add 1 to the y value of the array
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           temp_board[i][j] = board[i][j];
           board[i][j] = ' '; // to clear the board
       }
   }
   
   // then do this again
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           board[i][j+2] = piece[i][j];
           if ( j+2 > 10)
               break; // so you don't exceed the bounds of the array
       }
   }
   // now add 2 to the y value of the array.  You should probably make this
   // a variable that you increment every time.  Then if the bottom of the piece
   // reaches the bottom of the board, save the board like this and start a 
   // new piece
   return 0;
}


This is just a quick idea of how the game might go. Don't take any of this as real code or even the best option. This is just what came to mind and I suppose its where I might start.
You could also use a different approach - instead of having a matrix with the pieces, why not have a 4-element array with vectors to the bricks (for a lack of a better word) from a certain orientation point in your piece (for example the center or the top left corner)? That's a bit easier to deal with, IMHO.
Have a matrix storing dead pieces (those you can't move) and store the type, the position and rotation of the current piece falling. At each iteration, draw the matrix, calculate where the piece is and draw it.
store the type, the position and rotation


We already have a type system. In any case, saving the rotation you're at is redundant because rotating a piece is trivial.
Hanst99, i need to make one thing clear. Your seem like you know what your doing and i dont degrade that by any means. I hope to have your level of expertise on the issue. However, i believe you are being arrogant and only thinking about programming not the fact that i'm on the rise of learning.

I have to be able to code something efficient and relevant and to understand that i have to go to basics first and build from there. Then we can talk about vectors and redundancy. My point is the same one made about Visual for beginning programmers, the standards are so high a beginning programmer could never execute without some error. Its the same for you, i kindly ask that you bring your standards down to that of a beginner and not that of a high level programmer.

Duoas and other arrogant programmers need to remember what its like to be confused, and how it feels to struggle when making a programming and having a hard time even just understanding these concepts let alone use them.

That being said though, i agree. The code's redundant but seems efficent enough to run. A good start anyway. Thanks for the ideas and i will let you know what works. By the way rotating a piece is trivial. We just have to pick a point that is nuetral to the piece For Example:

1
11
1
(is a Z piece)

Then we can say the nuetral part here is the 1 under the very first one. This piece will remain nuetral no matter how we turn the piece so we can use this as a jump off point.

11
11
(one rotation to the right)

Take note that the piece's one i specified before hasnt changed position relative to the piece and not the space its in. So we can say that to rotate the piece we just need to make it different from the ones and zeros, then every time a button is pressed the piece is rotated as such, imagine a fulcrum if you will.

1
21
1

Now when we want to rotate the piece we designate 2 as stationary and rotate everything around it. While this idea may seem flawed right now, i'm sure if i worked on it more i could have a very trivial rotation system.


Edit: Please account for white space. It seems the site doesnt so my pieces look weird when i put them up.
Last edited on
Topic archived. No new replies allowed.