从控制台清除单个 space

Clearing a single space from the console

我不知道如何正确表达标题,但我想知道是否有任何方法可以让我从控制台清除单个 space 而不是清除整个内容只是为了re-write又来了?例如,假设我要绘制一个 3 x 3 的正方形,数字从 1 到 9 一直向下计数,有没有办法让我在不清除所有先前数字的情况下更改最后的 9。当我清除整个控制台时,它会产生一种非常烦人的闪烁效果。如果你想在代码中看到这样的例子,我打算在上面使用它:

program randomPath;

uses crt, sysutils;

type StrMultiArray = array of array of String;

var 
    finishedBoard : StrMultiArray;
    size, i, j : integer;
    regenerate : boolean;
    
function generateMap(size : integer) : StrMultiArray;

var
    posx, posy, choice, counter : integer;
    ongoing : boolean;
    board : StrMultiArray;

begin
    setLength(board, size, size);
    for i := 0 to (size-1) do
    begin
        for j := 0 to (size-1) do
        begin
            board[i,j] := '#';
        end;
    end;
    
    posx := (size div 2);
    posy := (size div 2);
    board[posx,posy] := ' ';
    ongoing := true;
    counter := 0;
    while ongoing = true do
    begin
        choice := random(2);
        if (choice = 0) then
        begin
            choice := random(2);
            if (choice = 0) then
            begin
                if (posx > 0) then
                begin
                    posx := posx - 1;
                end;
            end
            else
            begin
                if (posx < size-1) then
                begin
                    posx := posx + 1;
                end;
            end; 
        end
        else
        begin
            choice := random(2);
            if (choice = 0) then
            begin
                if (posy > 0) then
                begin
                    posy := posy - 1;
                end;
            end
            else
            begin
                if (posy < size-1) then
                begin
                    posy := posy + 1;
                end;
            end; 
        end;
        counter := counter + 1;
        board[posx,posy] := ' ';
        if counter = (size * 12) then
        begin
            ongoing := false;
        end;
    end;
    
    generateMap := board;
end;


procedure printBoard(board : StrMultiArray; size : integer);    
begin
    textColor(Cyan);
    write('+');
    for i := 0 to (size-1) do
    begin
        write('-');
    end;
    writeLn('+');

    for i := 0 to (size-1) do
    begin
        textColor(Cyan);
        write('|');
        textColor(White);
        for j := 0 to (size-1) do
        begin
            if (board[i,j] = '#') then
            begin
                textBackground(White);
            end;
            if (board[i,j] = '@') then
            begin
                textBackground(Red);
            end;
            //write(board[i,j]);
            write(' ');
            textBackground(Black);
        end;
        textColor(Cyan);
        writeLn('|');
    end;
    
    textColor(Cyan);
    write('+');
    for i := 0 to (size-1) do
    begin
        write('-');
    end;
    writeLn('+');
    textColor(White);
end;


procedure movePlayer(board : StrMultiArray; size : integer);

var
    ongoing : boolean;
    posx, posy, prevx, prevy : integer;
    input : char;

begin
    ongoing := true;
    posx := (size div 2);
    posy := (size div 2);
    while (ongoing = true) do
    begin
        board[posx,posy] := '@';
        prevx := posx;
        prevy := posy;
        printBoard(board, size);
        input := readKey();
        clrScr();
        case input of
            'w' :
                if (posx > 0) then
                begin
                    if (board[posx-1,posy] = ' ') then
                        posx := posx - 1;
                end;
            'a' :
                if (posy > 0) then
                begin
                    if (board[posx,posy-1] = ' ') then
                        posy := posy - 1;
                end;
            's' :
                if (posx < (size-1)) then
                begin
                    if (board[posx+1,posy] = ' ') then
                        posx := posx + 1;
                end;
            'd' :
                if (posy < (size-1)) then
                begin
                    if (board[posx,posy+1] = ' ') then
                        posy := posy + 1;
                end;
            'x' :
                begin
                    regenerate := false;
                    ongoing := false;
                end;
        else
            ongoing := false;
        end;
        board[prevx,prevy] := ' ';
    end;
    
end;


begin
    size := 10;
    regenerate := true;
    randomize;
    while (regenerate = true) do
    begin
        finishedBoard := generateMap(size);
        movePlayer(finishedBoard, size);
    end;
end.

我在 Pascal 中发现了一个有用的函数,它允许您 select 在控制台中的一个位置将“光标”移动到您可以在该位置写入的位置。该函数称为 GoToXY()。可以在此处找到文档:https://www.freepascal.org/docs-html/rtl/crt/gotoxy.html.