人生游戏——印版到同一位置
Game of Life - Printing board to the same position
#include <iostream>
using namespace std;
int board[10][10] = {{0,1,0,0,0,1,1,0,0,0},
{0,0,0,0,0,1,1,1,0,0},
{0,0,1,0,0,1,0,1,0,1},
{0,1,0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,1,0,0,0},
{0,0,0,0,0,1,1,1,0,0},
{0,0,1,0,0,1,0,1,0,1},
{0,1,0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0}};
void PrintBoard()
{
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
if(board[i][j] == 1)
{
cout << '*';
}
else
{
cout << '-';
}
}
cout << endl;
}
}
int main()
{
bool done = false;
while(!done)
{
done = false;
PrintBoard();
int i = 0;
i++;
cout << i;
}
}
我的问题是将电路板打印到控制台的相同位置。通过这种方式,它可以在控制台上一行向下打印数百块电路板。我希望它现在是一个无限循环,因为当我让后代部分工作时,它会像你期望的那样流畅地运行。
如果您在windows终端中,则显示为http://en.wikipedia.org/wiki/ANSI_escape_code#Windows_and_DOS会有所帮助。
char csi_esc = 27;
char csi_begin = '[';
// clear screen and reposition cursor
cout<<csi_esc<<csi_begin<< "2J]";
可以作为开始。(未经测试,我无法访问windows终端)。
哎呀,我把这篇维基文章读得很差。
Win32控制台本身不支持ANSI转义序列所有人。像ANSICON[7]这样的软件可以作为包装器标准的Win32控制台,并增加对ANSI转义序列的支持。否则,软件必须使用类似ioctl的方式来操作控制台控制台API[8]与文本输出交错。一些软件在内部解释正在打印的文本中的ANSI转义序列将它们转换为这些调用。[9]
所以你应该使用常规的WinAPI调用。
#include<windows.h>
void PrintBoard(){
// Position cursor at 0,0
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = coord.Y = 0;
SetConsoleCursorPosition( console, coord );
// Draw the rest of the stuff.
}
参见使用高级输入输出函数和API参考
如果您在unix系统的终端中,只需
#include<ncurses.h>
和链接库
g++ -o life life.cpp -lncurses
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 如何使用cudaMallocManaged在指针位置初始化对象?(C++)
- 在C++游戏中与库存系统作斗争
- 无法获取webot::PositionSensor对象中位置传感器的值
- 非常量变量只读位置的赋值
- 换位表导致测试失败(但在游戏中运行良好)
- 如何定义更改car类中car位置的方法
- 使用迭代器时如何访问对象在向量中的位置?
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 在重新居中之前获取游戏中的光标位置
- 人生游戏中的随机播种不能很好地工作
- 访问冲突写入位置0x00000000.C++/SFML游戏
- 在游戏对象定义中放置的位置
- 人生游戏——印版到同一位置
- 康威's游戏人生错误
- 追踪用户登陆桌面游戏的位置