人生游戏——印版到同一位置

Game of Life - Printing board to the same position

本文关键字:位置 人生游戏      更新时间:2023-10-16
#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