Question
Maybe the third time is a charm. This section of my csharp vs2019 class project. I get errors. program.cs public class Program { static void
program.cs
public class Program { static void Main(string[] args) { Console.CursorVisible = false; int numberOfFruitEaten = 0; List xPosition = new List(); List yPosition = new List(); xPosition.Add(25); yPosition.Add(20); Snake snake = new Snake(xPosition, yPosition, numberOfFruitEaten); Console.SetCursorPosition(snake.XPosition[0], snake.YPosition[0]); Console.WriteLine(((char)214).ToString()); ConsoleKey playerKey = Console.ReadKey().Key; Fruit fruit = new Fruit(); Console.WriteLine(fruit); do { Console.SetCursorPosition(snake.XPosition[numberOfFruitEaten], snake.XPosition[numberOfFruitEaten]); Console.WriteLine(\" \"); bool isEaten = false; if (snake.XPosition[0] == fruit.XPosition && snake.YPosition[0] == fruit.YPosition) isEaten = true; if (isEatean) { Console.WriteLine(fruit); isEaten = false; numberOfFruitEaten++; snake.XPosition.Add(snake.XPosition[0]); snake.YPosition.Add(snake.YPosition[0]); } List xold = snake.XPosition; List yold = snake.YPosition; snake.NumberOfFruitEaten = numberOfFruitEaten; for (int j = numberOfFruitEaten; j > 0; j--) { xold[j] = xold[j - 1]; yold[j] = yold[j - 1]; } if (playerKey == ConsoleKey.LeftArrow) snake.MoveLeft(); else if (playerKey == ConsoleKey.RightArrow) snake.MoveRight(); else if (playerKey == ConsoleKey.UpArrow) snake.MoveUp(); else if (playerKey == ConsoleKey.DownArrow) snake.MoveDown(); Console.SetCursorPosition(snake.XPosition, snake.YPosition); Console.WriteLine(snake); snake.XPosition = xold; snake.YPosition = yold; if (Console.KeyAvailable) playerKey = Console.ReadKey().Key; System.Threading.Thread.Sleep(240); } while (playerKey == ConsoleKey.LeftArrow || playerKey == ConsoleKey.RightArrow || playerKey == ConsoleKey.UpArrow || playerKey == ConsoleKey.DownArrow); Console.ReadKey(); } } }
errors
Program.cs CS0021 Cannot apply indexing with [] to an expression of type 'int' 20 CS0021 Cannot apply indexing with [] to an expression of type 'int' 20 CS0021 Cannot apply indexing with [] to an expression of type 'int' 27 CS0021 Cannot apply indexing with [] to an expression of type 'int' 28 CS0021 Cannot apply indexing with [] to an expression of type 'int' 31 CS0021 Cannot apply indexing with [] to an expression of type 'int' 32 CS0103 The name 'isEatean' does not exist in the current context 34 CS1061 'int' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) 39 CS0021 Cannot apply indexing with [] to an expression of type 'int' 39 CS1061 'int' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) 40 CS0021 Cannot apply indexing with [] to an expression of type 'int' 40 CS0029 Cannot implicitly convert type 'int' to 'System.Collections.Generic.List' 42 CS0029 Cannot implicitly convert type 'int' to 'System.Collections.Generic.List' 43 CS1061 'Snake' does not contain a definition for 'NumberOfFruitEaten' and no accessible extension method 'NumberOfFruitEaten' accepting a first argument of type 'Snake' could be found (are you missing a using directive or an assembly reference?) 44 CS0029 Cannot implicitly convert type 'System.Collections.Generic.List' to 'int' 60 CS0029 Cannot implicitly convert type 'System.Collections.Generic.List' to 'int' 61
snake.cs
public class Snake { // fields and properties //private int xPosition; //private int yPosition; private List xPosition; private List yPosition; private int numberOfFruitEaten; public int XPosition { get { return xPosition; } set { xPosition = value; } } public int YPosition { get { return yPosition; } set { yPosition = value; } } //constructor and method public Snake(List xPosition1, int xPosition, int yPosition) { this.xPosition = xPosition; this.yPosition = yPosition; }
public Snake(List xPosition, List yPosition, int numberOfFruitEaten) { this.xPosition = xPosition; this.yPosition = yPosition; this.numberOfFruitEaten = numberOfFruitEaten; }
public void MoveLeft() { XPosition--; } public void MoveRight() { XPosition++; } public void MoveUp() { YPosition--; } public void MoveDown() { YPosition++; } public override string ToString() { Console.SetCursorPosition(XPosition[0], YPosition[0]); Console.WriteLine(((char)214).ToString()); for (int i = 1; i => { Console.SetCursorPosition(XPosition[i], YPosition[i]); Console.Write(\"o\"); } return \"\"; } } }
these errors
CS0029 Cannot implicitly convert type 'System.Collections.Generic.List' to 'int' 20 CS0029 Cannot implicitly convert type 'int' to 'System.Collections.Generic.List' 21 CS0029 Cannot implicitly convert type 'System.Collections.Generic.List' to 'int' 25 CS0029 Cannot implicitly convert type 'int' to 'System.Collections.Generic.List' 26 CS0029 Cannot implicitly convert type 'int' to 'System.Collections.Generic.List' 31 CS0029 Cannot implicitly convert type 'int' to 'System.Collections.Generic.List' 32 CS0021 Cannot apply indexing with [] to an expression of type 'int' 60 CS0021 Cannot apply indexing with [] to an expression of type 'int' 60 CS0021 Cannot apply indexing with [] to an expression of type 'int' 64 CS0021 Cannot apply indexing with [] to an expression of type 'int' 64
I get no errors on fruit.cs
public class Fruit { //fields and properties private int xPosition; private int yPosition; public int XPosition { get { return xPosition; } set { xPosition = value; } } public int YPosition { get { return yPosition; } set { yPosition = value; } } public override string ToString() { Random random = new Random(); XPosition = random.Next(2, Console.WindowWidth - 2); YPosition = random.Next(2, Console.WindowHeight - 2); Console.SetCursorPosition(XPosition, YPosition); return ((char)64).ToString(); }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started