我目前正在研究Conway的C ++生活游戏的控制台版本。
问题在于我的Draw()方法正在尽可能快地绘制下一代。
我记得在某处读到使用GotoXY与获取控制台缓冲区相比非常慢。
问题是我没有一个概念如何实现控制台缓冲区来处理我的代码。
我并没有要求你们这样做,但我只是想让你看看我的Draw()和Update()方法,看看我是否正在做一些可怕的内存占用。 / p>
我的代码可能不是最好的,因为我仍然是#34;相当新的"到C ++,所以在批评之前要记住这一点。 :&#39)
细胞结构
struct Cell
{
int x, y; // Cell X, Y coordiantes
bool IsAlive; // Cell life state
string ToString()
{
return "X: " + to_string(x) + "\tY" + to_string(y) + "\tAlive State: " + to_string(IsAlive);
}
void Die()
{
IsAlive = false;
}
void Resurrect()
{
IsAlive = true;
}
};
更新
void Update()
{
// Update Cells
CalculateNextGeneration(CellMap);
}
绘制
void Draw()
{
for (auto cell : CellMap) // Iterate through all cells in CellMap vector
{
// Draw Cell if Alive
// If a cell was alive upto 10th generation, display cell as '1'.
if (cell.IsAlive)
GotoXY(cell.x, cell.y, AliveCell);
// If a cell is dead, display cell as ' '.
else
GotoXY(cell.x, cell.y, DeadCell);
}
}
CalculateNextGeneration
/*
TODO: Encapsulate IF Statements
Game Rules
1: Any Live Cell which has < 2 Live Neighbours, die [ Underpopulation ]
2: Any Live Cell which has 2 OR 3 Neighbours, live
3: Any Live Cell which has > 3 Neighbours, die [ Overpopulation ]
4: Any Dead Cell which has EXACTLY 3 Neighbours, resurrect [ Reporduciton ]
*/
void CalculateNextGeneration(vector<Cell> &map)
{
for (auto& cell : map) // Iterate through all cells as references
{
if ((cell.IsAlive && GetAdjacentCellCount(cell, map) < 2) || (cell.IsAlive && GetAdjacentCellCount(cell, map) > 3)) // If current cell has < 2 neighbours OR current cell has > 3 neighbours, die [ Underpopulation & Overpopulation ]
{
// Die
cell.Die();
}
if (cell.IsAlive && GetAdjacentCellCount(cell, map) == 2 || GetAdjacentCellCount(cell, map) == 3) // If current cell has 2 OR 3 adjacent neighbours, live until next generation.
{
// Live Until Next Generation
}
if (!cell.IsAlive && GetAdjacentCellCount(cell, map) == 3) // If current cell has EXACTLY 3 adjacent neighbours, resurrect [ Reproduciton ]
{
// Resurrect
cell.Resurrect();
}
}
}
GetAdjacentCellCount
/* Count Adjacent cells
Long version of counting adjacent cells.
TODO: Clean up code.
Example:
0 = Dead Cell
1 = Alive Cell
X = Current Cell
1 0 1
0 X 0
1 1 0
Return would be 4 in this case. Since There are 4 alive cells surround the current cell (X).
The function doesn't consider the current cell's life state.
*/
int GetAdjacentCellCount(Cell ¤tCell, vector<Cell> &map)
{
int aliveCount = 0;
int currentX = currentCell.x;
int currentY = currentCell.y;
vector<Cell> adjacentCells; // Create temporary vector with all adjacent cells
adjacentCells.push_back(GetCellAtXY(currentX - 1, currentY - 1, map)); // - - // TOP LEFT CELL
adjacentCells.push_back(GetCellAtXY(currentX, currentY - 1, map)); // 0 - // TOP MIDDLE CELL
adjacentCells.push_back(GetCellAtXY(currentX + 1, currentY - 1, map)); // + - // TOP RIGHT CELL
adjacentCells.push_back(GetCellAtXY(currentX + 1, currentY, map)); // + 0 // MIDDLE LEFT CELL
adjacentCells.push_back(GetCellAtXY(currentX + 1, currentY + 1, map)); // + + // MIDDLE RIGHT CELL
adjacentCells.push_back(GetCellAtXY(currentX, currentY + 1, map)); // 0 + // BOTTOM LEFT CELL
adjacentCells.push_back(GetCellAtXY(currentX - 1, currentY + 1, map)); // - + // BOTTOM MIDDLE CELL
adjacentCells.push_back(GetCellAtXY(currentX - 1, currentY, map)); // - - // BOTTOM RIGHT CELL
for (auto adjCell : adjacentCells) // Iterate through all adjacent cells
{
if (adjCell.IsAlive == true) // Count how many are alive
aliveCount++;
}
return aliveCount;
}
GetCellAtXY
Cell GetCellAtXY(int x, int y, vector<Cell> &map)
{
Cell retrievedCell = { 0, 0, false }; // Create a default return cell
for (auto cell : map) // Iterate through all cells in the map
{
if (cell.x == x && cell.y == y) // If Cell is found at coordinate X, Y
retrievedCell = cell; // Set found Cell to return Cell
}
return retrievedCell;
}
答案 0 :(得分:1)
您可能会花费大量时间重写GetAdjacentCellCount
功能:
if (GetCellAtXY(currentX - 1, currentY - 1, map) // - - // TOP LEFT CELL
aliveCount++;
if (GetCellAtXY(currentX, currentY - 1, map)); // 0 - // TOP MIDDLE CELL
aliveCount++;
if (GetCellAtXY(currentX + 1, currentY - 1, map) // + - // TOP RIGHT CELL
aliveCount++;
if (GetCellAtXY(currentX + 1, currentY, map) // + 0 // MIDDLE LEFT CELL
aliveCount++;
if (GetCellAtXY(currentX + 1, currentY + 1, map) // + + // MIDDLE RIGHT CELL
aliveCount++;
if (GetCellAtXY(currentX, currentY + 1, map) // 0 + // BOTTOM LEFT CELL
aliveCount++;
if (GetCellAtXY(currentX - 1, currentY + 1, map) // - + // BOTTOM MIDDLE CELL
aliveCount++;
if (GetCellAtXY(currentX - 1, currentY, map) // - - // BOTTOM RIGHT CELL
aliveCount++;
使用push_back
填充向量会导致在每次迭代时对每个单元格进行多次堆分配。
我不知道这是否是您的代码的一部分是瓶颈与否,您必须要知道这一点,但这似乎是一个简单的改进。
修改强>
我发布的太快了。您的问题出在GetCellAtXY
函数中。你循环(平均)一半的细胞来找到你的邻居。在每次迭代期间,每个单元格执行此操作8次!
相反,创建一个直接指向其8个邻居的单元格对象,例如:
struct Cell
{
int x, y; // Cell X, Y coordiantes
bool IsAlive; // Cell life state
std::array<Cell*,8> neighbors;
}
然后你通过循环找到邻居一次(或者你在创建它们时填充它们,这可能更好)。请注意,当您设置A.neighbor[left]=&B
时,您还需要设置B.neighbor[right]=&A
。
我相信你会得到没有指针的建议,使用指针不是正确的C ++。但我喜欢指针。
有很多选择:一个2D网格的单元格,你通过计算知道每个邻居的索引,一个std :: map,你用坐标的散列索引一个单元格等。
修改强>
这是一种可以为邻居编制索引的方法。这不一定是不那么冗长的方法,但它可以解决这些问题:
struct FieldSize {
int x, y;
}
FieldSize fieldSize{ 40, 20 };
struct Cell {
int x, y; // Cell X, Y coordiantes
bool IsAlive; // Cell life state
// ...
bool HasLeftNeighbor() {
return x != 0;
}
bool HasRightNeighbor() {
return x != fieldSize.x-1;
}
bool HasTopNeighbor() {
return y != 0;
}
bool HasTopLeftNeighbor() {
return HasLeftNeighbor() && HasTopNeighbor();
}
// ... etc.
int GetLeftNeighbor() {
return (x-1) + y * fieldSize.x;
}
int GetTopNeighbor() {
return x + (y-1) * fieldSize.x;
}
int GetTopLeftNeighbor() {
return (x-1) + (y-1) * fieldSize.x;
}
// ... etc.
}
然后在GetAdjacentCellCount中:
if (HasLeftNeighbor() && map[GetLeftNeighbor()].IsAlive)
aliveCount++;
// etc.
同样,这是非常冗长的,可以很容易地变得更加紧凑,也可能更高效,但我想强调你的逻辑。