Console Game

This would have been a really good learning exorcise except for the fact that it doesn’t work. I understand what it’s supposed to be doing, but I don’t understand why it’s not. Normally I’d ask what I’m doing wrong, but I ran it straight from the project solution file and it doesn’t work there either. It compiles with no errors, but doesn’t function how it’s supposed to.

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

I passed it and it worked. What is your issue?

The game just doesn’t really function at all. I have absolutely no idea why, because it’s doing the same thing when I run it straight from the solution code. It doesn’t work in the codecademy browswer window either. I didn’t change anything at all in the solution code. The console comes up and the fruit appears, but there’s no cursor to move. The only thing I see that could potentially be an issue is the “unused parameter” hints in SuperGame.cs. I understand the lesson and the code, which I suppose it’s what’s ultimately important, but it still irks me that it’s not working and I don’t know why.

I think the issue would be in the Game.cs file if there is one. This was my code.

EDIT. I can’t seem to get the code posted and put a spoiler on it. Sorry.

using System;

namespace ConsoleGame
{
  class Game : SuperGame
  {
  	public new static void UpdatePosition(string Key, out int X, out int Y)
  	{
  		switch (Key)
    	{
      	case "LeftArrow":
        	X = -1;
        	Y = 0;
        	break;         
      	case "RightArrow":
        	X = 1;
        	Y = 0;
        	break;
        case "UpArrow":
        	X = 0;
        	Y = -1;
        	break;        
      	case "DownArrow": 
        	X = 0;
        	Y = 1;
        	break;
      	default:
        	X = 0;
        	Y = 1;
        	break;
      }
  	}
    public new static char UpdateCursor(string Key)
    {
   		switch (Key)
    	{
      	case "LeftArrow":
					return '<';
        	break;         
      	case "RightArrow":
        	return '>';
        	break;
        case "UpArrow":
        	return '^';
        	break;        
      	case "DownArrow": 
        	return 'v';
        	break;
      	default:
					return '<';
        	break;
      }   
    }
    public new static int KeepInBounds(int Coordinate, int MaxValue)
    {
    	return Coordinate < 0 ? 0 : Coordinate >= MaxValue ? MaxValue - 1 : Coordinate;
    }
    public new static bool DidScore(int x1, int x2, int y1, int y2)
    {
    	return x1 == x2 && y1 == y2;
    }
  }
}

So I figured out part of it, the y coordinate is like a million rows long instead of being constrained to the console window. I had to scroll all the way up to find the cursor, but even still whenever I touch any arrows to move it, it just jumps to the right of the fruit and blinks. Notice the size of the scroll bar in the screen shot. I made the changes from the code you posted as well, same issue.

I struggle to understand the KeepInBounds() method. According to the project it asks for, Every time a key is pressed, it will be called twice: once for x and once for y. The method will constrain a coordinate between 0 and its maximum value to keep the character from going off-screen.

In that method, the exercise suggests us to use two arguments, one for the coordinate value and one for the maximum value.

Suppose we have, int KeepInBounds(int pos, int maxValue). The pos should be the coordinate in the x Axis and y Axis? I know that coordinates in a Cartesian coordinate system follow a principle like (2,9), where X and Y values accordingly. How can we find the MaxValue of the screen or can we declare it at our own preference, for example, to take the value of 20?

I can see that maxValue is an integer, but what does that integer tell the program to do? What that value represents? 20 pixels, or something else?

I also noticed that Game.cs does not have a static void Main(string args) like Program.cs, so I suppose that any method that we need to call should be done inside the Program.cs file.

Hi @psmilliorn,

Could you please explain the code inside KeepInBounds(int Coordinate, int MaxValue) method?

Hello, @madaskalas.

If you look in the Program.cs file at lines 12-14:

// Determine bounds and set starting positions
int rows = Console.BufferHeight;
int cols = Console.BufferWidth;

Then at the KeepInBounds method calls on line 61-64:

