How can I display only the new state of the grid?

I am making a console based snake game. I am able to move the snake using grids that show up successively on the console. I want to animate the snake using always the same grid.

    static void Main(string[] args)
{
char[,] board = new char[17, 17];
int count = 5;
for (int i = 0; i <= 16;             {
for (int j = 0; j <= 16; j++)
{
board[i, j] = '.';
}
}
while (count < 11)
{
board[8, count] = '*';
board[8, count + 1] = '*';
board[8, count + 2] = '*';
board[8, count + 3] = '*';
board[8, count + 4] = '*';
board[8, count + 5] = '*';
board[8, count + 6] = '*';

for (int i = 0; i <= 16; i++)
{
for (int j = 0; j <= 16; j++)
{
Console.Write(board[i, j]);
}
Console.WriteLine();
}
count++;
}
}


Assuming I understood your question correctly, simply call Console.Clear() before you draw your grid. This will clear the console of all text so when you draw your grid it will always be at the top.

• I put the Console.Clear() function below the while loop and it only draws one grid but the last grid. But I want the snake to animate on the one grid. – gamer67 Feb 9 '19 at 23:00
• @gamer67 Can you add a few pictures to show what you mean? – Charanor Feb 10 '19 at 2:10
• Put the clear() inside the while loop, at the beginning. If the animation is too fast, add a delay, or only move when a button is pressed – Peethor Feb 10 '19 at 17:04
• I put the clear in the bottom of the while loop and it works. I also put in a small delay. – gamer67 Feb 10 '19 at 22:46

I have made some progress. I have got the snake to move to the right and to move down.

        static void Main(string[] args)
{
const int board_size = 17;
char[,] board = new char[board_size, board_size];
int horizontal = 5, vertical = 8, count = 0;

for (int i = 0; i < board_size; i++)
{
for (int j = 0; j < board_size; j++)
{
board[i, j] = '.';
}
}

void draw_board()
{
for (int i = 0; i < board_size; i++)
{
for (int j = 0; j < board_size; j++)
{
Console.Write(board[i, j]);
}
Console.WriteLine();
}
}

while (count < 12)
{
if (count >= 0 && count < 6)
{
board[vertical, horizontal] = '*';
board[vertical, horizontal + 1] = '*';
board[vertical, horizontal + 2] = '*';
board[vertical, horizontal + 3] = '*';
board[vertical, horizontal + 4] = '*';
board[vertical, horizontal + 5] = '*';
board[vertical, horizontal + 6] = '*';

draw_board();

horizontal++;
}

if (count > 5 && count < 12)
{
board[vertical, horizontal] = '*';
board[vertical+1, horizontal] = '*';
board[vertical+2, horizontal] = '*';
board[vertical+3, horizontal] = '*';
board[vertical+4, horizontal] = '*';
board[vertical+5, horizontal] = '*';
board[vertical+6, horizontal] = '*';

draw_board();

vertical++;
}

count++;