Jump to content
  • Advertisement
Sign in to follow this  
c4c0d3m0n

Tetris headache [C++]

This topic is 3868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everybody. I've been programming Tetris in C++ to teach myself a thing or two about OOP. My basic game design is this: There is a Field, which is a int-Matrix of 20*10. Every int represents a block, 0 represents empty space. There is a Player, which handles moving tetrominoes, having them fall over time, and adding a tetrominoe to the Field if it hits anything from below. Right now, the design of my Tetrominoe class is bogus. It's a class with a 4*4 bool-Matrix which represents the form of the Tetrominoe. It also has coordinates and an int representing the type of tetrominoe we have here. This design has major implications, since not every Tetrominoe type needs all that space in the 4*4 Matrix. I have functions returning the amount of free space on each side of the Tetro, per row, per column, etc. Right now, I can't get the Player to check wether or not a Tetrominoe hit something in the Field, because of the crazy hazyness of function calls in if's based on crazy +-<> mathematics. My question is, what is the best way to design a Tetrominoe class? I'm glad I can get away with my crazy code by only rewriting two classes...

Share this post


Link to post
Share on other sites
Advertisement
Represent a piece by an W x H matrix of boolean values, a width-height pair (W,H), and a center position (x,y).

If a piece exists at position (x',y') within your world, then you need to check whether world[x'+i][y'+j] && piece[x+i][y+i] is true for any pair (i,j) such that the coordinates (x'+i,y'+j) and (x+i,y+i) are both valid, which will tell you if a collision exists.

bool collision(const piece &p, const world &w, const post &c)
{
for (int i = -p.x; i < p.width - p.x; ++i)
if (i + c.x >= 0 && i + c.x < w.width)
for (int j = -p.y; j < p.height - p.y; ++j)
if (j + c.y >= 0 && j + c.y < w.height)
if (p(p.x+i,p.y+j) && w(c.x+i,c.y+j))
return true;

return false;
}


I've left the implementation of operator() to you.

Share this post


Link to post
Share on other sites
Quote:
Original post by c4c0d3m0n
There is a Player, which handles moving tetrominoes, having them fall over time, and adding a tetrominoe to the Field if it hits anything from below.

I'm not sure how that can be justified as the job of the Player?

Quote:
Right now, the design of my Tetrominoe class is bogus. It's a class with a 4*4 bool-Matrix which represents the form of the Tetrominoe. It also has coordinates and an int representing the type of tetrominoe we have here. This design has major implications, since not every Tetrominoe type needs all that space in the 4*4 Matrix.

That doesn't sound so bad.

Quote:
I have functions returning the amount of free space on each side of the Tetro, per row, per column, etc.

How about just the width and height of the actual tetrad?
It's more intuitive to think about how much space a tetrad is taking up than the amount of space a tetrad is not taking up.

Quote:
Right now, I can't get the Player to check wether or not a Tetrominoe hit something in the Field

It should be the field that does the checking of whether or not the falling tetrad hit something, the Player could query the field with a hasCollided function.

Quote:
My question is, what is the best way to design a Tetrominoe class?

It's hard to identify the best way, you might go for the simplest, which is to store a 4x4x4 array for each of the 4 orientations of a tetrad.

Regardless, it's the interface you need to pay the most attention to as the underlying implementation shouldn't affect this - have a getBlock(x, y) function, or something, that returns the block value at a given location. Or another way is to return an iterator over the blocks.

Quote:
I'm glad I can get away with my crazy code by only rewriting two classes...

I'd say that's one class too many [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!