• ### What is your GameDev Story?

Public Group

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

## 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 on other sites
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 on other sites
Quote:
 Original post by c4c0d3m0nThere 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.

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

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]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• ### Forum Statistics

• Total Topics
634071
• Total Posts
3015340
×