C# – programming snake in c# with console

ascii, c++, console, logic

I'm trying to make a snake in the console to keep me in training in c #. I have a problem in the movement. To test I wanted to do so by pressing the letter "a" would shift the "food" on the X I noticed that in terms of graphics, but does not move through a Console.Write is incremented. Can you give me a hand? I am attaching the main classes, world food

Main:

namespace Snake{    class Program    {        static void Main(string[] args)        {            char s;            World mondo = new World(20, 10);            do            {                s = char.Parse(Console.ReadLine());                mondo.update();                mondo.drawWorld();            }            while (s == 'a');            Console.ReadLine();        }    }}

World:

namespace Snake{    class World    {        public int larghezza;        public int altezza;        public int sizeSnake = 3;        public Food food;        public Snake[] snake;        public Snake s;        public enum Box { FREE , FOOD, SNAKE };//0,1,2        public Box[,] size;        public World(int larghezza, int altezza)        {            this.larghezza = larghezza;            this.altezza = altezza;            size = new Box[larghezza, altezza];            food = new Food();            snake = new Snake[sizeSnake];            s = new Snake();            food.InstaceFood(altezza, larghezza);            size[food.pos.x,food.pos.y] = Box.FOOD;            for (int i = 0; i < sizeSnake; i++)            {                snake[i] = s;                snake[0].startSnake(altezza, larghezza);                size[snake[i].pos.x-i, snake[i].pos.y] = Box.SNAKE;            }       }        public void update()        {           // snake[0].move(snake);            food.moveee();        }        public void control (Box type)        {            switch (type)            {                case Box.FOOD:                     Console.ForegroundColor = ConsoleColor.Red;                     Console.Write('X');                     Console.ResetColor();                     break;                case Box.FREE:                     Console.Write(' ');                     break;                case Box.SNAKE:                     Console.ForegroundColor = ConsoleColor.Green;                     Console.Write('█');                     Console.ResetColor();                     break;            }        }        public void drawWorld()        {            Console.Write('╔');            for (int i = 0; i < larghezza; i++)            {                Console.Write('═');            }            Console.Write('╗');            for (int i = 0; i < altezza; i++)            {                Console.Write('\n');                for (int j = 0; j < larghezza; j++)                {                    if (j == 0)                    {                        Console.Write('║');                    }                       control(size[j,i]);                        if (j == larghezza - 1)                        Console.Write('║');                }            }            Console.Write('\n');            Console.Write('╚');            for (int i = 0; i < larghezza; i++)            {                Console.Write('═');            }            Console.WriteLine('╝');        }    }}

Food:

namespace Snake{    class Food    {        Random rnd = new Random();        public Vector2 pos;        public Food()        {        }        public void InstaceFood(int _Altezza, int _Larghezza)        {            int rndY = rnd.Next(0, _Altezza);            int rndX = rnd.Next(0, _Larghezza);            pos.x = rndX;            pos.y = rndY;        }        public void moveee()        {            this.pos.x += 1;        }    }}

Vector2:

namespace Snake{    struct Vector2    {        public int x;        public int y;        public Vector2(int xx, int yy)        {            x = xx;            y = yy;        }    }}

Best Solution

The food does not move because you are not updating Box[,] size with the new values. Try this modification:

public void update(){  // snake[0].move(snake);  size[food.pos.x, food.pos.y] = Box.FREE;  food.moveee();  size[food.pos.x, food.pos.y] = Box.FOOD;}