- 类的单一职责是
World
什么?它只是一个包含几乎所有功能的 blob。这不是好的设计。一个明显的责任是“代表放置块的网格”。但这与创建 tetroids 或操纵黑名单或绘图无关。事实上,大部分内容可能根本不需要在课堂上。我希望该World
对象包含BlockList
您调用的 StaticBlocks,以便它可以定义您正在播放的网格。
- 为什么要定义自己的
Blocklist
?你说你希望你的代码是通用的,那么为什么不允许使用任何容器呢?std::vector<Block>
如果我愿意,为什么我不能使用?或者一个std::set<Block>
,或者一些自制的容器?
- 使用不重复信息或自相矛盾的简单名称。
TranslateTetroid
不翻译 tetroid。它翻译阻止列表中的所有块。所以它应该是TranslateBlocks
什么的。但即使这样也是多余的。我们可以从签名(它需要一个BlockList&
)中看到它适用于块。所以就叫吧Translate
。
- 尽量避免使用 C 风格的注释 (
/*...*/
)。C++ 风格 ( //..
) 的表现要好一些,因为如果您使用 C 风格注释整个代码块,如果该块也包含 C 风格注释,它将中断。(作为一个简单的例子,/*/**/*/
这是行不通的,因为编译器会将第一个*/
视为注释的结尾,因此最后一个*/
不会被视为注释。
- 所有(未命名的)
int
参数是什么?它使您的代码无法阅读。
- 尊重语言特性和约定。复制对象的方法是使用其复制构造函数。因此,与其给出一个
CopyTetroid
函数,不如给出BlockList
一个复制构造函数。然后,如果我需要复制一个,我可以简单地做BlockList b1 = b0
.
- 而不是
void SetX(Y)
andY GetX()
方法,删除多余的 Get/Set 前缀并简单地使用void X(Y)
and Y X()
。我们知道它是一个 getter,因为它不接受参数并返回一个值。我们知道另一个是 setter,因为它接受一个参数并返回 void。
BlockList
不是一个很好的抽象。您对“当前 tetroid”和“当前网格上的静态块列表”有非常不同的需求。静态块可以用一个简单的块序列来表示(尽管行序列或二维数组可能更方便),但当前活动的 tetroid 需要额外的信息,例如旋转中心(不属于World
)。
- 一种表示 tetroid 并简化旋转的简单方法可能是让成员块存储与旋转中心的简单偏移量。这使得旋转更容易计算,并且意味着成员块在翻译过程中根本不需要更新。只需移动旋转中心即可。
- 在静态列表中,块知道它们的位置甚至效率不高。相反,网格应该将位置映射到块(如果我问网格“哪个块存在于 cell 中
(5,8)
,它应该能够返回块。但块本身不需要存储坐标。如果有,它可以成为维护难题。如果由于一些细微的错误,两个块最终具有相同的坐标怎么办?如果块存储自己的坐标,但如果网格包含哪个块在哪里的列表,则不会发生这种情况。)
- 这告诉我们,我们需要一种表示“静态块”,另一种表示“动态块”(它需要存储距 tetroid 中心的偏移量)。事实上,“静态”块可以归结为基本要素:网格中的一个单元格包含一个块,并且该块具有颜色,或者它不包含块。没有与这些块相关联的进一步行为,因此也许应该对放置它的单元进行建模。
- 我们需要一个代表可移动/动态 tetroid 的类。
- 由于您的许多碰撞检测都是“预测性的”,因为它处理“如果我将对象移到这里会怎样”,因此实现非变异平移/旋转功能可能更简单。这些应该使原始对象保持不变,并返回旋转/翻译的副本。
因此,这是您代码的第一次传递,只需重命名、注释和删除代码,而无需过多更改结构。
class World
{
public:
// Constructor/Destructor
// the constructor should bring the object into a useful state.
// For that, it needs to know the dimensions of the grid it is creating, does it not?
World(int width, int height);
~World();
// none of thes have anything to do with the world
///* Blocks Operations */
//void AppendBlock(int, int, BlockList&);
//void RemoveBlock(Block*, BlockList&);;
// Tetroid Operations
// What's wrong with using BlockList's constructor for, well, constructing BlockLists? Why do you need NewTetroid?
//void NewTetroid(int, int, int, BlockList&);
// none of these belong in the World class. They deal with BlockLists, not the entire world.
//void TranslateTetroid(int, int, BlockList&);
//void RotateTetroid(int, BlockList&);
//void CopyTetroid(BlockList&, BlockList&);
// Drawing isn't the responsibility of the world
///* Draw */
//void DrawBlockList(BlockList&);
//void DrawWalls();
// these are generic functions used to test for collisions between any two blocklists. So don't place them in the grid/world class.
///* Collisions */
//bool TranslateCollide(int, int, BlockList&, BlockList&);
//bool RotateCollide(int, BlockList&, BlockList&);
//bool OverlapCollide(BlockList&, BlockList&); // For end of game
// given that these functions take the blocklist on which they're operating as an argument, why do they need to be members of this, or any, class?
// Game Mechanics
bool AnyCompleteLines(BlockList&); // Renamed. I assume that it returns true if *any* line is complete?
bool IsLineComplete(int line, BlockList&); // Renamed. Avoid ambiguous names like "CompleteLine". is that a command? (complete this line) or a question (is this line complete)?
void ColourLine(int line, BlockList&); // how is the line supposed to be coloured? Which colour?
void DestroyLine(int line, BlockList&);
void DropLine(int, BlockList&); // Drops all blocks above line
// bad terminology. The objects are rotated about the Z axis. The x/y coordinates around which it is rotated are not axes, just a point.
int rotationAxisX;
int rotationAxisY;
// what's this for? How many rotation states exist? what are they?
int rotationState; // Which rotation it is currently in
// same as above. What is this, what is it for?
int rotationModes; // How many diff rotations possible
private:
int wallX1;
int wallX2;
int wallY1;
int wallY2;
};
// The language already has perfectly well defined containers. No need to reinvent the wheel
//class BlockList
//{
//public:
// BlockList();
// ~BlockList();
//
// Block* GetFirst();
// Block* GetLast();
//
// /* List Operations */
// void Append(int, int);
// int Remove(Block*);
// int SearchY(int);
//
//private:
// Block *first;
// Block *last;
//};
struct Colour {
int r, g, b;
};
class Block
{
public:
Block(int x, int y);
~Block();
int X();
int Y();
void Colour(const Colour& col);
void Translate(int down, int left); // add parameter names so we know the direction in which it is being translated
// what were the three original parameters for? Surely we just need to know how many 90-degree rotations in a fixed direction (clockwise, for example) are desired?
void Rotate(int cwSteps);
// If rotate/translate is non-mutating and instead create new objects, we don't need these predictive collision functions.x ½
//// Return values simulating the operation (for collision purposes)
//int IfTranslateX(int);
//int IfTranslateY(int);
//int IfRotateX(int, int, int);
//int IfRotateY(int, int, int);
// the object shouldn't know how to draw itself. That's building an awful lot of complexity into the class
//void Draw();
//Block *next; // is there a next? How come? What does it mean? In which context?
private:
int x; // position x
int y; // position y
Colour col;
//int colourR;
//int colourG;
//int colourB;
};
// Because the argument block is passed by value it is implicitly copied, so we can modify that and return it
Block Translate(Block bl, int down, int left) {
return bl.Translate(down, left);
}
Block Rotate(Block bl, cwSteps) {
return bl.Rotate(cwSteps);
}
现在,让我们添加一些缺失的部分:
首先,我们需要表示“动态”块、拥有它们的 tetroid 以及网格中的静态块或单元格。(我们还将向世界/网格类添加一个简单的“碰撞”方法)
class Grid
{
public:
// Constructor/Destructor
Grid(int width, int height);
~Grid();
// perhaps these should be moved out into a separate "game mechanics" object
bool AnyCompleteLines();
bool IsLineComplete(int line);
void ColourLine(int line, Colour col);Which colour?
void DestroyLine(int line);
void DropLine(int);
int findFirstInColumn(int x, int y); // Starting from cell (x,y), find the first non-empty cell directly below it. This corresponds to the SearchY function in the old BlockList class
// To find the contents of cell (x,y) we can do cells[x + width*y]. Write a wrapper for this:
Cell& operator()(int x, int y) { return cells[x + width*y]; }
bool Collides(Tetroid& tet); // test if a tetroid collides with the blocks currently in the grid
private:
// we can compute the wall positions on demand from the grid dimensions
int leftWallX() { return 0; }
int rightWallX() { return width; }
int topWallY() { return 0; }
int bottomWallY { return height; }
int width;
int height;
// let this contain all the cells in the grid.
std::vector<Cell> cells;
};
// represents a cell in the game board grid
class Cell {
public:
bool hasBlock();
Colour Colour();
};
struct Colour {
int r, g, b;
};
class Block
{
public:
Block(int x, int y, Colour col);
~Block();
int X();
int Y();
void X(int);
void Y(int);
void Colour(const Colour& col);
private:
int x; // x-offset from center
int y; // y-offset from center
Colour col; // this could be moved to the Tetroid class, if you assume that tetroids are always single-coloured
};
class Tetroid { // since you want this generalized for more than just Tetris, perhaps this is a bad name
public:
template <typename BlockIter>
Tetroid(BlockIter first, BlockIter last); // given a range of blocks, as represented by an iterator pair, store the blocks in the tetroid
void Translate(int down, int left) {
centerX += left;
centerY += down;
}
void Rotate(int cwSteps) {
typedef std::vector<Block>::iterator iter;
for (iter cur = blocks.begin(); cur != blocks.end(); ++cur){
// rotate the block (*cur) cwSteps times 90 degrees clockwise.
// a naive (but inefficient, especially for large rotations) solution could be this:
// while there is clockwise rotation left to perform
for (; cwSteps > 0; --cwSteps){
int x = -cur->Y(); // assuming the Y axis points downwards, the new X offset is simply the old Y offset negated
int y = cur->X(); // and the new Y offset is the old X offset unmodified
cur->X(x);
cur->Y(y);
}
// if there is any counter-clockwise rotation to perform (if cwSteps was negative)
for (; cwSteps < 0; --cwSteps){
int x = cur->Y();
int y = -cur->X();
cur->X(x);
cur->Y(y);
}
}
}
private:
int centerX, centerY;
std::vector<Block> blocks;
};
Tetroid Translate(Tetroid tet, int down, int left) {
return tet.Translate(down, left);
}
Tetroid Rotate(Tetroid tet, cwSteps) {
return tet.Rotate(cwSteps);
}
我们需要重新实现推测性碰撞检查。给定非变异的 Translate/Rotate 方法,这很简单:我们只创建旋转/平移的副本,并测试它们的碰撞:
// test if a tetroid t would collide with the grid g if it was translated (x,y) units
if (g.Collides(Translate(t, x, y))) { ... }
// test if a tetroid t would collide with the grid g if it was rotated x times clockwise
if (g.Collides(Rotate(t, x))) { ... }