For the school project I write a snake on c #. There are 3 classes: the main class of the game is Programm, Snake and FoodFactory. When implementing the movement of tail parts using recursion (there is no other solution in the head), an error occurs. Thank.

Programm.cs

using System; using System.Threading; using System.Collections.Generic; using System.Linq; namespace std { class Game { static void Main() { int n = 7; char[,] field = new char[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) if (i == 0 || i == n-1 || j == 0 || j == n-1) { field[i, j] = '#'; } else { field[i, j] = ' '; } } FoodFactory.generateNewFood(field, n); //show(field, n); Snake snake = new Snake(2,1, field); while (true) { Console.Clear(); show(field, n); char c = (char)Console.Read(); if (c == 'w') { field = snake.move( snake.x - 1 , snake.y, field); } if (c == 's') { field = snake.move(snake.x + 1, snake.y, field); } if (c == 'a') { field = snake.move(snake.x , snake.y - 1, field); } if (c == 'd') { field = snake.move(snake.x, snake.y + 1, field); } } }// Main() public static void show(char[,] field, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(field[i, j]); } Console.Write("\n"); } } } } 

Snake.cs

 using System; using System.Collections.Generic; using System.Linq; namespace std { internal class Snake { public int x; public int y; private int x0; private int y0; private char HeadSnake; private Snake anotherBody = null; private static List<Snake> snakes = new List<Snake>(); private static FoodFactory foodFactory = new FoodFactory ('0'); public bool isFeed = false ; public Snake(int x, int y, char [,] field) { this.x = x; this.y = y; field[x, y] = '@'; } public char[,] move(int x1, int y1, char[,] mainField) { char[,] field = mainField; if (field[x1, y1] == '#' || field[x1, y1] == '@') { theEnd(); } if (field[x1, y1] == '0') { isFeed = true; anotherBody = new Snake(x1,y1,field); field = FoodFactory.generateNewFood(field, 7); } x0 = x; y0 = y; field[x, y] = ' '; x = x1; y = y1; field[x, y] = '@'; if (anotherBody != null) { field = anotherBody.move(x0, y0, field, isFeed); } isFeed = false; return field; } public char[,] move(int x1, int y1, char[,] mainField, bool Feed) { char[,] field = mainField; if (Feed) { isFeed = true; anotherBody = new Snake(x1,y1,field); } x0 = x; y0 = y; field[x, y] = ' '; x = x1; y = y1; field[x, y] = '@'; if (anotherBody != null) { field = anotherBody.move(x0, y0, field, isFeed); } return field; } public static void theEnd () { Console.Clear(); Console.WriteLine("YOU LOSE"); Console.ReadKey(); } } } 
  • Well, you call this public char[,] move(int x1, int y1, char[,] mainField, bool Feed) with the parameter Feed=true , and then recursively and endlessly call this field = anotherBody.move(x0, y0, field, isFeed); - tym32167

0