Console Game Has A Error

Link to project:
https://www.codecademy.com/practice/projects/console-game-challenge-project

Code:
Game.cs:

using System; namespace ConsoleGame { class Game : SuperGame { public new static void UpdatePosition(string key, out int changeX, out int changeY) { changeX = 0; changeY = 0; switch (key) { case "DownArrow": changeY = 1; break; case "UpArrow": changeY = -1; break; case "LeftArrow": changeX = -1; break; case "RightArrow": changeX = 1; break; } } public new static char UpdateCursor(string key) { char symbol; switch (key) { case "LeftArrow": symbol = "<"; break; case "RightArrow": symbol = ">"; break; case "UpArrow": symbol = "^"; break; case "DownArrow": symbol = "v"; break; } return symbol; } public new static int KeepInBounds(int coord, int maxCoord) { if (coord < 0) { return 0; } else if (coord > maxCoord - 1) { return maxCoord - 1; } } public new static bool DidScore(int characterX, int characterY, int fruitX, int fruitY) { if ((characterX == fruitX) && (characterY == fruitY)) { return true; } return false; } } }

Program.cs:

using System; namespace ConsoleGame { class Program { static void Main(string[] args) { Random rand = new Random(); Console.CursorVisible = false; // Determine bounds and set starting positions int rows = Console.BufferHeight; int cols = Console.BufferWidth; char cursor = '<'; int characterRow = rows / 2; int characterCol = cols / 2; char fruit = '@'; int fruitRow = rand.Next(1, rows); int fruitCol = rand.Next(1, cols); int score = 0; // Code in this loop executes infinitely // unless Q or CTRL + C is pressed while (true) { // Draw score, character, and fruit Console.Clear(); Console.SetCursorPosition(0, 0); Console.Write($"Score: {score}"); Console.SetCursorPosition(characterCol, characterRow); Console.Write(cursor); Console.SetCursorPosition(fruitCol, fruitRow); Console.Write(fruit); // Capture user input ConsoleKeyInfo cki = Console.ReadKey(false); // End game if Q is pressed if (cki.Key == ConsoleKey.Q) { Console.Clear(); Console.SetCursorPosition(0, 0); Console.CursorVisible = true; break; } // Change character position based on key // Uses UpdatePosition() string key = cki.Key.ToString(); int colChange = 0; int rowChange = 0; Game.UpdatePosition(key, out colChange, out rowChange); characterCol += colChange; characterRow += rowChange; // Update character symbol // Uses UpdateCursor() cursor = Game.UpdateCursor(key); // Keep character in bounds // Uses KeepInBounds() characterCol = Game.KeepInBounds(characterCol, cols); characterRow = Game.KeepInBounds(characterRow, rows); // Update score and fruit if player scored // Uses DidScore() if (Game.DidScore(characterCol, characterRow, fruitCol, fruitRow)) { score++; fruitCol = rand.Next(cols); fruitRow = rand.Next(rows); } } } } }

SuperGame.cs:

using System; namespace ConsoleGame { class SuperGame { public static void UpdatePosition(string key, out int xChange, out int yChange) { xChange = 0; yChange = 0; } public static char UpdateCursor(string key) { return '<'; } public static int KeepInBounds(int dimension, int max) { return dimension; } public static bool DidScore(int x1, int y1, int x2, int y2) { return false; } } }

Why is the Game.cs file causing an error?

What is the error?

1 Like

I have spent about 40 minutes but there is still errors. A lot of my past projects(maybe about 30%) still have errors.
This:


.

You don’t generally call the Main function, but perhaps you’ve been told to. When you call any function in C#, you provide the parameters, but not the type of parameter. Just like you don’t write:

int x = 5;
System.Console.WriteLine(int x);
//this definitely throws an error

That is where all the errors stem from.

1 Like

It didn’t tell me to.

I used this line so that I could provide a default value. I didn’t want it to just be a letter but just the current cursor:
image

I just mean that I have to go to the Program class which is in program.cs and get the cursor shape.
Fail!
image


Oh no!

Main is a function which returns nothing. What are you trying to access using . from it?

Well yes. What did you think would happen? Why are you trying to call the Main function anyway?

1 Like

To get the cursor field variable which is in Main.
Look:


I want this:
If the parameter of the UpdatePosition method it doesn’t mathch any standards of switch and it just sets it to the Program class’ cursor variable(is it a field).

You can’t access variables within a function using . notation. That is for accessing properties of classes. If you want a function to return a variable, then you can use the value stored in variable elsewhere.

1 Like

This doesn’t work:
Game.cs:

using System; namespace ConsoleGame { class Game : SuperGame { public new static void UpdatePosition(string key, out int changeX, out int changeY) { changeX = 0; changeY = 0; switch (key) { case "DownArrow": changeY++; break; case "UpArrow": changeY++; break; case "LeftArrow": changeX--; break; case "RightArrow": changeX ++; break; } } public new static char UpdateCursor(string key) { string[] args = {}; char symbol = Program.Main; switch (key) { case "LeftArrow": symbol = '<'; break; case "RightArrow": symbol = '>'; break; case "UpArrow": symbol = '^'; break; case "DownArrow": symbol = 'v'; break; default: symbol = '|'; break; } return symbol; } public new static int KeepInBounds(int coord, int maxCoord) { if (coord < 0) { return 0; } else if (coord > maxCoord - 1) { return maxCoord - 1; } return coord; } public new static bool DidScore(int characterX, int characterY, int fruitX, int fruitY) { if ((characterX == fruitX) && (characterY == fruitY)) { return true; } return false; } } }

Program.cs:

using System; namespace ConsoleGame { class Program { public static char Main(string[] args) { Random rand = new Random(); Console.CursorVisible = false; // Determine bounds and set starting positions int rows = Console.BufferHeight; int cols = Console.BufferWidth; char cursor = '<'; int characterRow = rows / 2; int characterCol = cols / 2; char fruit = '@'; int fruitRow = rand.Next(1, rows); int fruitCol = rand.Next(1, cols); int score = 0; // Code in this loop executes infinitely // unless Q or CTRL + C is pressed while (true) { // Draw score, character, and fruit Console.Clear(); Console.SetCursorPosition(0, 0); Console.Write($"Score: {score}"); Console.SetCursorPosition(characterCol, characterRow); Console.Write(cursor); Console.SetCursorPosition(fruitCol, fruitRow); Console.Write(fruit); // Capture user input ConsoleKeyInfo cki = Console.ReadKey(false); // End game if Q is pressed if (cki.Key == ConsoleKey.Q) { Console.Clear(); Console.SetCursorPosition(0, 0); Console.CursorVisible = true; Console.Write($"Score: {score}."); break; } // Change character position based on key // Uses UpdatePosition() string key = cki.Key.ToString(); int colChange = 0; int rowChange = 0; Game.UpdatePosition(key, out colChange, out rowChange); characterCol += colChange; characterRow += rowChange; // Update character symbol // Uses UpdateCursor() cursor = Game.UpdateCursor(key); // Keep character in bounds // Uses KeepInBounds() characterCol = Game.KeepInBounds(characterCol, cols); characterRow = Game.KeepInBounds(characterRow, rows); // Update score and fruit if player scored // Uses DidScore() if (Game.DidScore(characterCol, characterRow, fruitCol, fruitRow)) { score++; fruitCol = rand.Next(cols); fruitRow = rand.Next(rows); } } return cursor; } } }

Error:


Also, what is an warning and exception in C#?

After some time I figured out to get cursor I can’t use a char return type. I cleverly thought maybe I could use out.

Too bad it doesn’t work. I have a big pile of errors. Let’s not worry about getting the cursor variable and returning it and using it as if the user doesn’t press a key(in the Game.cs and UpdateCursor function).
Here is my code:
Game.cs:

using System; namespace ConsoleGame { class Game : SuperGame { public new static void UpdatePosition(string key, out int changeX, out int changeY) { changeX = 0; changeY = 0; switch (key) { case "DownArrow": changeY++; break; case "UpArrow": changeY++; break; case "LeftArrow": changeX--; break; case "RightArrow": changeX ++; break; } } public new static char UpdateCursor(string key) { char symbol = Program.GetCursor(); switch (key) { case "LeftArrow": symbol = '<'; break; case "RightArrow": symbol = '>'; break; case "UpArrow": symbol = '^'; break; case "DownArrow": symbol = 'v'; break; default: symbol = '|'; break; } return symbol; } public new static int KeepInBounds(int coord, int maxCoord) { if (coord < 0) { return 0; } else if (coord > maxCoord - 1) { return maxCoord - 1; } return coord; } public new static bool DidScore(int characterX, int characterY, int fruitX, int fruitY) { if ((characterX == fruitX) && (characterY == fruitY)) { return true; } return false; } } }

Program.cs:

using System; namespace ConsoleGame { class Program { public static void Main(string[] args, out char cursor) { Random rand = new Random(); Console.CursorVisible = false; // Determine bounds and set starting positions int rows = Console.BufferHeight; int cols = Console.BufferWidth; cursor = '<'; int characterRow = rows / 2; int characterCol = cols / 2; char fruit = '@'; int fruitRow = rand.Next(1, rows); int fruitCol = rand.Next(1, cols); int score = 0; // Code in this loop executes infinitely // unless Q or CTRL + C is pressed while (true) { // Draw score, character, and fruit Console.Clear(); Console.SetCursorPosition(0, 0); Console.Write($"Score: {score}"); Console.SetCursorPosition(characterCol, characterRow); Console.Write(cursor); Console.SetCursorPosition(fruitCol, fruitRow); Console.Write(fruit); // Capture user input ConsoleKeyInfo cki = Console.ReadKey(false); // End game if Q is pressed if (cki.Key == ConsoleKey.Q) { Console.Clear(); Console.SetCursorPosition(0, 0); Console.CursorVisible = true; Console.Write($"Score: {score}."); break; } // Change character position based on key // Uses UpdatePosition() string key = cki.Key.ToString(); int colChange = 0; int rowChange = 0; Game.UpdatePosition(key, out colChange, out rowChange); characterCol += colChange; characterRow += rowChange; // Update character symbol // Uses UpdateCursor() cursor = Game.UpdateCursor(key); // Keep character in bounds // Uses KeepInBounds() characterCol = Game.KeepInBounds(characterCol, cols); characterRow = Game.KeepInBounds(characterRow, rows); // Update score and fruit if player scored // Uses DidScore() if (Game.DidScore(characterCol, characterRow, fruitCol, fruitRow)) { score++; fruitCol = rand.Next(cols); fruitRow = rand.Next(rows); } } } public static char GetCursor() { return Main; } } }

SuperGame.cs:

using System; namespace ConsoleGame { class SuperGame { public static void UpdatePosition(string key, out int xChange, out int yChange) { xChange = 0; yChange = 0; } public static char UpdateCursor(string key) { return '<'; } public static int KeepInBounds(int dimension, int max) { return dimension; } public static bool DidScore(int x1, int y1, int x2, int y2) { return false; } } }

I’ll give you a little bit of a picture of my errors:

Is getting a variable from a Main method in the same namespace even possible? I can’t really use return or out.

No. I’ve been trying to say this for a while. Main must return void and accept a String[] argument. Otherwise it isn’t the entry point for the program. You also don’t really call it using class.Main, as its functionality isn’t part of the class it’s “in”. Why not create other method in your Program class to return some of the variables you’re using.
EDIT: If you follow the instructions on the side of the lesson, I’m sure you’ll do fine.

4 Likes

How am I going to do that? I can’t just create another method that gets the method since it is in the Main method. Now what?

Read through the instructions. They will guide you through it. Ignore my idea; according to the instructions, you don’t have to alter any code in Program.cs.

1 Like

You don’t get it. I am extending the project not following instructions. So do you mean I can’t create another method that gets the variable. Yesterday I thought of a solution:
because the method call return value of UpdateCursor() is reassigned to itself. I can break the problem down:
The UpdateCursor() method:
set the cursor variable which is going to be the return value of the UpdateCursor value and is also going to be reassigned to itself, if the key parameter is not up, right, left, down the variable is not going to be changed for the rest of the method since it doesn’t match the up, right… keys. I makes sense for if the key parameter(it is going to be the representation of key presed), is not one of the arrow keys(up, down, left, right), the cursor variable should be the same as the original cursor variable in Program.cs. I can’t get that variable from Main or another method.

check if a key parameter equals right key, left key, up key, down key:
set appropiate values to the cursor:
if right key:
>
if left key:
<
if up key:
^
If down key:
v

after set return the cursor.
In Program.cs:
UpdateCursor() gets called and gets reassigned to the same cursor variable.
This is to change the cursor shape.

Let me explain my solution:
If I can’t get the cursor variable from Program.cs then this:
I can’t find the original cursor shape; if the key parameter didn’t represent up, down, left, right than I would be reassigning the cursor variable in Program.cs to itself(not nessesary and also I have some benefits). I have a smart things:
What if I reassign the cursor variable to itself only if the key pressed is up, down, left, right?
Benefit of that:
The only cursor possibilities of the key parameter is a string value representing:
A left, right, up, down.
Then I don’t need to find the original cursor key since it would match one the four conditions(a case statement in a switch statement). If it was right, down, left right then the returned value would be either ^, v, >, <. So I wouldn’t have to get the original cursor variable since it wouldn’t be it by the end. What a good plan!

Oh no! This shows an error:
image
image