Storage advice.

Ok, so I'm making Tetris in the console. ( Yes, in the console, before anyone wants to have a rant about using the console for unintended purposes... )

I just want some advice, tips or just plain "How you'd store..."

The picture below is how I'm storing the shapes, within a 2d array. shape[ 2 ][ 4 ].

http://s749.photobucket.com/albums/xx132/Bigdave876/Cpp/?action=view&current=Tetris.jpg

As you can see, when I move the line down( The second shape ) there are 0's( blank spaces ) below it. Is there a better way, where I can store the shape without all the extra space around them?

Thanks for any help/advice.

Edit:
I only ask this, as I'm creating these shapes via a class.
Last edited on
Is there any chance we might see some code snippets? I'm not sure exactly how you output those shapes to the console, but I'm guessing there's an extra endl somewhere (or whatever equivalent you are using as output)
My output is fine, I've made it exactly like that for testing purposes.

My problem, before I get to coding the movement etc. of the shapes, is that I want to know if there's are better ways to store the shapes.

Right now, I have them stored in a 2x4 array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const int shapeRow = 2;
const int shapeColumn = 4;

class Shapes
{
	public:
		Shapes( int type, int x, int y );
		~Shapes(void);

		/* snip */

	private:
		/* snip */
		int shape[ shapeRow ][ shapeColumn ];
};


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
Shapes::Shapes( int type, int x, int y ) : shapeType( type ), direction( UP )
{
	pos.X = x;
	pos.Y = y;

	for( int i = 0; i < shapeRow; ++i )
		for( int j = 0; j < shapeColumn; ++j )
			shape[ i ][ j ] = 0;

	setColour();

	createShape();

	draw();
}


Shapes::~Shapes(void)
{
}

void Shapes::createShape()
{
	switch( shapeType )
	{
		case 0: //Square.
			shape[ 0 ][ 1 ] = 1;
			shape[ 0 ][ 2 ] = 1;
			shape[ 1 ][ 1 ] = 1;
			shape[ 1 ][ 2 ] = 1;

			//temp output
			outputShapeArray();

			break;

		/* snip */
	}
}


Shapes created in main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	int x = 10;
	int y = 10;
	int i = 0;

	Shapes square( i++, x, y+=3 );
	Shapes line( i++, x, y+=3 );
	Shapes rStair( i++, x, y+=3 );
	Shapes lStair( i++, x, y+=3 );
	Shapes steps( i++, x, y+=3 );
	Shapes lShape( i++, x, y+=3 );
	Shapes blShape( i++, x, y+=3 );
	
	con.gotoXY( 0, 0 );
	con.wait( "System paused..." );

	return 0;
}
It's funny because I once made exactly this - Tetris in the console, coded in C++, using Windows functions for console manipulation :)

But I don't remember much, it was a long time ago. I only remember that console drawing was horribly slow, I made a library to render only those cells which actually had been changed.
Last edited on
I'm doing exactly that! lol. I've made a console class which I use for applications I make.
gotoXY( int x, int y ); functions etc.

Can you remember how you stored the shapes?
Last edited on
Ok, so I thought I'd explain this a bit better.

Rotation test.
http://s749.photobucket.com/albums/xx132/Bigdave876/Cpp/?action=view&current=rotationTest.jpg

Rotation test. With highlighted spaces( Dark Green ).
http://s749.photobucket.com/albums/xx132/Bigdave876/Cpp/?action=view&current=rotationTest_spaces.jpg

In the above screenshots, if you look at the bottom shape, in the first column( facing UP ).

You'll see:
███.
..█.

If I don't cout the spaces, the .'s above, I'll end up with a shape like so:
███
█

Now. If I'm moving this shape into position within an already started game, i.e.:

█    ███
████ ███
████████

Because I have to cout the spaces, I'll end up with the following:

█ ███.██
██..█.██
████████


Surely there's a more suitable way to store these shapes?
I've thought about just writing each shape to the console. i.e.:
1
2
3
4
5
6
7
8
9
10
11
gotoXY( xpos++, ypos )
std::cout << ( char )219;

gotoXY( xpos++, ypos );
std::cout << ( char )219;

gotoXY( xpos, ypos );
std::cout << ( char )219;

gotoXY( xpos, ++ypos );
std::cout << ( char )219;

But I'd rather not code that just yet, until I know I have too. Plus, that way I'd have to code it for each shape, then each rotation of that shape.

Someone please help! lol. I can't really code any further until this is sorted out. If I code the rotation then change from an array, to just cout'ing the shape, I'd have to recode it all.
You could store a shape as a list of points. For example, for the following shape:
XXX
  X
There would be 4 points (x,y): (0,0), (1,0), (2,0), (2,1).

Then, you could start with a 2D table filled with spaces, and gradually put shapes at the required positions. Once all shapes are put, you could render the table line by line.
Last edited on
Thanks for the reply. I have actually been thinking about doing it like that.
With: COORD ofset[ 4 ];

And looking at the shapes, it's not as bad as I though. As only 3 of the shapes have 4 facing directions, whereas the rest only have 2.

Guess that's the way it's going to have to be done... lol.
Just for future use, if someone stumbles across this post. Here's the code I've used to create the shapes.

Within my Shapes class, I have declared:
COORD offset[ 4 ];

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
void Shapes::createShape()
{
	for( int i = 0; i < 4; ++i )
	{
		offset[ i ].X = pos.X;
		offset[ i ].Y = pos.Y;
	}

	switch( shapeType )
	{
		case 0: //Square.
			offset[ 1 ].X = pos.X + 1;

			offset[ 2 ].Y = pos.Y + 1;

			offset[ 3 ].X = pos.X + 1;
			offset[ 3 ].Y = pos.Y + 1;

			break;

		case 1: //Line
			for( int i = 1; i < 4; ++i )
				offset[ i ].Y = pos.Y + i;

			break;

		case 2: //Right stair.
			++offset[ 0 ].X;
			
			offset[ 1 ].X += 2;

			++offset[ 2 ].Y;

			++offset[ 3 ].X;
			++offset[ 3 ].Y;

			break;

		case 3: //Left stair.
			++offset[ 1 ].X;

			++offset[ 2 ].X;
			++offset[ 2 ].Y;

			offset[ 3 ].X += 2;
			++offset[ 3 ].Y;

			break;

		case 4: //Steps
			++offset[ 0 ].X;

			++offset[ 1 ].Y;

			++offset[ 2 ].X;
			++offset[ 2 ].Y;

			offset[ 3 ].X += 2;
			++offset[ 3 ].Y;

			break;

		case 5: //L shape.
			offset[ 0 ].X += 2;

			++offset[ 1 ].Y;

			++offset[ 2 ].X;
			++offset[ 2 ].Y;
			
			offset[ 3 ].X += 2;
			++offset[ 3 ].Y;

			break;
	
		case 6: //Backwards L.
			++offset[ 1 ].X;

			offset[ 2 ].X += 2;

			offset[ 3 ].X += 2;
			++offset[ 3 ].Y;

			break;
	}
}


Thanks for the replies, Abramus.
Topic archived. No new replies allowed.