0

I don't know how to word the title correctly but I was wondering if there is any way for me to be able to clear a single space from the console instead of clearing the entire thing just to re-write it again? for example, say I was to draw out a 3 by 3 square with the numbers counting from 1-9 all the way down, is there a way for me to change the final 9 without clearing all the prior numbers. when I clear the entire console it creates a flicker effect which is very annoying. if you want to see an example of this in code here is the thing I plan to use it on:

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.

1 Answers1

0

I found a useful function in Pascal that allow you to select a position in the console to move the "cursor" to from which point you can write in that location. the function is called GoToXY(). documentation can be found here: https://www.freepascal.org/docs-html/rtl/crt/gotoxy.html.