// Keep character in bounds
// Uses KeepInBounds()
characterCol = Game.KeepInBounds(characterCol, cols);
characterRow = Game.KeepInBounds(characterRow, rows);

You can see that the maximum value for the X axis (cols) is determined by the Console.BufferWidth and the maximum value for the Y axis (rows) is determined by the Console.BufferHeight. Each time a key that moves the ‘character’ is pressed, the KeepInBounds() method is called to assign the characterCol (X coordinate) and the characterRow (Y coordinate).

I haven’t seen your particular KeepInBounds() method, so I can’t say for sure how it behaves, but it should either prevent the X and Y coordinates from exceeding the maximum value for each by returning the maximum value when either is greater than the maximum value and returning 0 if either is less than 0, or by allowing the character to ‘loop around’ to the opposite side of the screen.

You are correct. The functions in your Game.cs file are all called by code in the Program.cs file. That doesn’t mean that we couldn’t include a call to another method inside a method in the Game.cs file, but in this project, there is no need to do so.

1 Like

Thanks, @midlindner

I have run to multiple compiling errors, except for one that it seems that I can not solve and it keeps bugging me. Here is the source code to the error that it relates to.

public new static int KeepInBounds(int pos, int maxValue) {
      if (pos > maxValue) {
        return maxValue;
      }
      else if (pos < 0){
        return 0;
      }
    }

Error–>

Consider this: What if pos is any valid value? You have too high and too low covered, but what about a value that is in bounds?

I wrote the source code keeping that in mind and in regards to your last quote, I still get that error.

Source Code -->

public new static int KeepInBounds(int pos, int maxValue) {
      if (pos > maxValue) {
        return maxValue;
      }
      if (pos < 0) {
        return 0;
      }
      if (pos >= 0 && pos < maxValue) {
        return pos;
      }
    }

Error -->

You’re overthinking this just a bit. In your previous code you had all out of bounds coordinates accounted for, and were set to return an in bounds coordinate. Since you had all out of bounds coordinates covered, all that remained were in bounds coordinates. No need for an additional if statement. An else at the end of your original if ... else if would be a perfect fit:

The reason for your most recent error is you still have one possibility that is unaccounted for. What if pos were exactly equal to maxValue?

Thanks, @midlindner,

That fixed it. The program now runs without any problems.

However, I have another question. In the arrow button section, how these coordinates (X and Y points) equal these values? Ohh, and happy birthday! :birthday:

Image Attached -->

1 Like

Thanks!
To answer your question, we’re not actually using a grid like your picture indicates with an X and Y axis where (0, 0) is at the center. We are using rows (y coordinate) and columns (x coordinate). The top-left corner of the console is (0, 0) => column 0 & row 0. To move diagonally down and to the right, our new position would be (1, 1) => column 1 & row 1. From there, going two spaces to the right, our new position would be (3, 1) => column 3 & row 1.

Got it, Thanks, @midlindner.

I’m not sure why mine isn’t working…

public new static int KeepInBounds ( int Coordinate, int maxValue)
{
if (Coordinate >= maxValue)
{
return maxValue;
}
else
return Coordinate;
}

Hello, @jonathancohen2946373, and welcome to the forums.

The name of the function is KeepInBounds. You’ve handled the right and bottom boundary with maxValue, but what about the left and top boundary?

Also, proper indentation and use of curly braces helps make your code easier to read. The missing curly braces around your else block don’t prevent the code from running, but consistency will pay off big as you write longer and more complex code.

I’m not quite sure I follow but if the top left corner is Coordinate (0,0), then I should put something that will return 0 at the extremities?

public new static int KeepInBounds ( int Coordinate, int maxValue)
{
if (Coordinate >= maxValue)
{
return maxValue;
}

  else if (Coordinate < 0)
 {
   return 0;
  }



   else
       {
         return Coordinate;
            }
 }

Yes, that is what you’re missing. You have to account for attempting to move to a coordinate that is less than zero.